Last active
August 27, 2020 13:44
-
-
Save fecub/6669291 to your computer and use it in GitHub Desktop.
GameDev - Jumping Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Initialize the variables | |
-------------------------- | |
private int jumpWidth = 0; | |
private boolean jumpStatus = false; | |
private int gravity = 2; | |
private int jumpPower = 5; | |
private boolean startJump = false; | |
*/ | |
public void update() { | |
if (keyboardInput.up.isPressed() == true) { | |
if (keyboardInput.shift.isPressed() == true) | |
y -= 6; | |
else | |
y--; | |
} | |
if (keyboardInput.down.isPressed() == true) { | |
if (keyboardInput.shift.isPressed() == true) | |
y += 6; | |
else | |
y++; | |
} | |
if (keyboardInput.left.isPressed() == true) { | |
if (keyboardInput.shift.isPressed() == true) | |
x -= 6; | |
else | |
x--; | |
} | |
if (keyboardInput.right.isPressed() == true) { | |
if (keyboardInput.shift.isPressed() == true) | |
x += 6; | |
else | |
x++; | |
} | |
if (keyboardInput.space.isPressed() == true) { | |
if (jumpStatus != true && jumpWidth <= 0) { | |
startJump = true; | |
jumpStatus = true; | |
} | |
} | |
/* Jump algorithm */ | |
if (startJump) { | |
if (jumpStatus) { | |
if (jumpWidth < jumpHeight) { | |
System.out.println("y = " + y); | |
jumpWidth += jumpPower + gravity; | |
y -= jumpPower + gravity; | |
} | |
else if (jumpWidth >= jumpHeight) { | |
jumpWidth += jumpPower + gravity; | |
y -= jumpPower + gravity; | |
jumpStatus = false; | |
} | |
} | |
else if (!jumpStatus) { | |
if (jumpWidth > 0) { | |
System.out.println("y = " + y); | |
jumpWidth -= jumpPower + gravity; | |
y += jumpPower + gravity; | |
} | |
else if (jumpWidth <= 1) { | |
jumpWidth -= jumpPower + gravity; | |
y += jumpPower + gravity; | |
startJump = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What do i put for jumpHeight?