# How to implement a cleaner code for jump up in air and fall down to ground mechanic

## Recommended Posts

Nicholas Kong    1535

I managed to get it to work using two different approaches: one is a position dependent code and the other is object state dependent. The latter being the best choice in my opinion given the fact that I am trying to make a 2D platformer in Java from scratch.

Problem: I got the character to move up but I cannot tell the character to go down without making the condition in the if statement evaluating a y coordinate of a position on screen. The reason why I do not like the position dependent code is because it looks messy but it works...for now. I have not added any platforms to the game so this code might need to be entirely object state dependent.

Is there another approach that is readable? I have a habit of writing unreadable code initially even with a lot of diagram and pseudo code planning on paper. Any suggestion on your approach or a refinement on the below code is welcomed.

if(state == ActionState.JUMPING)
{
// 315 is the y coordinate threshold on the screen. the character is on y coordinate 595.
// y coordinate decreases to this threshold and then will execute the else if
if(getY() > 315)
{
move character up
setY(getY() - jumpSpeed);
}
else
{
otherwise do this
state = ActionState.FALLING;
}
}
// will execute when character reaches the threshold
else if(state == ActionState.FALLING )
{
// temp is a copied value of the initial y coordinate when the character was created on the ground
// y coordinate
if(getY() < temp)
{
// move character down
setY(getY() + fallSpeed);
}
// at some point the character will reached its initial coordinate which is the value of temp
else
{
// so it will execute its idle animation
state = ActionState.IDLE;
}
}



##### Share on other sites
ApochPiQ    23011

The simple solution is to have your character carry three vectors: a location, a velocity, and an acceleration.

Each tick, you add the appropriate amount of acceleration to the current velocity. Then add the appropriate amount of velocity to the current position. To "jump" just add acceleration in the vertical direction. Apply a constant downward acceleration to represent gravity.

##### Share on other sites
ferrous    6137

And to get a jump motion like Mario, you add to your acceleration while the jump button is held down.  So holding the jump button down lets you jump higher.

##### Share on other sites
DiegoSLTS    2113

If you want a rule to decide if the character must start falling that doesn't depend on the height you can check the time since it started jumping. When you press down and change the state to jumping you set a timestamp, and you define somewhere a time value for a jump (half a second?). Then you change your "if(getY() > 315)" to "if(currentTime() > getTimestamp() + getJumpTime())".

The condition to stop falling also depends on the y position... it shouldn't, and it shouldn't depend on time neither. You need some collision detection system since you also plan to add platforms. You can make a first try checking if the chracter's bottom touches the ground's top before trying a more generic approach... and later check if it touches the top of any platform, not just the ground.

Edited by DiegoSLTS

##### Share on other sites
Nicholas Kong    1535

Interesting suggestions guys! Time and physics calculation and collision detection system it is!

##### Share on other sites
Mussi    4408

If you're trying to mimic a more realistic jump(i.e. not jumping higher while holding the jump button), you should add velocity instead of acceleration in the vertical direction the first time the jump button is pressed and the player is on the ground. In real life you only accelerate upwards while your feet are on the ground, once your feet leave the ground you have an upwards velocity and start accelerating downwards due to gravity.

##### Share on other sites
Anthony Serrano    3285

If you're trying to mimic a more realistic jump(i.e. not jumping higher while holding the jump button), you should add velocity instead of acceleration in the vertical direction the first time the jump button is pressed and the player is on the ground. In real life you only accelerate upwards while your feet are on the ground, once your feet leave the ground you have an upwards velocity and start accelerating downwards due to gravity.

In fact this is basically how old-school 2D games implement jumping: pressing the jump button instantaneously sets the character's vertical speed to a certain value, and then gravity is applied to the vertical speed every frame until the character is back on the ground.

Then there are basically three ways to deal with jump height control. The first method is that there is no jump height control; this is seen in games like Castlevania. The second method is seen in games like Mega Man and Sonic - here, if the character is moving upwards and the jump button is released, their upward velocity is capped to a specific value. The final method is the one used in games like Super Mario Bros where, when the character is moving upwards and the jump button is held down, the force of gravity is reduced.

Similarly, there are several different ways to deal with horizontal jump control.