Saturday, December 2, 2017

c# - How to incorporate delta time into movement?



I want to use delta time in my movement code for my player, and I'm not sure how to do it. At the moment, I have this:


else if (keyboardState.IsKeyDown(Keys.D))
{
double moveSpeed = 0.3 * delta;

if (velocity < 0)
{
double turnSpeed = 0.5 * delta;
velocity += turnSpeed;
}

else
{
if (velocity + moveSpeed > .1)
{
velocity = .1;
}
else
{
velocity += moveSpeed;
}

}
}

position.X += (float)velocity;

Is this correct? I'm trying to make my player's movement gradually speed up until it hits the maximum velocity.


position.X is the Vector2 X of the player. delta is set to gameTime.ElapsedGameTime.TotalSeconds.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...