Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D platformer gravity physics with slow-motion

Started by Dynx Jun 18, 2010 at 12:47 AM 1 replies 2.3k views
Original Post
Dynx
Dynx
Hi all,

I fine tuned my 2d platformer physics and when I added slow-motion I realized that it is messed up.

The problem I have is that for some reason the physics still depends on framerate. So when I scale down time elapsed, every force is scaled down as well. So the jump force is scaled down, meaning in slow-motion, character jumps vertically smaller height and gravity force is scaled down as well so the character goes further in the air without falling.

I'm sending update function in hopes that someone can help me out here (I separated vertical (jump, gravity) and walking (arbitrary walking direction on a platform - platforms can be of any angle) vectors):

characterUpdate:(float)dt{    //Compute walking velocity    walkingAcceleration     = direction of platform * walking acceleration constant * dt;    initialWalkingVelocity  = walkingVelocity;    if( isWalking )    {        if( !isJumping )            walkingVelocity = walkingVelocity + walkingAcceleration;        else             walkingVelocity = walkingVelocity + Vector( walking acceleration constant * dt, 0 );    }    // Compute jump/fall velocity    if( !isOnPlatform )    {        initialVerticalVelocity = verticalVelocity;        verticalVelocity        = verticalVelocity + verticalAcceleration * dt;    }    // Add walking velocity    position = position + ( walkingVelocity + initialWalkingVelocity ) * 0.5 * dt;    //Add jump/fall velocity if not on a platform    if( !isOnPlatform )        position = position + ( verticalVelocity + initialVerticalVelocity ) * 0.5 * dt;    verticalAcceleration.y = Gravity * dt;
Alrecenk
Alrecenk
Accelerations should not be affected by dt and should be the same at different time-steps. They are only multiplied by dt when integrating to get velocity, and velocity is only multiplied by dt when integrating to get position. I haven't been terribly thorough in looking at the code since it's almost 4AM here, but the behavior you describe and that first line of code where acceleration is based on dt suggests that is probably the problem. I'd replace any dt's in your acceleration terms with whatever your dt was before you changed it, so you don't have to retweak everything.
Dynx
Dynx
Yes that was it, thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.