2
\$\begingroup\$

The player object in my game has its x-axis locked. It can only jump on Y. It's the ground that constantly moves to the left so that it appears like the player is moving to the right.

The ground is moving left at a speed of 240 pixels per second.

Whenever the player falls and hits the ground, I want it to jump so that it hits the ground again when it has gone 180 pixels to the left.

So, it should jump, the ground should get to move 180 pixels and then the player should hit the ground at that moment and jump. Then repeat. 180 pixels each time.

Oh and, the jump height may be variable. Specifically, there will be two jumps: a small jump and a big jump. Both need to land after the ground has moved 180 pixels.

I'm bad at math, so I can't figure it out. I would appreciate any help!

\$\endgroup\$
4
\$\begingroup\$

If your ground scrolls at

groundSpeed = 240 // pixels per second

And you want your jump to cover

jumpDistance = 180 // pixels

Then your jump needs to last

jumpDuration = jumpDistance / groundSpeed = 180 / 240 = 0.75 // seconds

Let's say arbitrarily that you want the jump to take a parabolic arc. If we track the time since the jump started, and the height the player was at then, we can set:

currentHeight = startingHeight 
              + jumpHeight
              * (1 - 4 * (timeSinceJumpStart / jumpDuration - 0.5)^2)

This gives you a parabolic height that hits startingHeight at timeSinceJumpStart == 0 and at timeSinceJumpStart == jumpDuration, and hits a maximum altitude of startingHeight + jumpHeight in between.

Note that doing it this way, when you do your high jump, you're also effectively increasing gravity - to bring you down faster even though you had more upward velocity to carry you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.