* GM: Handling Platform Game Jumping

(1/1)

Richard Marks:
Okay, I want you to stop and think about it for a few minutes and then I want you to tell me how you think one should go about implementing "jumping" characters in a game. Any programming languages are valid. Even Non-languages such psuedo-code is fine. But, if you read this, then you MUST post a reply that relates to this thread.  8)

Below is the data structure (in psuedo-code) for the character's "physics model" to help you name things accordingly.
Code:

BEGIN STRUCTURE: PhysicsModel
 POSITION AS FLOAT COORDINATE
 VELOCITY AS FLOAT COORDINATE
 ACCELERATION AS FLOAT
 MASS AS FLOAT
 CENTER_OF_GRAVITY AS FLOAT COORDINATE
 SURFACE_FRICTION AS FLOAT
END STRUCTURE

maligare:
I am by no means the authority on "gamming programming fundamentals"  but this is how I'd envision conquring a problem such as the one mentioned.
 
Code:


public function jump(figure f){
    if(!f.isJumping()){
        f.setJumping(true);
        f.setJumpTime(env.getTime());
        f.setYVelocity(jumpVelocity); // jumpVelocity is a predefined value that represents
                                      //the "height" of the jump.  This value can be adjusted given
                                     //"jump bonuses" such as moonboots, rocket boosters, etc
    }
}


Thats pretty much it for the actual jumping bit.  How the environment would react to your movement would be coded in the environment itself.  Such things as low ceilings can reduce yVelocity to zero when they are reached and yVelocity is also instantly zeroed when ground (or a ledge) is reached.

You could also easily implement a double jump or other combo jump by checking the "isJumping" variable.
ie adding the following code to the above snippet:
Code:

public function jump(figure f){
    if(!f.isJumping()){
    ...
    }
    else if(f.specialJumpingIsEnabled() && !f.isSpecialJumping() ){
        f.setIsSpecialJumping(true);
        f.setYVelocity(f.getYVelocity() + jumpBonus);
    }
}

After determining that the figure is already jumping, the program would check to see whether or not the figure is eligible for special jumping and whether the figure is already in the process of special jumping.  After determining that the elgible is currently elgible to perform the desired action, the action is performed and the physics engine of the gaming environment takes over from there.   

Notice that in both of these examples, all that the jump functions change is the yVelocity which is a data space in the figure object.  This is all you will have to change if you do a good job programming the physics engine of the gaming environment itself.

A few more words about the physics engine that I imagine -
A centralized timer would keep everything running together, it would render graphics at minimally 24fps to ensure smooth motion.
It also would cycle through all figures and perform the necessary velocity reductions of gravity:
Code:

public void calculateGravity(){
    for(Figure f:figureCollection){
        if(!f.grounded()){
            f.setVelocity(f.getVelocity - gravity * (currentTime - f.getJumpTime())
        }
    }
}
public void moveFigures(){
    for(Figure f:figureCollection){
        f.setXPos(f.getXPos + f.getXVelocity);
        f.setYPos(f.getYPos + f.getYVelocity);
    }
}
public void timerClick{
    calculateGravity();
    moveFigures();
}



As I said before, this is by no means perfect.  Please reply if you see something that you think would work better. :-D

Also, I noticed that I did not write the code for dealing with collisions (with the ground after landing, with other obstacles in the gaming env).  I'd imagine another collection of immediate obstcles (ledges, walls, trees, etc) that could be quickly scanned to check if the position of each figure is within the range of position coordinates for an immovable object.  Finding an object that has collided, the engine would adjust the figure's velocity accodingly (either zero it for a dead stop, or direct it in the opposite direction for a rebounding affect.)

enjoy!

Navigation

[0] Message Index