Solved Changing Velocity for Pushing

Discussion in 'Spigot Plugin Development' started by N1ark, May 17, 2018.

  1. Hello!
    I ran into a problem earlier today, when trying to make a propelling block, that every few seconds, will push away all near entities in the opposite direction. I came up with this:
    Code (Text):
    e.setVelocity(new Vector(e.getLocation().getX() - block.getX(), e.getLocation().getY() - block.getY(), e.getLocation().getZ() - block.getZ()).normalize().add(new Vector(0,0.1,0)));
    I do not master completely vectors, so I am quite unsure about what I'm doing, but my problem right now is that even if it seems to work, after testing it I saw that it works awkwardly, as it doesn't push exactly in the opposite direction, I really don't know how to explain it...
    If anybody knows how to make a nice formula to do it, it would be more than welcome!
  2. Let's say you have a vector called blockMiddle, which is the middle of your propelling block. You can get this vector by invoking the Location#toVector() method of the specific block's location, and adding another vector with x = 0.5, y = –0.5, z = 0.5 to it.
    There is also a vector called playerPos, which you can get with the exact same way.

    You would then substract blockMiddle from playerPos, e.g.:
    Code (Java):
    Vector direction = blockMiddle.subtract(playerPos);
    //normalize the vector -> this means it has a length of 1
    direction = direction.normalize();
    The direction vector is now the direction in which you have to accelerate the player in order to push him away from said block.
    • Useful Useful x 1
  3. Oh okay! Thank you! I will try this ^^
  4. It works! I just had to substract the block's vector to the entity's vector, and I added 0.1 to Y to make the player jump a bit too!
    Here is the result:
    Code (Text):
    e.getLocation().toVector().subtract(block.toVector().add(new Vector(0.5, -0.5, 0.5))).add(new Vector(0,0.1,0))
    Thanks and have a nice day!
    • Friendly Friendly x 1

Share This Page