• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1rubsnick  Members

106
Like
0Likes
Like

Posted 10 December 2012 - 02:47 PM

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.

### #2Steve_Segreto  Members

2007
Like
0Likes
Like

Posted 10 December 2012 - 02:52 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!

### #3rubsnick  Members

106
Like
0Likes
Like

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.

### #4America Freeman  Members

436
Like
0Likes
Like

Posted 10 December 2012 - 03:09 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?

### #5rubsnick  Members

106
Like
0Likes
Like

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 MisterFuzzy   Banned

161
Like
0Likes
Like

Posted 11 December 2012 - 10:09 AM

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

### #7TheChubu  Members

8954
Like
1Likes
Like

Posted 11 December 2012 - 11:15 AM

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

### #8Bacterius  Members

13100
Like
0Likes
Like

Posted 11 December 2012 - 11:33 AM

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.”

### #9America Freeman  Members

436
Like
0Likes
Like

Posted 11 December 2012 - 01:28 PM

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 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.

### #10SimonForsman  Members

7585
Like
0Likes
Like

Posted 11 December 2012 - 01:47 PM

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 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.

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 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!

### #11Mussi  GDNet+

4000
Like
0Likes
Like

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.

### #12rubsnick  Members

106
Like
0Likes
Like

Posted 19 December 2012 - 07:20 PM

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]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.