# OpenGL first person jumping code

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.

-
You got bad performance? the code is good after all. A question: if a player is jumping falling will never be true? and if a player is falling jumping will never be true? I Mean: can jumping and falling be true together? – Marco Acierno Mar 1 '14 at 23:59
@MarcoAcierno My performance is fine, I was just wondering if there was a more efficient way rather than checking the player is jumping every frame. Also, falling and jumping will never be true at the same time so I have changed it to jumping and !jumping. Thank you for pointing that out. – icedvariables Mar 2 '14 at 0:47

After your comment, the only change i could say is to change

private void jump(){ // called every frame
if(jumping){
if(y >= -jumpHeight){
y -= jumpSpeed;
}else{
jumping = false;
falling = true;
}
}

if(falling){


to

private void jump(){ // called every frame
if(jumping){
if(y >= -jumpHeight){
y -= jumpSpeed;
}else{
jumping = false;
falling = true;
}
}

else if(falling){


To avoid to check falling too if it's jumping. But its a macro optimization (useless too maybe in this case.) But really, this code is perfect how it is. So go ahead!

Just got this idea in my mind: you could define a "PlayerStatus" enum with: Falling, Jumping but it will affect the logic of your game, change it only if you think you will need more status and maintain two boolean variables (or more if you add more conditions) will be hard. If you need only one turned on and others off.. it's a great way to do it:

enum PlayerStatus
{
FALLING,
JUMPING
}


and use the enum to change the status..

PlayerStatus status;
status = PlayerStatus.FALLING;

-