Applying deltaTime to platformer

Sorry if this is isn’t the proper category to post this in.

I am trying to apply deltaTime to the physics of the player object in my current platforming game project. I need to figure out how to properly apply deltaTime to the player’s walking and jumping velocity.

I am using simple math for the calculations. I am not using the Corona API physics.

The value of deltaTime in this case is from this tutorial.

I have researched numerous threads and articles on Corona Forum and elsewhere, but nothing I have tried so far has worked properly.

Simplified code without deltaTime:

[lua]

– master object class

Next Position X = Map Position X + Velocity X
Next Position Y = Map Position Y + Velocity Y

– player class

if( Analog Stick == Left )then

    – increase walk speed left
    Velocity X = Velocity X + Walk Speed * -1

        – cap velocity at max speed left
        if( Velocity X < Max Walk Speed *-1 )then
            Velocity X = Max Walk Speed * -1
        end

elseif( Analog Stick == Right )then

    – increase walk speed right
    Velocity X = Velocity X + Walk Speed

        – cap velocity at max speed right
        if( Velocity X > Max Walk Speed )then
            Velocity X = Max Walk Speed
        end

else

    if( Velocity X > 0 )then

            – decrease walk speed left
            Velocity X = Velocity X + ( Friction * -1 )
            if(Velocity X <= 0)then
                Velocity X = 0
            end

      elseif( Velocity X < 0 )then

            – decrease walk speed right
            Velocity X = Velocity X + Friction
                if( Velocity X > 0 )then
                    Velocity X = 0
                end

        end

 end

if( Analog Stick == Up and Fall == 0 )then

    – apply jump speed
    Fall = 1
    Velocity Y = Jump Speed * -1
end

– apply gravity
Velocity Y =  Velocity Y + Gravity

end

[/lua]

[Update]

So far I have applied deltatime to walking movement, and it appears to work properly in the simulator and on device.

Applying deltatime to the jumping calculation is still giving me trouble. Specifically, both in the simulator and on the device. Each time the player jumps, their jump velocity is inconsistent. They jump high, medium, or low erratically. This is because deltaTime is different each frame and causing the inconsistency.

[lua]

Velocity Y = Jump Speed * deltaTime

[/lua]

I’m guessing I need to figure the proper calculation to stabilize the velocity. I tried a bunch of things I found while researching this but nothing has worked so far. I will continue to work on it and try to figure it out.

Simplified code with deltatime applied

[lua]

– master object class

Next Position X = Map Position X + Velocity X
Next Position Y = Map Position Y + Velocity Y

– player class

if( Analog Stick == Left )then

    – increase walk speed left
    Velocity X = ( Velocity X + Walk Speed * -1 ) * deltaTime

        – cap velocity at max speed left
        if( Velocity X < ( Max Walk Speed *-1 ) * deltaTime )then
            Velocity X = (Max Walk Speed * -1) * deltaTime
        end

elseif( Analog Stick == Right )then

    – increase walk speed right
    Velocity X = ( Velocity X + Walk Speed ) * deltaTime

        – cap velocity at max speed right
        if( Velocity X > ( Max Walk Speed ) * deltaTime )then
            Velocity X = ( Max Walk Speed ) * deltaTime
        end

else

    if( Velocity X > 0 )then

            – decrease walk speed left
            Velocity X = Velocity X + ( ( Friction * -1 ) * deltaTime )
            if(Velocity X <= 0)then
                Velocity X = 0
            end

      elseif( Velocity X < 0 )then

            – decrease walk speed right
            Velocity X = Velocity X + ( Friction * deltaTime )
                if( Velocity X > 0 )then
                    Velocity X = 0
                end

        end

 end

if( Analog Stick == Up and Fall == 0 )then

    – apply jump speed
    Fall = 1
    Velocity Y = ( Jump Speed * -1 ) * deltaTime
end

– apply gravity

if( Fall == 1 )then
    Velocity Y =  Velocity Y + ( Gravity * deltaTime )

end

end

[/lua]

I was able to find the answer to my problem.

Before I was trying to apply delta time like:

[lua]

– initiate jump

if( Jump == true and Fall == 0 )then

     Fall = 1

     Velocity Y = ( Jump Speed * -1 ) * deltaTime

end

– apply gravity right after jump is applied

if(Fall == 1 )then

     Velocity Y = Velocity Y + ( Gravity * deltaTime )

end

[/lua]

After researching further, I was able to figure out the proper method. Instead of applying delta time in whole, you need to:

  1. Add jump speed to the velocity when the player is jumping
  2. After the player has jumped, apply half of gravity x deltaTIme to the velocity
  3. Multiply Velocity by deltaTime
  4. Add full gravity x deltaTime to velocity

So now the code will be:

[lua]

– initiate jump

if( Jump == true and Fall == 0 )then

     Fall = 1

     Velocity Y = ( Jump Speed * -1 ) – only apply the normal jump speed without dt!

end

– apply gravity right after jump is applied

if(Fall == 1 )then

     Velocity Y = deltaTime * ( Velocity Y + ( ( Gravity * deltaTime ) * 0.5)  ) – velocity + half gravity and then multiply that by dt

     Velocity Y = Velocity Y + ( Gravity * deltaTime ) – apply the rest (full) gravity * dt

end

[/lua]

I tested this code on my device and it worked properly. I am currently trying to use this technique for the Velocity X movement as well. Once I have finished implementing the code and can confirm it works properly, I will make one last post with final code and mark the thread as solved.

Credit goes to this tutorial for helping me figure it out.

I was able to successfully implement delta time to the horizontal velocity. Now delta time is applied to both x and y velocities. The player runs and jump at the correct rate, and consistently on devices when the frame rate is lower than the set amount.

However there is now a side effect.

The config frame rate is set at 60 fps. If the actual frame rate drops low enough, delta time becomes such a large number that the player object’s difference in position between frames is large enough to cause the player to not register with tiles and collectible objects. It is possible to jump and glitch down through a single row of ground tiles and to not pick up gold pieces that are in your path when falling.

This problem occurs on the device, and can be simulated in the simulator by printing the screen which causes the frame rate to drop. The value of delta time rises to about 7 when printing the screen in the simulator.

I am still looking into how to fix this. This occurs both when using the tutorial value for delta time, and the plugin value for delta time.

[Update]

So far I have applied deltatime to walking movement, and it appears to work properly in the simulator and on device.

Applying deltatime to the jumping calculation is still giving me trouble. Specifically, both in the simulator and on the device. Each time the player jumps, their jump velocity is inconsistent. They jump high, medium, or low erratically. This is because deltaTime is different each frame and causing the inconsistency.

[lua]

Velocity Y = Jump Speed * deltaTime

[/lua]

I’m guessing I need to figure the proper calculation to stabilize the velocity. I tried a bunch of things I found while researching this but nothing has worked so far. I will continue to work on it and try to figure it out.

Simplified code with deltatime applied

[lua]

– master object class

Next Position X = Map Position X + Velocity X
Next Position Y = Map Position Y + Velocity Y

– player class

if( Analog Stick == Left )then

    – increase walk speed left
    Velocity X = ( Velocity X + Walk Speed * -1 ) * deltaTime

        – cap velocity at max speed left
        if( Velocity X < ( Max Walk Speed *-1 ) * deltaTime )then
            Velocity X = (Max Walk Speed * -1) * deltaTime
        end

elseif( Analog Stick == Right )then

    – increase walk speed right
    Velocity X = ( Velocity X + Walk Speed ) * deltaTime

        – cap velocity at max speed right
        if( Velocity X > ( Max Walk Speed ) * deltaTime )then
            Velocity X = ( Max Walk Speed ) * deltaTime
        end

else

    if( Velocity X > 0 )then

            – decrease walk speed left
            Velocity X = Velocity X + ( ( Friction * -1 ) * deltaTime )
            if(Velocity X <= 0)then
                Velocity X = 0
            end

      elseif( Velocity X < 0 )then

            – decrease walk speed right
            Velocity X = Velocity X + ( Friction * deltaTime )
                if( Velocity X > 0 )then
                    Velocity X = 0
                end

        end

 end

if( Analog Stick == Up and Fall == 0 )then

    – apply jump speed
    Fall = 1
    Velocity Y = ( Jump Speed * -1 ) * deltaTime
end

– apply gravity

if( Fall == 1 )then
    Velocity Y =  Velocity Y + ( Gravity * deltaTime )

end

end

[/lua]

I was able to find the answer to my problem.

Before I was trying to apply delta time like:

[lua]

– initiate jump

if( Jump == true and Fall == 0 )then

     Fall = 1

     Velocity Y = ( Jump Speed * -1 ) * deltaTime

end

– apply gravity right after jump is applied

if(Fall == 1 )then

     Velocity Y = Velocity Y + ( Gravity * deltaTime )

end

[/lua]

After researching further, I was able to figure out the proper method. Instead of applying delta time in whole, you need to:

  1. Add jump speed to the velocity when the player is jumping
  2. After the player has jumped, apply half of gravity x deltaTIme to the velocity
  3. Multiply Velocity by deltaTime
  4. Add full gravity x deltaTime to velocity

So now the code will be:

[lua]

– initiate jump

if( Jump == true and Fall == 0 )then

     Fall = 1

     Velocity Y = ( Jump Speed * -1 ) – only apply the normal jump speed without dt!

end

– apply gravity right after jump is applied

if(Fall == 1 )then

     Velocity Y = deltaTime * ( Velocity Y + ( ( Gravity * deltaTime ) * 0.5)  ) – velocity + half gravity and then multiply that by dt

     Velocity Y = Velocity Y + ( Gravity * deltaTime ) – apply the rest (full) gravity * dt

end

[/lua]

I tested this code on my device and it worked properly. I am currently trying to use this technique for the Velocity X movement as well. Once I have finished implementing the code and can confirm it works properly, I will make one last post with final code and mark the thread as solved.

Credit goes to this tutorial for helping me figure it out.

I was able to successfully implement delta time to the horizontal velocity. Now delta time is applied to both x and y velocities. The player runs and jump at the correct rate, and consistently on devices when the frame rate is lower than the set amount.

However there is now a side effect.

The config frame rate is set at 60 fps. If the actual frame rate drops low enough, delta time becomes such a large number that the player object’s difference in position between frames is large enough to cause the player to not register with tiles and collectible objects. It is possible to jump and glitch down through a single row of ground tiles and to not pick up gold pieces that are in your path when falling.

This problem occurs on the device, and can be simulated in the simulator by printing the screen which causes the frame rate to drop. The value of delta time rises to about 7 when printing the screen in the simulator.

I am still looking into how to fix this. This occurs both when using the tutorial value for delta time, and the plugin value for delta time.