Making a derailed minecart able to go uphill

Discussion in 'Plugin Development' started by Locercus, Jul 30, 2014.

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

    Locercus

    Hi!
    I have this setup here (code below) that makes a RideableMinecart go in the direction the rider is looking. Now this all works great when the minecarts is on a flat piece of land, however the second it hits a block, it stops. I'd like it to go up the block. Now I've looked into stuff like VehicleBlockCollision but it just turned into a bumpy ride and I couldn't get it to drive up the hill properly. Most of the hills are made of half slabs, if that helps.

    The below code is run every tick that the player hold down rightclick (from PlayerInteractEvent).
    The only exception is the velocityMod bit, as that is from VehicleEnterEvent.

    Code:java
    1. Vector playerVector = player.getLocation().getDirection();
    2.  
    3. double x = Math.round(playerVector.getX());
    4. double z = Math.round(playerVector.getZ());
    5.  
    6. Vector velocity = new Vector(x, 0, z);
    7.  
    8. Vector velocityMod = new Vector(1, 0, 1);
    9.  
    10. vehicle.setDerailedVelocityMod(velocityMod);
    11. vehicle.setVelocity(velocity);


    The reason I am doing Math.round(x) on the player's location is so that the movement is more smooth than a wibbly wobbly ride. Try stripping out that bit and you'll see what I mean.

    I am aware that I'm setting the derailed Y modifier to 0, but I'm sure we can work that out! :) I didn't have it set to 0 while I was testing the uphill stuff.

    Thank you so much in advance!

    EDIT: I just had an idea! EntityHorse handles this exact problem very well! Does anyone have the source code for that bit, so we could maybe port it to minecarts? :p
     
  2. Offline

    Totom3

    I though about a way to achieve what you want :

    1. Test if the x or the z velocity of the minecart is 0. (there's a block in his way if it is the case)
    2. If the x velocity is 0, you test if the relative block at (1/-1, 0, 0) is a slab or a full block. Do the same for the z axis but with (0, 0, 1/-1)
    3. If it is a slab, you tp the minecart 0.5 block above. If it is a full block you tp it 1 block above.

    You might want to consider "tp'ing" it a bit higher in order to make sure the bug in which the entity gets stuck in the block doesn't occurs. Also you have to test everything before you set the velocity.
     
Thread Status:
Not open for further replies.

Share This Page