Minecarts not making turns?

Discussion in 'Plugin Development' started by Dmrtje, Jan 29, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Dmrtje

    I'm trying to let a minecart go in a circle with the help of velocity, although instead of going a circle the cart goes a little bit in a circle and then it only goes straight
    [​IMG]
    This gif is tested with a radius of 5 instead of 7.5, but they do have the same result.

    Here's my code:

    Vliegtuigjes class
    Code:
    public class Vliegtuigjes implements Listener {
    
        private HashMap<CustomEntityMinecart, Integer> carts = new HashMap<>();
        private List<Location> locations;
        private Location center = new Location(Bukkit.getWorlds().get(0), 384, 73, 733);
        private double radius = 7.5D;
        private int amount = 50;
        private int cartAmount = 3;
        private int count = 0;
    
        public Vliegtuigjes()
        {
    
            new BukkitRunnable() {
                public void run() {
                    for (CustomEntityMinecart cart : carts.keySet()) {
                        if (cart.isAlive()) {
                            move(cart);
                        } else {
                            carts.remove(cart);
                            Bukkit.broadcastMessage("Vliegtuig crash!");
                        }
                    }
                }
            }.runTaskTimer(Voxelpark.getInstance(), 0, 10);
        }
    
        @EventHandler
        public void onThrow(PlayerEggThrowEvent e)
        {
            locations = getHorizontalCircle(center, radius, amount);
            spawnCarts();
        }
    
        public static ArrayList<Location> getHorizontalCircle(Location center, double radius, int amount)
        {
            World world = center.getWorld();
            ArrayList<Location> locs = new ArrayList<>();
            double increment = 6.283185307179586D / amount;
    
            for (int i = 0; i < amount; i++) {
                double angle = i * increment;
                double x = center.getX() + radius * Math.cos(angle);
                double z = center.getZ() + radius * Math.sin(angle);
                locs.add(new Location(world, x, center.getY(), z));
            }
            return locs;
        }
    
    
        public void spawnCarts()
        {
            int cartDistance = (int)Math.floor(this.amount / this.cartAmount);
            int cartPos = cartDistance;
    
            for (int i = 1; i <= this.cartAmount; i++) {
                CustomEntityMinecart cart = spawnCart(locations.get(cartPos));
                carts.put(cart, cartPos);
                cartPos = cartPos + cartDistance;
            }
        }
    
        public CustomEntityMinecart spawnCart(Location loc)
        {
            CraftWorld handle = (CraftWorld) Bukkit.getWorlds().get(0);
            CustomEntityMinecart cart = new CustomEntityMinecart(Bukkit.getWorlds().get(0));
            cart.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), FaceUtil.faceToYaw(BlockFace.WEST), cart.getPitch());
            handle.getHandle().addEntity(cart);
            cart.setYaw(FaceUtil.faceToYaw(BlockFace.WEST));
            cart.slowWhenEmpty = false;
    
            return cart;
        }
    
        public void move(CustomEntityMinecart cart)
        {
            Location from = locations.get(carts.get(cart));
            Location to = locations.get(carts.get(cart) + 1);
    
            cart.setDerailedVelocityMod(new Vector(1, 1, 1));
            cart.maxSpeed = 0.125;
            cart.slowWhenEmpty = false;
            cart.positionChanged = true;
    
            Vector v = to.toVector().subtract(from.toVector());
            cart.setVelocity(v);
            cart.setYaw(FaceUtil.faceToYaw(FaceUtil.getDirection(v)));
    
            if (count == amount) {
                count = 0;
            } else {
                count++;
            }
        }
    }
    
    The cart that i'm trying to spawn is a custom entity here is the code for that:
    Code:
    public class CustomEntityMinecart extends EntityMinecartRideable {
    
        private float pit = 0.0F;
        private float yaww = 0.0F;
    
        public CustomEntityMinecart(org.bukkit.World world)
        {
            super(((CraftWorld)world).getHandle());
        }
    
        @Override
        public void n()
        {
            super.n();
            this.setYawPitch(yaww, pit);
        }
    
        @Override
        public void collide(Entity entity)
        { }
    
        public void setYaw(float yaw)
        {
            this.yaww = yaw;
            this.setYawPitch(yaw, this.getPitch());
        }
    
        public float getYaw()
        {
            return yaww;
        }
    
        public void setPitch(float pitch)
        {
            this.pit = pitch;
            this.setYawPitch(this.getYaw(), pitch);
        }
    
        public float getPitch()
        {
            return pit;
        }
    
        public Location getLocation()
        {
            return this.getBukkitEntity().getLocation();
        }
    
        public void setVelocity(Vector v)
        {
            this.getBukkitEntity().setVelocity(v);
        }
    
        public Vector getVelocity()
        {
            return this.getBukkitEntity().getVelocity();
        }
    }
    And the FaceUtil class is something from BkCommonLib: click here for code
     
  2. Offline

    Zombie_Striker

    @Dmrtje
    Why not just use Math.PI *2? Using this is much cleaner than this.

    Can you try testing your minecarts on a large, flat plane to see what exactly your problem is. It could be that the increment is too small, put the minecarts in the 'wrong' spot.
     
  3. Offline

    Dmrtje

    @Zombie_Striker someone learned me to do it that way, i will change it to Math.PI * 2 and do it on a flatland

    Edit: Doing it on a flat plain it's doing the same thing :/ although what i did found out is that if there is an entity in the cart it keeps going fort and if there isn't it will stop until you go near it.
    [​IMG]

    I also changed my spawnCart method to this: so that there will be a chicken in the cart when spawned.
    Code:
    public CustomEntityMinecart spawnCart(Location loc)
        {
            CraftWorld handle = (CraftWorld) Bukkit.getWorlds().get(0);
            CustomEntityMinecart cart = new CustomEntityMinecart(Bukkit.getWorlds().get(0));
            cart.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), FaceUtil.faceToYaw(BlockFace.WEST), cart.getPitch());
            handle.getHandle().addEntity(cart);
            cart.setYaw(FaceUtil.faceToYaw(BlockFace.WEST));
            cart.slowWhenEmpty = false;
    
            Chicken chicken = (Chicken) cart.getLocation().getWorld().spawnEntity(cart.getLocation().add(0, 5, 0), EntityType.CHICKEN);
            chicken.setAgeLock(true);
            chicken.setBaby();
            chicken.setBreed(false);
            cart.getBukkitEntity().setPassenger(chicken);
    
            return cart;
        }
    Edit 2:
    also changing the runnable to run every tick instead of every 10 doesn't make a difference
     
    Last edited: Jan 30, 2016
  4. Offline

    Zombie_Striker

    @Dmrtje
    They seem to be only traveling in a straight line. Try getting a single minecart instance and printing out it's velocity. There should be a massive change in the velocity over time if the minecarts wold be going in a circle.
     
  5. Offline

    Dmrtje

    @Zombie_Striker I tried making a field in my CustomEntityMinecart class called cartPos, then i changed the carts map to a list and used the cartPos int from the CustomEntityMinecart class to check at what position it was, no luck.
    then i tried printing the size of the carts list and it gave me some weird results:
    [​IMG]
    It's continuely printing 3 and then a zero, so what that says is the one moment the list is empty and the other moment there are 3 carts in it... is Java doing weird or is it just me?
     
  6. Offline

    Zombie_Striker

    @Dmrtje
    Do you ever edit that list at any point? Do you add/remove anything from it at any point?

    It seems to be following the pattern "3300-3300-3300". Are there any repeating tasks or things that happen at specific intervals that may edit the array/minecarts?
     
  7. Offline

    Dmrtje

    @Zombie_Striker Count's editing an object inside the list too? If so yes i do in my move method i set the cartPos field inside the CustomEntityMinecart to the carts position, and my move method is inside a Bukkit Runnable that runs every 10 ticks, I also check in the runnable if minecarts are still alive so i won't get a Exception if they die i remove them from the list, but other than that i don't think i'm editing the list.

    hmm since the timer is running from the start of the plugin and the carts are spawned when i throw an egg i edit the list at that moment, but other than that no.
     
  8. Offline

    boomboompower

    @Zombie_Striker
    As you can see from that image it doesn't keep going for "3300-3300-3300". Look at the bottom (Just thought would point this out) :p
     
    Zombie_Striker likes this.
  9. Offline

    Dmrtje

Thread Status:
Not open for further replies.

Share This Page