Sign in to follow this  

Jump impulse based on length of pressed jump-button in box2d

This topic is 1104 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

What is a simple approach to let a player jump, but let the height/impulse based on length of the pressed jump button or - how do i add constant impulse while a jumping is done (jump key is pressed until released, to increase the jump over time) without weird behavior. Like super mario jump...

 

I never had done this in box2d before so i wanted to ask you if there is a simple way to do this..

 

Thanks,

Final

Share this post


Link to post
Share on other sites

Hmm, Mario style games do it by lowering gravity on the character.  I am not sure if Box2D has local gravity per object, but you could probably do something like add a force equal to half the gravity in the opposite direction of gravity while the jump button is pressed, and then remove it when it's released.

Share this post


Link to post
Share on other sites

Box2D objects each have a gravity scale, if i remember correctly, so you could do as ferrous said.

Alternatively, you could keep a force or impulse accumulator in you object, let's call it jump_energy, and each frame you want to apply F amount of force if the jump key is down, first check if the object has any jump_energy left, by taking force_to_apply = std::min(jump_energy, F), subtract jump_energy -= force_to_apply, and do object.applyImpulse(force_to_apply). You have to fiddle with the numbers to get the right feel, but this is one way to do it.
You could do something similar, but with distance travelled as the check, or with a timer so the player can keep the jump key down for x seconds before you stop applying the jump impulse every frame.

 

Also, i seem to vaguely remember there being a problem with this if applying impulses every frame, and it's done with forces instead, but don't hold me to this, and try it both ways.

Share this post


Link to post
Share on other sites

Thats the approach i implemented now, works really good after tuning the properties. Thanks.

 

Box2D objects each have a gravity scale, if i remember correctly, so you could do as ferrous said.

Alternatively, you could keep a force or impulse accumulator in you object, let's call it jump_energy, and each frame you want to apply F amount of force if the jump key is down, first check if the object has any jump_energy left, by taking force_to_apply = std::min(jump_energy, F), subtract jump_energy -= force_to_apply, and do object.applyImpulse(force_to_apply). You have to fiddle with the numbers to get the right feel, but this is one way to do it.
You could do something similar, but with distance travelled as the check, or with a timer so the player can keep the jump key down for x seconds before you stop applying the jump impulse every frame.

 

Also, i seem to vaguely remember there being a problem with this if applying impulses every frame, and it's done with forces instead, but don't hold me to this, and try it both ways.

Share this post


Link to post
Share on other sites

This topic is 1104 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this