Original Post
🔒 Locked
Jumping
Advertisement
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?
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?
You'll want to track your character's ySpeed all the time and then every game tick, move the character by their ySpeed:
To make the character jump, all you'd have to do is set the ySpeed to a negative number:
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;}
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
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
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
Regards
Admiral
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement