SFML community forums
Help => Graphics => Topic started by: dotty on January 20, 2012, 08:40:27 pm
-
Hay I have a simple script which makes my sprite jump.
I have this line being fired every frame
this->sprite.Move( (this->deltaTime * this->xVelocity), (this->jumpSpeed * this->deltaTime) * this->yVelocity);
And when my jump button is pressed this code gets run
this->yVelocity = -this->weight;
if (this->sprite.GetPosition().y < apex) {
this->isJumping = false;
}
This works well. I hit jump, my character jumps, reaches a peak then drops down. However, my movement is more like an upside down V rather than an arch. How would I accomplish a more "real" jump effect?
-
Your yVelocity variable should be changed every frame to simulate gravity.
-
How would I about doing this? If Y is above a certain point then change the yVelocity, or if a certin amount of time has pass changed the Yvelocity?
-
add some small amount to your y velocity every frame. When jumping add a far bigger (negative) amount to it.
Depending on your coordinate system, scale, etc. this might need some adjustments, so consider this just to give you an idea:
if(jump) dy -= 0.05;
dy += 0.001;
-
Kinda what Mario said, you have a position and a velocity, so why not add an acceleration (which is how you'd calculate the effect of gravity anyhow), so now it is:
velocity.y += acceleration.y; // acceleration.y would be negative, while velocity.y would initially be positive.
position.y += velocity.y;
-
There is a problem with different frame.
I made something like in this topic gravity -= 0.1f; and move it, when stp jumping set again the gravity to old value.
The problem is if i enable VSync the sprite jump really high, if i disable it it jump like i want... how can i fix this?
I was thinking about using clock.getelapsedtipe().assecond() to make jumptimer the same!
-
this->yVelocity = -this->weight; => this->yVelocity = -this->weight*delta_time;
Maybe :roll: