Page 1 of 1

Applying Linear Velocity Seems to Ignore Gravity

Posted: Wed Jan 02, 2013 12:55 am
by matt33315
I have a box in my game that I want to move when the player presses a key. ie A key

This was very simple to implement by using the LinearVelocity property on the Entity. (I have set this using the forward vector of the object)

So now when the user presses the A key the block moves forward... So far so good :)

However, I have found that if the object goes off a ledge and I keep the A key pressed down the box does not fall at the same rate as when the A button is released.

I'm guessing because I am just using the forward vector of the object as the LinearVelocity and this is not taking into account gravity and once I release A the liner velocity is calculated automatically by BEPU and gravity is taken into account.

Is there a way I can continuously apply a force to the block but make it so it will always react to gravity?

Or is there a way I can access the gravity value so I could use this when calculating the linear velocity?

Thanks

Matt

Re: Applying Linear Velocity Seems to Ignore Gravity

Posted: Wed Jan 02, 2013 1:42 am
by Norbo
Instead of overwriting the current velocity, add the difference between the current speed and desired speed along the axes it controls.

So, if you want your character to move at some desiredMoveSpeed in some normalized movementDirection, measure the current move speed along the direction and add the difference:

Code: Select all

float currentMoveSpeed = Vector3.Dot(movementDirection, entity.LinearVelocity);
entity.LinearVelocity += movementDirection * (desiredMoveSpeed - currentMoveSpeed);
The key thing to note here is that the velocity is only being changed along the movementDirection. As long as it's pointing in a reasonable direction, it won't interfere with gravity.

You can make it fancier by limiting the acceleration rate and such.