# Jumping

This topic is 4260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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;}

##### Share on other sites
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

##### Share on other sites
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

• 17
• 10
• 19
• 14
• 19