Here’s a post that I responded to from someone asking about this, but with jumping specifically:
To tweak this a bit for strafe acceleration:
//class vars
float intendedStrafeDirection;
float acceleration;
float maxStrafeVelocity;
// Input
//thumbstick
intendedStrafeDirection = CurrGamePadState.ThumbSticks.Left.X;
//or dpad
if (CurrGamePadState.IsButtonDown(Buttons.DPadLeft)) { intendedStrafeDirection = -1; }
elseif (CurrGamePadState.IsButtonDown(Buttons.DPadRight) { intendedStrafeDirection = 1; }
else { intendedStrafeDirection = 0; }
// Update movement
float deltaTime = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;
float prevVelocityX = velocity.X;
velocity.X += intendedStrafeDirection * acceleration * deltaTime;
if (Math.Abs(velocity.X) > maxStrafeVelocity) { velocity.X = Math.Sign(velocity.X) * maxStrafeVelocity; }
//verlet integration
position.X += (prevVelocityX + velocity.X) * 0.5f * deltaTime;
In essence, use gameTime code (deltaTime) when things change over time in your world:
Velocity adding onto position → use deltaTime
Acceleration affecting velocity → use deltaTime
Friction affecting velocity → use deltaTime
Setting velocity to a constant → DON’T use deltaTime (I explain next)
If you look at how I implement jump in the link, I set the velocity as such:
velocity.Y = jump.verticalRate; //no deltaTime
position.Y += (prevVelocityY + velocity.Y) * 0.5f * deltaTime;
When I set the value to a constant rate, I don’t need deltaTime. However when I add the velocity onto position, I still need to use deltaTime.
Another thing to note is some things don’t need deltaTime, such as mouse movement over time. Say for example, you create a camera that pans or rotates when you move the mouse. In this case, you don’t use deltaTime:
Vector2 mouseDelta = new Vector2(mousePos.X - (Graphics.GraphicsDevice.Viewport.Width / 2),
mousePos.Y - (Graphics.GraphicsDevice.Viewport.Height / 2));
mouseDelta.X /= Graphics.GraphicsDevice.Viewport.Width;
mouseDelta.Y /= Graphics.GraphicsDevice.Viewport.Height;
camera.Position += mouseDelta * panSpeed;
Notice how this doesn’t require deltaTime. I’ll link to the reason I was given back when I didn’t know about this: