Advertisement

2D Platformer Jumping Problems

Started by September 15, 2014 04:23 AM
0 comments, last by Finalspace 10 years, 5 months ago

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 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

This topic is closed to new replies.

Advertisement