How to make a circle with vectors

Discussion in 'Spigot Plugin Development' started by timmaker, May 5, 2015.

  1. I am trying to make a circle with vectors but the entity just flips out

    this is my code
    Code (Text):
    package net.voxelvoid.themepark.attractions;

    import java.util.ArrayList;
    import java.util.List;
    import net.voxelvoid.themepark.Themepark;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
    import org.bukkit.entity.Pig;
    import org.bukkit.event.Listener;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;

    /**
    * @author Timmaker
    */
    public class Intergalacticum implements Listener {
       
        Themepark plugin = Themepark.getInstance();
       
        List<CartGroup> groups = new ArrayList<>();
        List<CustomEntityMinecart> carts = new ArrayList<>();
        int circlecount = 0;
       
        public void setupCarts()
        {
            final CustomEntityMinecart group1Cart1 = spawnCart(-201.75, 7.5, -10.40, 135);
            final CustomEntityMinecart group1Cart2 = spawnCart(-201.75, 7.5, -8.85, 45);
            final CustomEntityMinecart group1Cart3 = spawnCart(-200.25, 7.5, -8.85, 315);
            final CustomEntityMinecart group1Cart4 = spawnCart(-200.25, 7.5, -10.40, 225);
           
            carts.add(group1Cart1);
            carts.add(group1Cart2);
            carts.add(group1Cart3);
            carts.add(group1Cart4);
        }
       
        private CustomEntityMinecart spawnCart(double x, double y, double z, float yaw)
        {
            CraftWorld handle = (CraftWorld) Bukkit.getWorlds().get(0);
            CustomEntityMinecart cart = new CustomEntityMinecart(Bukkit.getWorlds().get(0));
            cart.setPositionRotation(x, y, z, yaw, cart.getPitch());
            cart.setYaw(yaw);
            handle.getHandle().addEntity(cart);
            cart.setYaw(yaw);
            cart.positionChanged = true;
            cart.slowWhenEmpty = false;
           
            return cart;
        }
       
        public void start()
        {
            Bukkit.broadcastMessage("started!");
            doCircle(carts.get(0));
            //new Location(Bukkit.getWorlds().get(0), -208, 7.5, -10)
        }

        public void doCircle(final CustomEntityMinecart cart)
        {
            Location center = cart.getBukkitEntity().getLocation();
            final List<Vector> vectors = makeCircle(center, 7);
            new BukkitRunnable() {
                @Override
                public void run() {
                    if (!vectors.isEmpty()) {
                        if (circlecount <= vectors.size()) {
                            cart.getBukkitEntity().setVelocity(vectors.get(circlecount));
                            cart.setDerailedVelocityMod(vectors.get(circlecount));
                            circlecount++;
                        } else {
                            this.cancel();
                            circlecount = 0;
                        }
                    } else {
                        this.cancel();
                    }
                }
            }.runTaskTimer(Themepark.getInstance(), 1L, 5L);
        }
       
       
        public void test()
        {
            Location center_location = new Location(Bukkit.getWorlds().get(0), -208, 7.5, -10);
            Pig pig = center_location.getWorld().spawn(center_location, Pig.class);
            for (double angle = 0.0; angle < 360.0; angle++) {
                double x = Math.cos(angle);
                double z = Math.sin(angle);
                Vector directon = new Vector(x, 0.0, z).multiply(0.2); //You can replace the multiplier with whatever number you want
                pig.setVelocity(directon);
            }

        }
       
        public List<Vector> makeCircle(Location loc, Integer r) {
            List<Vector> ret = new ArrayList<>();
            double x;
            double y = loc.getY();
            double z;
                 
            for (double i = 0.0; i < 360.0; i += 0.1) {
            double angle = i * Math.PI / 180;
                x = (loc.getX() + r * Math.cos(angle));
                z = (loc.getZ() + r * Math.sin(angle));
                Location pos2 = new Location(loc.getWorld(), x, y, z);
                Vector v = loc.subtract(pos2).toVector().multiply(0.2D);
               
                ret.add(v);
            }
            return ret;
        }
    }
     
     
  2. A vector can't be a circle .
     
  3. But how would i make a minecart make a circle around a location
     
  4. Use cos and sin and you take a point and an other point then create a vector with them.
     
    • Agree Agree x 1
  5. You can use the trigonometry to get the next location for the minecart and create a vector to go at this location.

    (^^)
     
    • Agree Agree x 1
  6. @ryukk @maccier @Yannici How woul i do this things, im kinda noob at maths but i need this for one of my projects
     
  7. Let me the time to turn on the computer :D I write you that ;)
     
  8. I don't have tested but it should be ok :)

    Code (Text):
            int radius = 5;
            int precision = 12;
     
            for(int i = 0; i < precision; i++) {
                double p1 = (i * Math.PI) / (precision / 2);
                double p2 = (((i == 0) ? precision : i-1) * Math.PI) / (precision / 2);
         
                double x1 = Math.cos(p1) * radius;
                double x2 = Math.cos(p2) * radius;
                double z1 = Math.sin(p1) * radius;
                double z2 = Math.sin(p2) * radius;

                Vector vec = new Vector(x2-x1,0,z2-z1);

             }
    Replace for while by a Scheduler and it will run.
     
  9. @ryukk what do you mean by for while i don't see a while loop
     
  10. Your minecart turn around a location no? You have to define his position every X seconds no?
     
  11. @ryukk something like this?

    Code (Text):
        public void makeCircle(final CustomEntityMinecart cart)
        {
            final int radius = 5;
            final int precision = 12;

            new BukkitRunnable() {
                @Override
                public void run() {
                    for (int i = 0; i < precision; i++) {
                        double p1 = (i * Math.PI) / (precision / 2);
                        double p2 = (((i == 0) ? precision : i - 1) * Math.PI) / (precision / 2);

                        double x1 = Math.cos(p1) * radius;
                        double x2 = Math.cos(p2) * radius;
                        double z1 = Math.sin(p1) * radius;
                        double z2 = Math.sin(p2) * radius;

                        Vector vec = new Vector(x2 - x1, 0, z2 - z1);
                        cart.getBukkitEntity().setVelocity(vec);
                    }
                }
            }.runTaskTimer(Themepark.getInstance(), 1L, 5L);
        }
     
  12. No, like this :

    Code (Text):
        public void makeCircle(final CustomEntityMinecart cart)
        {
            final int radius = 5;
            final int precision = 12;

            new BukkitRunnable() {
                int current = 0;

                @Override
                public void run() {
                        double p1 = (current * Math.PI) / (precision / 2);
                        double p2 = (((current == 0) ? precision : current - 1) * Math.PI) / (precision / 2);

                        double x1 = Math.cos(p1) * radius;
                        double x2 = Math.cos(p2) * radius;
                        double z1 = Math.sin(p1) * radius;
                        double z2 = Math.sin(p2) * radius;

                        Vector vec = new Vector(x2 - x1, 0, z2 - z1);
                        cart.getBukkitEntity().setVelocity(vec);
                        current++;
                        if(current > precision)
                             current = 0;

                }
            }.runTaskTimer(Themepark.getInstance(), 1L, 5L);
        }
     
  13. @ryukk got some problems with that since current is final you can't add or set its value after you set it but if it isn't final the runnable cannot use it what should i do?

    EDIT: i think he edited his post, or i copied it wrong :p
     
    #14 timmaker, May 5, 2015
    Last edited: May 5, 2015
  14. #15 timmaker, May 5, 2015
    Last edited: May 5, 2015
  15. Yep you can just do :

    Code (Text):
                        double x1 = x + Math.cos(p1) * radius;
                        double x2 = x + Math.cos(p2) * radius;
                        double z1 = z + Math.sin(p1) * radius;
                        double z2 = z + Math.sin(p2) * radius;
     
  16. @ryukk that doesn't work the carts only seem to make a bigger circle but they don't go around the middle point

    this is my code atm:
    Code (Text):
    package net.voxelvoid.themepark.attractions;

    import java.util.ArrayList;
    import java.util.List;
    import net.voxelvoid.themepark.Themepark;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
    import org.bukkit.entity.Pig;
    import org.bukkit.event.Listener;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;

    /**
    * @author Timmaker
    */
    public class Intergalacticum implements Listener {
       
        Themepark plugin = Themepark.getInstance();
       
        List<CartGroup> groups = new ArrayList<>();
        List<CustomEntityMinecart> carts = new ArrayList<>();
       
        public void setupCarts()
        {
            final CustomEntityMinecart group1Cart1 = spawnCart(-201.75, 7.5, -10.40, 135);
            final CustomEntityMinecart group1Cart2 = spawnCart(-201.75, 7.5, -8.85, 45);
            final CustomEntityMinecart group1Cart3 = spawnCart(-200.25, 7.5, -8.85, 315);
            final CustomEntityMinecart group1Cart4 = spawnCart(-200.25, 7.5, -10.40, 225);
           
            carts.add(group1Cart1);
            carts.add(group1Cart2);
            carts.add(group1Cart3);
            carts.add(group1Cart4);
        }
       
        private CustomEntityMinecart spawnCart(double x, double y, double z, float yaw)
        {
            CraftWorld handle = (CraftWorld) Bukkit.getWorlds().get(0);
            CustomEntityMinecart cart = new CustomEntityMinecart(Bukkit.getWorlds().get(0));
            cart.setPositionRotation(x, y, z, yaw, cart.getPitch());
            cart.setYaw(yaw);
            handle.getHandle().addEntity(cart);
            cart.setYaw(yaw);
            cart.positionChanged = true;
            cart.slowWhenEmpty = false;
           
            return cart;
        }
       
        public void start()
        {
            Bukkit.broadcastMessage("started!");
           
            for (CustomEntityMinecart cart : carts) {
                makeCircle(cart, new Location(Bukkit.getWorlds().get(0), -208, 7, -10));
            }
            //new Location(Bukkit.getWorlds().get(0), -208, 7.5, -10)
        }
       
        public void makeCircle(final CustomEntityMinecart cart, final Location center)
        {
            final int radius = 5;
            final int precision = 12;

            new BukkitRunnable() {
                int current = 0;

                @Override
                public void run() {
                        double p1 = (current * Math.PI) / (precision / 2);
                        double p2 = (((current == 0) ? precision : current - 1) * Math.PI) / (precision / 2);

                        double x1 = center.getX() + Math.cos(p1) * radius;
                        double x2 = center.getX() + Math.cos(p2) * radius;
                        double z1 = center.getZ() + Math.sin(p1) * radius;
                        double z2 = center.getZ() + Math.sin(p2) * radius;

                        Vector vec = new Vector(x2 - x1, 0, z2 - z1);
                        cart.getBukkitEntity().setVelocity(vec);
                        cart.setDerailedVelocityMod(new Vector(1, 1, 1));
                        current++;
                        if(current > precision)
                             current = 0;

                }
            }.runTaskTimer(Themepark.getInstance(), 1L, 5L);
        }
    }
     
     
  17. Try update it every 10ticks?