Delta time not working properly

I’m trying to make my game use delta time so it becomes independent of FPS. I had a go at doing it and compared them side-by-side:

But the player on the 144fps one still goes faster. My code:

https://pastebin.com/AHvUUdhE

What did I do wrong?

P.S: I’m positive it’s not just my eyes playing with me. I can see the player going faster on 144fps than the one on 60fps.

Short answer.

 // This is delta time to keep things straight for movement.
 elapsedThisFrame = (float)(gameTime.ElapsedGameTime.TotalSeconds);

It’s easier to test by just turning off fixed time stamp all together and run at mega speed then turning it back on.

Alternately you can do the following.

now = gameTime.TotalGameTime.TotalSeconds;
elapsed = (double)(now - last);
last = now;

Similar to what the fps class below is doing but cutting out the message alarm trigger.

    public class BasicFps
    {
        private double frames = 0;
        private double updates = 0;
        private double elapsed = 0;
        private double last = 0;
        private double now = 0;
        public double msgFrequency = 1.0f;
        public string msg = "";

        /// <summary>
        /// The msgFrequency here is the reporting time to update the message.
        /// </summary>
        public void Update(GameTime gameTime)
        {
            // I do this because i usually want to get the time now.
            // As well as count the updates draws ect.
            now = gameTime.TotalGameTime.TotalSeconds;
            elapsed = (double)(now - last);
            if (elapsed > msgFrequency)
            {
                msg = " Fps: " + (frames / elapsed).ToString() + "\n Elapsed time: " + elapsed.ToString() + "\n Updates: " + updates.ToString() + "\n Frames: " + frames.ToString();
                elapsed = 0;
                frames = 0;
                updates = 0;
                last = now;
            }
            updates++;
        }

        public void DrawFps(SpriteBatch spriteBatch, SpriteFont font, Vector2 fpsDisplayPosition, Color fpsTextColor)
        {
            spriteBatch.DrawString(font, msg, fpsDisplayPosition, fpsTextColor);
            frames++;
        }
    }

eg

playerPosition += playersUnitLengthMoveDirection * playerSpeed * elapsedThisFrame ;

// with the above it doesn’t matter if you are going at 1000 fps or 10.
// you move the same distance in a second.

So should I multiply delta only when I am applying the speed and not in the calculations?

Define calculations.
(can’t see what your engines calculations are internally from that code)

Speed is a scalar for directional acceleration, so is the delta. Normally you can multiply the speed by the delta by the direction of motion to alter your players position.

If you are using some acceleration to change your speed like to ramp it up over time.
Then you could apply it to that instead.

As shown in my code, I use velocity to gradually make the character go faster until it reaches a maximum. I check if adding the velocity would make it go above the maximum. Would I use delta in that check?

https://pastebin.com/AHvUUdhE3
?

This is updated code because I’ve been working on this for a while now.

else if (keyboardState.IsKeyDown(Keys.D))
            {
                double moveSpeed = 5;

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

            position.X += (float)(velocity * delta);
            Console.WriteLine(position.X);

You should probably multiply the moveSpeed by the delta as well.

Just think about someone running it at 1000 fps
how fast is he going to get to 100 velocity vs a guy running at 10 fps ?

The position will of course be fine like that as delta on it makes it velocity per second, not per frame.

Tried something like this, but it didn’t work:

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

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

        position.X += (float)velocity;

The player speed varies with 60 (fixed timestep) and 4000 (unfixed timestep).

if (keyboardState.IsKeyDown(Keys.D))
{
    double moveSpeed = 0.03d;
    velocity += moveSpeed  * delta;
    if (velocity > 1)
        velocity = 1;
    position.X += (float)velocity  * delta;
}

if (keyboardState.IsKeyDown(Keys.A))
{
    double moveSpeed = -0.03d;
    velocity += moveSpeed  * delta;
    if (velocity < -1)
        velocity = -1;
    position.X += (float)velocity  * delta;
}

try that.

Thank god. It’s finally showing some good results. Thanks a ton.

New code:

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

            if (velocity < 0)
            {
                double turnSpeed = 1 * delta;
                velocity += turnSpeed;
            }
            else
            {
                if (velocity + moveSpeed > 200)
                {
                    velocity = 200;
                }
                else
                {
                    velocity += moveSpeed;
                }
            }
        }

        position.X += (float)(velocity * delta);

Yeah, I know right about the speed and max velocity, but it seems to work normally, both on 60fps and 4000fps.

Yep

but isn’t it kind of weird to turn only if velocity is below zero
As well not be able to accelerate if its below zero ?
You should probably keep your turn logic and acceleration logic seperated.

Basically what I had there was if you’re pressing a button to go one way, but you’re currently going the other, it speeds up the velocity a bit to make a faster turn. I removed that because the speed boost was very visible on higher fps, and wasn’t really necessary anyway.