HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

OpenGL first person jumping code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
personjumpingfirstopenglcode

Problem

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.

Solution

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;

Code Snippets

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

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

    else if(falling){
enum PlayerStatus
{
    FALLING,
    JUMPING
}
PlayerStatus status;
status = PlayerStatus.FALLING;

Context

StackExchange Code Review Q#43179, answer score: 5

Revisions (0)

No revisions yet.