Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
2  
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 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 at 0:47

1 Answer 1

up vote 5 down vote accepted

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.