I'm making a game in Java with LWJGL and I just implemented jumping. I don't really think the code is particularly well written or very efficient. Here is the code of my Camera
class:
public void startJump(float height, float land){
jumpHeight = height;
jumpLand = land;
if(!falling && !jumping)
jumping = true;
}
private void jump(){ // called every frame
if(jumping){
if(y >= -jumpHeight){
y -= jumpSpeed;
}else{
jumping = false;
falling = true;
}
}
if(falling){
if(y <= -jumpLand){
y += jumpSpeed;
}else{
jumping = false;
falling = false;
}
}
}
If someone knows a cleaner or more efficient method please tell me!
EDIT:
I was thinking of using jumping
and !jumping
but this introduces a bug where if the jump key is held down then the player hovers in the air so unless your making a jet-pack use jumping
and falling
.
jumping
falling
will never be true? and if a player isfalling
jumping
will never be true? I Mean: canjumping
andfalling
be true together? – Marco Acierno 2 days agojumping
and!jumping
. Thank you for pointing that out. – icedvariables 2 days ago