Solved [HELP] Can't use setVelocity at the same time!

Discussion in 'Spigot Plugin Development' started by Gober, Jun 28, 2018.

Thread Status:
Not open for further replies.
  1. Hello there!
    first of all, I want to apologize for the Tons of Questions I've been asking on Spigot today.
    I'm being working on a plugin right now.
    I notice that you couldn't have Two entity#setVelocity,
    It will ignore the second one and instead just runs the first one.
    Example:
    Code (Java):

    public void Push(Entity e, Location loc, Double speed) {
            Vector vector = loc.getDirection();
            e.setVelocity(vector.normalize().multiply(speed)); // This works
            e.setVelocity(vector.normalize().multiply((speed)); // But this wont work
    /*
     NOTE: Changing the Values will still be the same
    */

     
     
  2. Benz56

    Moderator Supporter

    You have a “(“ too much in the second statement.

    However, what are you trying to achieve?
    Just do: multiply(speed * 2)

    I am sure it works just fine it is simply doubling the velocity if you multiply two times in a row.
     
    • Like Like x 1
  3. Why would you even wanna do that?
     
    • Funny Funny x 1
  4. Gawd, You where quick. Almost won
     
  5. Just an example xD.
    The first one will work but the second wont work because there's the first one already.
    Just try to change the value of the second one, it should not work.
     
  6. Okay! But i think Benz56 answered your question so xD
     
  7. electronicboy

    IRC Staff

    setVelocity just sets how fast the entity is going to move when it's next ticked (assuming that something else doesn't change that elsewhere), you're calling setVelocity twice with the same values, and so, it's going to have no effect whatsoever (as that velocity is already set)
     
    • Agree Agree x 1
    • Winner Winner x 1
  8. now im wondering for a possible way
     
  9. What? :) Please explain what your end goal is? :D
     
  10. I want to apply 2 setVelocity's
     
  11. Why? What do you expect to happen?
     
  12. Benz56

    Moderator Supporter

    That doesn’t make sense.

    If you want more velocity then multiply by a larger value?
     
  13. Like:
    - Pushing entity Upwards
    - Pushing entity Leftwards
     
  14. I'm making a custom wasd movement for entities.
    But sadly I could only move forward when I pressed Forward Key (W) and Right Key (D) or any other key.
     
  15. The actual answer to your problem:
    What you want is to specify a velocity in both the direction the player is looking in (W and S keys) and to set the velocity in the direction perpendicular (90 degrees) to the direction the player is looking at (A and D keys). When setting the velocity, you can only set the values of one vector, so calling that method multiple times, will just override the first few calls. What you actually have to do is to calculate one vector that contains both values. We can simply add vectors together to combine them, so that simplifies it. We need one vector to describe the forward (or negative forward, backwards) direction and one vector to describe the left (or negative left, right) direction. The first vector, the forward vector is easy.
    Code (Text):
    vec3 direction = new vec3(player.getDirection().x, 0, player.getDirection().z).normalize();
    vec3 forward = new vec3(d).multiply(speedForward);
    First of all, we remove the y-direction! Why? Well, when making player movement, we almost always make special buttons for movement in the Y direction. Minecraft for example, uses space and shift. That is why we want to ignore the direction the player is looking at on the Y-axis. After this we have to normalize the player direction vector. If we would not do this, you get unexpected results due to wrong multiplications. Then, we copy the direction vector, because we will also need the direction vector to calculate the left vector. Then we simply multiply the vector with the speed. This means that with a speed of one, due to the normalization of the direction vector, the player will go 1 unit forward in the direction he/she is looking at. That is how we calculate the forward vector. The left vector is a little bit more complicated as we have to get the direction to the left of the player's looking direction. Luckily for us, rotating 90 degrees for a 2D vector is very easy. But wait, we are working with a 3D vector, aren't we? No, as told in the first part, we remove the Y direction. Actually, we can ignore that vector completely. A more detailed explanation of how a 90 degrees rotation works can be found here: https://stackoverflow.com/questions/4780119/2d-euclidean-vector-rotations! After we rotated the vector, we can do exactly the same as we did with the forward vector:
    Code (Text):
    vec3 direction = new vec3(player.getDirection().x, 0, player.getDirection().z).normalize();
    vec3 forward = new vec3(d).multiply(speedForward);
    vec3 left = new vec3(-d.z, 0, d.x).multiply(speedLeft);
    As said in the beginning, to combine them, simply add them together. Then this vector can be used in the setVelocity method:
    Code (Text):
    vec3 direction = new vec3(player.getDirection().x, 0, player.getDirection().z).normalize();
    vec3 forward = new vec3(d).multiply(speedForward);
    vec3 left = new vec3(-d.z, 0, d.x).multiply(speedLeft);
    vec3 speed = forward.add(left);
    player.setVelocity(speed);
    Important
    - This is probably not valid java code, and for a reason. By not making it perfect, it will make sure you will have to take a good look at it, good enough to understand it.
    - When rotating the vector, I am negating the new value of the x-axis. This may be the wrong value. If you get the opposite direction you are supposed to walk in, remove the negative sign from that side and put it at the size of the z-axis:
    Code (Text):
    vec3 left = new vec3(d.z, 0, -d.x).multiply(speedLeft);
    The location of the negative sign specifies what rotation it makes: a rotation to the left or a rotation to the right!

    A funny quantum joke xD
    The problem with your code is that you try to have specific information on both the velocity and the position. The uncertainty principle, a quantum mechanics theory by Heisenberg, proofs that this is physically impossible. There is no way for us to know both the position and momentum at the same time. For those who do not know what momentum is, it is a way to describe velocity for a specific mass. So, you have to skip a tick before actually changing the velocity. (DO NOT DO THIS, THIS IS A JOKE)
     
  16. We may have confused the poor guy. Take a read through this, and see if you can't merge the info together. I'm about to shower then sleep, or else I would.
     
Thread Status:
Not open for further replies.