3
\$\begingroup\$

I'm trying to create a proper jumping system for a 2D platformer with box2D, however, I'm not exactly sure how to make a good jumping system. Right now, I'm just doing

public void jump(float delta) {
    body.applyLinearImpulse(new Vector2(0, 7), body.getWorldCenter(), true);
}

What I want to do, is have a system like Mario's or Sonic's, where the height jumped is determined by how long the player held the jump button. How do I do something like that? Also, what's better, a rectangular box2D fixture, or an oval one? I'm sorry for such a newb question, and thank you for the help!

\$\endgroup\$
3

2 Answers 2

2
\$\begingroup\$

SMB "weakens" gravity to extend jump height when these three conditions are met:

  1. You are still moving upwards.
  2. You didn't release the jump button since you left the ground.
  3. You didn't collect a power up.

I included a link to the documentation regarding SMB jumping physics in a comment above. The idea is that you have an initial upwards speed s when you hit the jump button. For instance 10m/s and as long as the previous conditions are being met, gravity is much weaker or in Box2D terms, there is a continuous force pushing you upwards and that force is weaker than gravity, being about 70% as strong so gravity is reduced to 30% of it's usual strength.

This allows the player to fine tune the jump height.

\$\endgroup\$
1
\$\begingroup\$

iforce2d to the rescue. if you can do some vector math nothing is impossible.

https://www.iforce2d.net/b2dtut/jumping

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .