Minecart's direction is off

Discussion in 'Spigot Plugin Development' started by DucPro, Apr 25, 2019.

  1. I have made a plugin to run a minecart on a rail tracks at a constant speed. However, I encountered this problem while doing a few tests:
    Firstly, here's my code for moving the minecart.
    Code (Java):
    Block nxt = ginfo.getNxt(world.getBlockAt(cart.getLocation()));
                    if (nxt == null) return;
                    Block cur = world.getBlockAt(cart.getLocation());
                    double velox = 0, veloz = 0, veloy = 0;
                    if (nxt.getX() > cur.getX()) velox = 0.1;
                    if (nxt.getX() < cur.getX()) velox = -0.1;
                    if (nxt.getZ() > cur.getZ()) veloz = 0.1;
                    if (nxt.getZ() < cur.getZ()) veloz = -0.1;
                    if (nxt.getY() > cur.getY()) veloy = 0.1;
                    if (nxt.getY() < cur.getY()) veloy = -0.1;
                    cart.setVelocity(new Vector(velox, veloy, veloz));
    getNxt is basically the block the cart will move to after this current one.
    Here's the minecart normally:
    https://ibb.co/DkqN6Kf
    However, after going up, the minecart sways a bit to left.
    https://ibb.co/87K2bj2
    https://ibb.co/gDZTrxW
    Yes, the minecart stays in that direction throughout the whole straight path in the second picture.
    I don't know what could be causing this. Thanks for any help.
     
  2. Sorry I have to bump again. I haven't been able to figure out the cause for this yet
     
  3. Try posting a video of your issue, can't really see the issue from your pictures tbh.
     
  4. I'm assuming instead of facing the correct direction it stays at a slanted angle or whatever
     
  5. I'll do it when i get back into my PC. But yes after going up it stays a slanted angle and corrects itself at random positions. The position it corrects itself differs everytime I run it. A video will be posted in an hour
     
  6. Wouldn't it be better to get the direction of a block relative to where the next rail is located? You can check against the location from where the vehicle last moved from in comparison to where the vehicle is moving too with the VehicleMoveEvent.

    Edit:

    You can also get the current velocity and multiply it simply with the VehicleMoveEvent. Make sure to check for the location that you are moving to and from to avoid inefficiency.

    Code (Text):
    Vehicle vehicle = event.getVehicle();

    if(event.getTo() != event.getFrom()) {
        vehicle.setVelocity(vehicle.getVelocity().multiply(2));
    }
    Having the Velocity at 1.1 works perfectly fine.
     
    #7 StefTheDev, Apr 27, 2019
    Last edited: Apr 27, 2019
  7. 1.1 would be too fast, but I'll try out this idea. Thanks for your help
    For everyone else, here's a video example of 2 clips showcasing the minecart slanting to a side before correcting itself
     
  8. Bump again since I still don't have a single idea on why the minecart slants a bit to the left after going up, but not down.
     

Share This Page