Skip to content

Instantly share code, notes, and snippets.

@fecub
Last active August 27, 2020 13:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fecub/6669291 to your computer and use it in GitHub Desktop.
Save fecub/6669291 to your computer and use it in GitHub Desktop.
GameDev - Jumping Algorithm
/*
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;
}
}
}
}
@Williamsusman
Copy link

What do i put for jumpHeight?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment