Jumping Physics

Started by
1 comment, last by Dawoodoz 12 years, 6 months ago
Hello,

I am currently working on a 2D platformer. I am trying to make jumping easier to control. What I would like is something where is you hold the jump button down longer, you jump higher. Like most 2D platformers, like Mario, have.

The problem with this is that I cant get it to look realistic. What I am doing now is I have a MAX_TIME that you can hold jump for. Then during this time period or until the jump button is released an upward acceleration is applied to the sprite. But this looks like it is floating up rather than jumping.

Here is my code so far:


JumpTime -= gameTime.ElapsedGameTime.Milliseconds;
if (JumpTime <= 0)
{
EndJump();
}

if (IsJumping)
Velocity.Y += -8 * time;

if (CurrentObjectState == ObjectState.Air)
{
Position += Velocity * time + physicsManager.GravityOverTwo * time * time;
Velocity += physicsManager.Gravity * time;
}
else
{
Position += Velocity * time;
}



Does anyone have any advice on this? Or could someone point me to a good tutorial? Every tutorial I have found just has a fixed jump that isn't affected by how long the button is held down.
Advertisement
Start with a decent upward velocity.
Each frame subtract gravity from velocity.
If the jump button is held for that frame and the frame is below a certain fixed number, add some upward velocity (a fixed amount, not an acceleration) to counter-act its gravitational pull.
Add velocity to position.

This should work.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You might also need midpoint sampling for collisions if things start to shake and bounce too much. Just approximate where the item would be after one half timestep and sample the pressures from there.

This topic is closed to new replies.

Advertisement