How can I make the bounce height constant when the game is gradually getting faster?

Viewed 85

My main character is a ball that bounces forever at the same force. But I having difficulties finding the right way of bouncing at the same height as the game is increasing speed. Basically, here's my formula.

by the way, I'm using C++ and SDL2 so the Y axis is POSITIVE going down and NEGATIVE going up:

every frame, I'm adding the Yvelocity to the ball. Then I add the gravity to the velocity for gravity effect:

ballY += Yvelocity
Yvelocity += gravity
if(Yvelocity >= maxVelocity)
{
   Yvelocity = maxVelocity
}

Then whenever I hit the ground, I do this to bounce the ball

Yvelocity = bounceForce; // -10 for example

Everything works great. But I want to make the game faster and faster. So every specific time I do this:

bounceForce += bounceForce * multiplier;
gravity += gravity * multiplier;
maxVelocity += gravity * multiplier;
...
//increase other object's speed too

The game speed is indeed getting faster but the bounce of the ball is also getting higher.

It looks like I have to increase the gravity more than bounceForce. So I tried doing this:

bounceForce += bounceForce * multiplier;
gravity += gravity * (multiplier * 1.5f); ///////    <-------------------------
maxVelocity += gravity * multiplier;
...
//increase other object's speed too

That works fine at the beginning. But will likely do the same problem as the time goes by(bounces higher).

I think the more the Yvelocity is increasing, the more the gravity's multiplier has to increase too. Because the ball is getting lighter every increase of speed.

But I really can't find the right formula for that, of how I can make the ball bounce faster and faster but at the same height. Because it's not ideal for my game to increase speed then the ball is in constant speed. Sorry I'm a newbie in game programming. Thanks in advance.

1 Answers

The easy way to make things “the same, only faster” is to increase your time step between frames. Of course, you then have to make explicit all the places where you should have such a dt but have hardcoded it to 1. (Finding invisible code is always fun.)

The obvious places are of course the +=s at the beginning of your code (sketch); your dynamics may include more if they’re complicated enough. (Also note that your equations will become significantly inaccurate with a very large time step; you may want to make dt smaller (at first) and run more than one physics cycle per frame of animation.)

Related