Skip to main content
gamedev.net
🔒 Locked

Jumping

Started by Arangol Sep 21, 2006 at 10:45 AM 4 replies 1k views
Original Post
Arangol
Arangol
How would be the best way to do jumping in a 3d third-person role playing game? I have a void Jump() with mPos.y for your y-axis position. Please post examples, source code, pseudocode, guidelines or just ideas for jumping. Thanks in advance.
win_crook
win_crook
I think a sine wave on the axis would work... because sine waves start from 0 and go up to 1 you just need to find a good way to implement it.
Arangol
Arangol
I was thinking along the lines of:

void Jump()
{
float speed = 0.6;
speed-=0.05;

mPos.y+=speed;

if(mPos.y>5)
{
mPos.y-=2;

if(mPos.y<0)
mPos.y = 0;

}

But then I still have to have a boolean isJumping for checking if already jumping and so that multiple keypresses when in air wont be done.

Would this work?

Or is there a better way?
Rob Loach
Rob Loach
You'll want to track your character's ySpeed all the time and then every game tick, move the character by their ySpeed:
void Update(){    mPos.y += ySpeed; // move character    ySpeed += 0.1f; // apply gravity    // Check for collisions    if(mPos.y > 5)        ySpeed = 0f;}

To make the character jump, all you'd have to do is set the ySpeed to a negative number:
void Jump(){    ySpeed = -60f;}
Rob Loach [Website] [Projects] [
Palidine
Palidine
This may just be different words for what others have suggested, but here's what i do:

1) implement gravity in my physics system
2) jump is then just setting the upwards velocity to an "upwards" value (for me that's setting the z coordinate of the velocity vector to a positive number).
3) because you implemented gravity, the thing jumping will go up and then fall back to the ground according to a nice parabolic arc. then tuning your jump is as simple as adjusting the amount by which you modify the upwards velocity component.

funtimes

-me
TheAdmiral
TheAdmiral
Just for the record, a sine wave isn't a very good approximation to a parabola. Newton's universal law of gravitation directly implies that a jumping body follows a '-x²' curve. To achieve such a path in instantaneous code, you need to alter velocity a constant amount per unit time. The examples already given are great.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.