Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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.

  • You cannot reply to this topic
11 replies to this topic

#1 rubsnick   Members   -  Reputation: 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.

Sponsor:

#2 Steve_Segreto   Crossbones+   -  Reputation: 1551

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!

#3 rubsnick   Members   -  Reputation: 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.

#4 America Freeman   Members   -  Reputation: 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?

#5 rubsnick   Members   -  Reputation: 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   -  Reputation: 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

 


#7 TheChubu   Crossbones+   -  Reputation: 4791

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


#8 Bacterius   Crossbones+   -  Reputation: 9293

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#9 America Freeman   Members   -  Reputation: 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.

#10 SimonForsman   Crossbones+   -  Reputation: 6322

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!

#11 Mussi   Crossbones+   -  Reputation: 2103

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.

#12 rubsnick   Members   -  Reputation: 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.



PARTNERS