Matthew
Matthew

Reputation: 867

Entity movement with vectors

I am developing a game for the game "Minecraft" where I must model a battering ram arm "hitting" a gate, swinging back from recoil, and then returning to its original position. If you are not aware of what a battering ram is, watch this video.

My first issue is I cannot model this movement in the way that I want. When I apply a new Vector to an entities velocity, the entity starts out at maximum acceleration. Rather than building up acceleration over time, say, from 0 -> 100mph over 10 seconds, the entities speed starts at 100mph and immediately begins decelerating.

My second issue is I cannot change the direction in which the entity is moving in a way that makes sense. For example, if I run the following code:

        stand.setVelocity(new Vector(0, 0, -.25));
        stand.getVelocity().add(new Vector(0, 0, 1));

Minecraft will not acknowledge the second vector. If instead I use the following code:

        stand.setVelocity(new Vector(0, 0, -.25));
        stand.setVelocity(new Vector(0, 0, 1));

Minecraft will "overwrite" the first vector, obviously, with the new velocity, and not moving the entity in the first direction.

I want to increase the speed of the entity over time, and then once a certain speed is reached, reduce speed until at a stop (while still moving in the same direction). Once at 0 movement (a complete stop), apply another vector to move the entity in the opposite direction (just how the battering ram does in the video).

Is there a best practice how to handle such a situation? Are there any Minecraft-specific solutions?

Upvotes: 0

Views: 771

Answers (1)

user13241190
user13241190

Reputation:

In the first example you use .getVelocity which just returns the velocity you cant change it with that. If you want it to slowly accelerate use some kind of loop like:

int velocity = 0.25;

loop {
   velocity = velocity + 0.01;
   stand.setVelocity(new Vector(0, 0, velocity)); 
}

That would accelerate the loop at your wanting speed just change how much it adds to it and then you can add a check that if the velocity is the maximum and then stop it like you want.

If you want to change the direction it moves just change the velocity to like -x or -z or +x or +z. depending on your situation.

Upvotes: 2

Related Questions