How can I get my character to come back down even if the user still has the button pressed?

Started by
10 comments, last by rubsnick 11 years, 4 months ago
I sort of cheated in my Physics Department I want to make my Code Efficient. I don't want it to do any complex physica algorithms since I"m developing the game for a Cell Phone and want it to work on the weakest ones avalible. So thus I cheated on my physics. When you press the jump button he jumps and he has a limit of 50pixels. He won't go past 50 pixels. Which is great so no matter how long the button is pressed he won't go past his boundry. The main problem I'm having is even after the button is pressed he will simply just float. He reaches the limit but only floats. I want my character to act like sonic. The longer the press the higher he goes but once he reachers his peak I want him to fall back down. I just don't know how to bring him down. I tried to check if he was at 50px subtract it until 0 but it only does it once before he goes back up (thanks to the game loop). So I don't really know how to time the input out or what would the best course of action be.
Advertisement
You might have to use some math. It won't be as complex as a physics engine, so the code will still be efficient.

Basically, define a number of pixels per frame for gravity, like -20. Then apply this to your character's y-position each frame, clamping to the current height of the platform he is standing on (so he doesn't sink through the ground).

Each time they press jump button add a positive number of pixels per frame to counteract gravity. Like +30 per jump button press. Manage the height of the character so it doesn't go over +50 pixels above the ground and you've got it!

You might have to use some math. It won't be as complex as a physics engine, so the code will still be efficient.

Basically, define a number of pixels per frame for gravity, like -20. Then apply this to your character's y-position each frame, clamping to the current height of the platform he is standing on (so he doesn't sink through the ground).

Each time they press jump button add a positive number of pixels per frame to counteract gravity. Like +30 per jump button press. Manage the height of the character so it doesn't go over +50 pixels above the ground and you've got it!


That's exactly how I emplemented it. but If I keep the button pressed he never goes down. My gravity get cancelled out.
You could create a bool where jumping only works if it's false. Set it to true once they reach the 50 pixel limit, and then reset it back to false when they're touching the ground again. Does that make sense?

You could create a bool where jumping only works if it's false. Set it to true once they reach the 50 pixel limit, and then reset it back to false when they're touching the ground again. Does that make sense?


Yes it makes sense, that would actually work quite nicely. I have no idea why I didn't think of it any sooner.
You should probably move that bool to when the player releases the jump button, because if it only triggers at the 50px mark, you could make a half-jump but then press the button again to hover.

"Only idiots quote themselves" - MisterFuzzy

Or just set up the thing so the player can't jump if the character isn't on the ground.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator


Or just set up the thing so the player can't jump if the character isn't on the ground.

This is the correct solution. Really, all jumping is, is applying a downward force to the ground, which applies an upward force to us and lifts us up in the air against gravity. If we're not in contact with the ground, we can't jump and gravity pulls us back down with no force to push us back up.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


[quote name='TheChubu' timestamp='1355246155' post='5009456']
Or just set up the thing so the player can't jump if the character isn't on the ground.

This is the correct solution. Really, all jumping is, is applying a downward force to the ground, which applies an upward force to us and lifts us up in the air against gravity. If we're not in contact with the ground, we can't jump and gravity pulls us back down with no force to push us back up.
[/quote]
If he was going for realistic physics, yes, but it sounds like he wanted jumping to work where the longer you held down the jump key, the higher you jump. It's the kind of jumping you see in many platformers.

And my mistake, Mister Fuzzy's correction of swapping when the key is released is the correct way to go.

[quote name='Bacterius' timestamp='1355247230' post='5009462']
[quote name='TheChubu' timestamp='1355246155' post='5009456']
Or just set up the thing so the player can't jump if the character isn't on the ground.

This is the correct solution. Really, all jumping is, is applying a downward force to the ground, which applies an upward force to us and lifts us up in the air against gravity. If we're not in contact with the ground, we can't jump and gravity pulls us back down with no force to push us back up.
[/quote]
If he was going for realistic physics, yes, but it sounds like he wanted jumping to work where the longer you held down the jump key, the higher you jump. It's the kind of jumping you see in many platformers.

And my mistake, Mister Fuzzy's correction of swapping when the key is released is the correct way to go.
[/quote]

Thats best solved the same way though, except you allow for acceleration to continue for a fixed amount of time after the jump started.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement