How can I get my character to come back down even if the user still has the button pressed?
#1 Members - Reputation: 105
Posted 10 December 2012 - 02:47 PM
#2 Members - Reputation: 836
Posted 10 December 2012 - 02:52 PM
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!
#3 Members - Reputation: 105
Posted 10 December 2012 - 03:00 PM
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.
#5 Members - Reputation: 105
Posted 10 December 2012 - 03:40 PM
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.
#6 Banned - Reputation: 161
Posted 11 December 2012 - 10:09 AM
"Only idiots quote themselves" - MisterFuzzy
#8 Crossbones+ - Reputation: 3516
Posted 11 December 2012 - 11:33 AM
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.Or just set up the thing so the player can't jump if the character isn't on the ground.
#9 Members - Reputation: 284
Posted 11 December 2012 - 01:28 PM
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.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.
Or just set up the thing so the player can't jump if the character isn't on the ground.
And my mistake, Mister Fuzzy's correction of swapping when the key is released is the correct way to go.
#10 Members - Reputation: 3678
Posted 11 December 2012 - 01:47 PM
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.
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.
Or just set up the thing so the player can't jump if the character isn't on the ground.
And my mistake, Mister Fuzzy's correction of swapping when the key is released is the correct way to go.
Thats best solved the same way though, except you allow for acceleration to continue for a fixed amount of time after the jump started.
The voices in my head may not be real, but they have some good ideas!
#11 Crossbones+ - Reputation: 959
Posted 11 December 2012 - 02:17 PM
*** 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.
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.
#12 Members - Reputation: 105
Posted 19 December 2012 - 07:20 PM
[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]






