Hi there, I am trying to make a jumping movement with delta time (compatible with any framerate). Unfortunately, I am struggling to accomplish this. The original code that is not compatible with dt is below:
-- jumping
if player.jumping then
-- prevents legs from moving whilst jumping
if player.frame ~= 0 or player.aTimer ~= 0 then
player.frame = 0
player.aTimer = 0
legsQuad:setViewport(60*player.frame,player.legY,60,15,legs:getDimensions())
end
if not player.falling then
if player.acceleration >= 15 then
player.falling = true
else
player.y = player.y - (15 - player.acceleration)
player.acceleration = player.acceleration + 1
end
if not player.soundPlayed then
playSound("jump")
player.soundPlayed = true
end
end
end
Any help or direction to making this code dt-compatible would be appreciated.
There is a difference between velocity and acceleration. Generally speaking the formula is
change in position = velocity*dt
With jumping you want to set the velocity once and let the body fall due to gravity. https://2dengine.com/?p=platformers
with an acc value between 0 and 1 (in fact, less than 0.1). The target speed is something like the current maximum speed that the player wants to reach. This value can be changed, say depending on the ground the player is standing on, or if he is currently in the air. (Do the same for the y acceleration in case you have a top down view on the player)