0
\$\begingroup\$

I am trying to implement jump in a game I have written for android using canvas. I have referred this tutorial for implementing jump. My code is like

if(jumpKeyPressed){
    jumpVel = -initialMomentum;
    state = "jumping";
}
if(state == "jumping"){
    jumpVel += gravity * deltaTime / 2;
    loc.y += jumpVel * deltaTime;
    jumpVel += gravity * deltaTime / 2;
}

Even after tweaking gravity and initialMomentum, character is jumping too slow (seems like stuck at ground). I have read many posts but I am not able to figure out what am I doing wrong here?

Edit: Video

\$\endgroup\$
  • \$\begingroup\$ Does jumpVel and jumpVelocityY refer to the same object/value? You might want to try simple Euler integration first to see if that works, like Lolums suggested in his answer, and then use the method in the article to make it more accurate. Note that the metod in the article is only beneficial in case you want to handle low framerates. You can solve this problem in other ways too, such as using a fixed physics timestep. \$\endgroup\$ – Danik Jan 24 '16 at 13:13
  • \$\begingroup\$ Sounds like your initialMomentum and gravity numbers might not be in balance (too little momentum for the amount of gravity), or some other code is interfering (say, overzealous collision detection preventing the object from moving vertically while colliding, even if it's trying to move away). Can you show us the numbers you're using, and a video of the problem so we all have the same understanding of the symptoms? \$\endgroup\$ – DMGregory Jan 24 '16 at 15:06
  • \$\begingroup\$ Thanks Danik for pointing that out, I have edited my question. \$\endgroup\$ – Ishank Gulati Jan 24 '16 at 17:31
  • \$\begingroup\$ @DMGregory Right now I am using InitialMomentum=60 and gravity=40 and my character goes up really slowly and the comes down. If I try to increase momentum, character goes higher but still very slow. If I increase gravity the character jumps lower but not enough change in acceleration. I think I am not able to tune them correctly. Any recommendation? \$\endgroup\$ – Ishank Gulati Jan 24 '16 at 17:48
  • \$\begingroup\$ My recommendation remains: show us a video. \$\endgroup\$ – DMGregory Jan 24 '16 at 17:51
2
\$\begingroup\$

My answer is based on Lolums' answer. His logic is good, especially if you are making a simulation of something with realism. But Philipp points out something important as well: correct physics do not typically translate well to platform games.

The take-away is that you should apply the idea of gravity velocity_y -= gravity * delta_time but you should not be afraid to use numbers that seem unrealistic. The ones you use will likely be higher than you'd expect by studying the real world, sometimes much higher. Case in point, consider Super Mario World.

  • When jumping regularly, Mario experienced a gravity of 67.82 m/s^2 (6.9 times Earth gravity). Here he had a push-off velocity of 17.36 m/s.
  • When high-jumping, Mario experienced a gravity of 34.79 m/s^2 (3.5 times Earth gravity). Here he had a push-off velocity of 15.21 m/s.
  • When falling off a ledge, Mario experienced a gravity of 55.88 m/s^2 (5.7 times Earth gravity).
  • As for running, Mario seemed to follow a perfectly constant run velocity pattern (as is expected, really). When walking, he moved at a speed of about 3.7 m/s, and when running, moved at a speed of about 9.1 m/s.

Source: http://hamaluik.com/posts/super-mario-world-physics/

Almost seven times Earth's gravity, under some conditions. And for that matter, note how inconsistent the acceleration values are. This violates many laws of physics, and that's okay.

My advice: Apply the basic model of gravity as you know it to exist, and then test your game. Play with the numbers. The force gravity in Castlevania 4 feels very, very strong. The gravity in Symphony of the Night doesn't. They're both excellent platform games, because the developers experimented and found what worked for their games.

\$\endgroup\$
  • \$\begingroup\$ Thanks...I have adjusted the values and jump seems to be much better now. As you pointed out the problem was I was trying to be more realistic in setting the parameters. \$\endgroup\$ – Ishank Gulati Jan 25 '16 at 20:32
2
\$\begingroup\$

Physics student here.

Firstly, separate your velocity into two components.

Character {
    float velocity_x;
    float velocity_y;
}

Now, when they jump, you add an initial velocity to their velocity_y and every frame, you do something like loc.x += velocity_x * delta_time (and the same for y).

Every frame, you also do velocity_y -= gravity * delta_time (and only to the y value). If they're falling to quickly, turn gravity down. If they're jumping too high, turn gravity up. Play with it.

As for the velocity_x, you can set that to the speed at which they're moving whilst they jump. When they horizontally collide, set the velocity_x to 0.

\$\endgroup\$
  • \$\begingroup\$ isn't velocity_y(pixel/s) -= gravity (pixel/s^2) imbalanced? I have seperated character velocity in x and y components but I have not shown it here. \$\endgroup\$ – Ishank Gulati Jan 24 '16 at 9:06
  • \$\begingroup\$ Ah. It's supposed to be imbalanced, aka the player falls back down after jumping. Pixels are a horrible measure of distance because of screen resolutions. Use units like "Tiles" or "Blocks". \$\endgroup\$ – Lolums Jan 24 '16 at 10:20
  • \$\begingroup\$ I believe Ishank refers to the different dimensions on the two sides in velocity_y -= gravity since gravity is an acceleration and not a velocity, in that case it should be velocity_y -= gravity*delta_time. \$\endgroup\$ – Danik Jan 24 '16 at 13:12
  • \$\begingroup\$ Yes, Danik is right but velocity_y -= gravity*delta_time is already what I was using and it doesn't seem to work. \$\endgroup\$ – Ishank Gulati Jan 24 '16 at 13:28
  • 1
    \$\begingroup\$ This might give you physically correct jumping, but those aren't the jump physics player expect from the average platformer. \$\endgroup\$ – Philipp Jan 24 '16 at 15:07
0
\$\begingroup\$

You never tweak gravity. It is a constant force. A jump is an instantaneous force applied to a body only when the altitude of the body is at 0.

To jump, you must apply a force or acceleration strong enough to overcome gravity.

We do this by applying a force that results an acceleration of +9.8 or higher

example: Mass=10 Force=10 Acceleration=1 (no-good)

Mass=10 Force=100 Acceleration=10 (good)

\$\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.