• Advertisement
Sign in to follow this  

2D Platformer Jumping Problems

This topic is 2107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the 2D platformer I'm working on, the jumping feels bad and I'm not sure if my variables are set poorly, or if the design I'm using is a bad choice. When I jump, the player jumps up very quickly, and then falls at a slower rate. Ideally I'd want the jump and fall to feel like they're the same speed.

Currently, to jump I'm applying an impulse, and then letting gravity take over. To slow the jump speed down I tried increasing the gravity but then the falling speed doesn't feel good. When I try to lower the magnitude of the impulse, I can't get the desired jump height. It seems like every time I try tweaking one variable to fix a problem it produces some other undesirable result. I guess what I want is to maintain the current jump height, but also have the jumping speed be slowed so that it's equal to the falling speed (gravity). Am I taking entirely the wrong approach here?

If you want to see what I'm talking about you can download the "game" here: [url="http://www.mediafire.com/?jfn3jdxvc5nvspq"]http://www.mediafire...jfn3jdxvc5nvspq[/url], enter is start, arrows move, x is jump

Here is the code to calculate position:
[CODE]
void CPhysicsController::update(double time)
{
ddx_ = forceX_ / mass_;
ddy_ = forceY_ / mass_;
dx_ += ddx_;
dy_ += ddy_;

float linearDamp = -0.15f;
dx_ += (dx_ * linearDamp);
dy_ += (dy_ * linearDamp);
x_ += (time * dx_);
y_ += (time * dy_);
}

void CPhysicsController::applyForce(float forceX, float forceY)
{
forceX_ += forceX;
forceY_ += forceY;
}

void CPhysicsController::applyImpulse(float impulseX, float impulseY)
{
dx_ += impulseX;
dy_ += impulseY;
}
[/CODE]

For gravity I do this:
[CODE]
if (physicsControllers_[i]->isOnGround() == false)
{
physicsControllers_[i]->applyForce(0.0f, 70.0f);
}
[/CODE]

And for jumping, I do this:
[CODE]
if (physicsController_->isOnGround() == true)
{
physicsController_->applyImpulse(0.0f, -2000.0f);
}
[/CODE]

Share this post


Link to post
Share on other sites
Advertisement
You could have your physics engine check whether or not the player is currently jumping, and then only apply gravity if they're not
That way, you could set the force of gravity and your impulse force how you want and they wouldn't affect each other

Share this post


Link to post
Share on other sites
[quote][code] dx_ += ddx_;
dy_ += ddy_;
[/code][/quote]

Do you mean this?
[code] dx_ += time*ddx_;
dy_ += time*ddy_;[/code]

It looks like your linear damp might be too large. If you set it to zero, your jump should be completely symmetric.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1334109296' post='4930076']
[quote][code] dx_ += ddx_;
dy_ += ddy_;
[/code][/quote]

Do you mean this?
[code] dx_ += time*ddx_;
dy_ += time*ddy_;[/code]

It looks like your linear damp might be too large. If you set it to zero, your jump should be completely symmetric.
[/quote]

You're right, I set it to 0.0001 and it felt much better jumping, and I also scaled the acceleration by time.

But When I set it too low things move too fast and get slippery. Or maybe this is just because I need to implment friction/air resistance?

Share this post


Link to post
Share on other sites
Well, what you had there was some for of air resistance, but perhaps it was too much, so it felt like jumping in molasses. If your character is sliding on the floor, you need to implement friction.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1334110945' post='4930085']
Well, what you had there was some for of air resistance, but perhaps it was too much, so it felt like jumping in molasses. If your character is sliding on the floor, you need to implement friction.
[/quote]

Ah, I see. But then doesn't the problem exist that given enough time in the air, with such a low linearDamp value, the player would be able to get to very high velocities? Or maybe I should just keep playing around with it to see if I can find a good balance.

Share this post


Link to post
Share on other sites
Yes, the balance thing. The terminal velocity of a human body is about 56m/s according to Wikipedia. If you want realism, you can set your drag to match that. However, drag is primarily quadratic in the speed of the object, not linear.

Or you could ignore physical reality and do whatever feels right in your game, perhaps including a hard speed limit for falling characters. You need to experiment until you get the feeling you want.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1334126097' post='4930146'] Yes, the balance thing. The terminal velocity of a human body is about 56m/s according to Wikipedia. If you want realism, you can set your drag to match that. However, drag is primarily quadratic in the speed of the object, not linear. Or you could ignore physical reality and do whatever feels right in your game, perhaps including a hard speed limit for falling characters. You need to experiment until you get the feeling you want. [/quote]

Thank you so much for the help! I came up with a solution I'm happy with. I took some time to play the games that are an influence on what I'm trying to make and I realized the player characters actually don't even really use acceleration when they move, just a constant velocity. I think if I were to write a platformer where you can't change direction in an instant it would be frustrating so what I did was add a movement vector that sets a constant velocity and in my update function I added them like so:

[CODE]
x_ += (time * (dx_ + movementX_));
y_ += (time * (dy_ + movementY_));
[/CODE]

So I can still apply external forces like gravity, or a gust of wind, or standing on a conveyor belt or someting, but also have the option to move with a constant velocity as well.

In case anyone's interested here's a compiled version with my changes: [url="http://www.mediafire.com/?14lk6gaplgu69zi"]http://www.mediafire...14lk6gaplgu69zi[/url]. There's still a few bugs to work out but I think the biggest hump has been passed. Thanks again!

Share this post


Link to post
Share on other sites
I wrote a platform game and a bunch of articles explaining how it worked - you can download some example source for free if you like my movement feel:

[url="http://www.wildbunny.co.uk/blog/welcome-to-our-members-area/"]http://www.wildbunny.co.uk/blog/how-to-make-a-platform-game-source-code-options/[/url]

:)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement