Sign in to follow this  
noodlyappendage

2D Platformer Jumping Problems

Recommended Posts

I am having a little trouble getting the jumping physics right in a 2D platformer I'm working on. What I'm trying to do is set an impulse value, and then let the gravity force slow it down, and eventually reverse it, but it isn't quite working. The result I'm seeing is the player instantly appears at the peak of the jump, and then begins to fall. I'm sure I'm doing something wrong but I'm not seeing it. My update step looks like this:

update(double time)
{
   ddx_ = (forceX_ / mass_) * time;
   ddy_ = (forceY_ / mass_) * time;

   impulseX_ *= time;
   impulseY_ *= time;

   dx_ += (ddx_ + impulseX_);
   dy_ += (ddy_ + impulseY_);

   dx_ += (dx_ * linearDampX_);
   dy_ += (dy_ * linearDampY_);

   dx_ *= time;
   dy_ *= time;

   x_ += dx_;
   y_ += dy_;

   // Impulse should only be set in one update step.
   impulseX_ = 0.0;
   impulseY_ = 0.0;
}

Share this post


Link to post
Share on other sites

 

I am having a little trouble getting the jumping physics right in a 2D platformer I'm working on. What I'm trying to do is set an impulse value, and then let the gravity force slow it down, and eventually reverse it, but it isn't quite working. The result I'm seeing is the player instantly appears at the peak of the jump, and then begins to fall. I'm sure I'm doing something wrong but I'm not seeing it. My update step looks like this:

update(double time)
{
   ddx_ = (forceX_ / mass_) * time;
   ddy_ = (forceY_ / mass_) * time;

   impulseX_ *= time;
   impulseY_ *= time;

   dx_ += (ddx_ + impulseX_);
   dy_ += (ddy_ + impulseY_);

   dx_ += (dx_ * linearDampX_);
   dy_ += (dy_ * linearDampY_);

   dx_ *= time;
   dy_ *= time;

   x_ += dx_;
   y_ += dy_;

   // Impulse should only be set in one update step.
   impulseX_ = 0.0;
   impulseY_ = 0.0;
}

 

I am not sure what is about, but this look very confusing.

 

An typical update step in any game may look like this:

 

You have "velocity" (Acceleration over the entity/player lifetime - is there are no force or impulse this stays forever in that current state)

You have "force" (A force accumulator with applied forces, like gravity to achieve "jumping" - which is cleared after the update steps)

You have "position" your actual position you want to integrate to

 

Then its just a simple integration - which works for platformers very well:

 

acceleration = (force / mass)

velocity += acceleration * delta time

position += velocity * deltatime

 

If you need damping you just "damp" the velocity like this - before applying to the position:

velocity *= 0.99

 

Of course there are other ways to do damping - like for example in a particle simulation, i use this: 

velocity *= pow(0.99, delta time)

which works better (depends on the style of physics you are aiming for):

 

And a very good tip: Do not shorten variable names! Its confusing for other guys and will also most confuse yourself when you read it years later.

 

One last thing, jumping may be done using an upward impulse (instant change in velocity) which is as simple as this:

velocity += impulse

Edited by Finalspace

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this