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

This topic is 1856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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!

Share on other sites
[quote name='Steve_Segreto' timestamp='1355172729' post='5009193']
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!
[/quote]

That's exactly how I emplemented it. but If I keep the button pressed he never goes down. My gravity get cancelled out.

Share on other sites
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?

Share on other sites
[quote name='America Freeman' timestamp='1355173776' post='5009199']
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?
[/quote]

Yes it makes sense, that would actually work quite nicely. I have no idea why I didn't think of it any sooner.

Share on other sites
You should probably move that bool to when the player[b][i] releases [/i][/b]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.

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

Share on other sites
[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.
[/quote]
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.

Share on other sites
[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.
[/quote]
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 [i]released [/i]is the correct way to go.

Share on other sites
[quote name='America Freeman' timestamp='1355254115' post='5009515']
[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.
[/quote]
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 [i]released [/i]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.

Share on other sites
[quote name='SimonForsman' timestamp='1355255278' post='5009523']
*** snip ***

Thats best solved the same way though, except you allow for acceleration to continue for a fixed amount of time after the jump started.
[/quote]

I'd also set that fixed amount of time to 0 when the button in released and reset it once you hit the ground to guarantee smooth movement when mashing the jump button.

Share on other sites
Thanks everyone for your responses I've been working on it for a while and I just cannot seem to get past my problem. He stays up in the air regardless of what I do. :/ Here is my code What the hell am I doing wrong...

[source lang="java"] if(Gdx.input.isKeyPressed(Keys.SPACE)&&man.y<=150&&AllowJump==true)
{

stateTime += Gdx.graphics.getDeltaTime(); // #15
currentFrame = walkAnimation.getKeyFrame(stateTime, true);

man.y += 450 * Gdx.graphics.getDeltaTime();

if(man.y>150)
{
AllowJump=false;
}

}

//This is my Release

if(!Gdx.input.isKeyPressed(Keys.SPACE))
{
if(man.y>=50)
{
man.y -= 350 * Gdx.graphics.getDeltaTime();
}
if(man.y<=50)
{
AllowJump=true;
}
}[/source]