2D platformer gravity physics with slow-motion
- by DD
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;
}