SFML community forums

Help => Graphics => Topic started by: dotty on January 20, 2012, 08:40:27 pm

Title: Jump doesn't realistically
Post 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

Quote
   this->sprite.Move( (this->deltaTime * this->xVelocity), (this->jumpSpeed * this->deltaTime) * this->yVelocity);


And when my jump button is pressed this code gets run

Quote
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?
Title: Jump doesn't realistically
Post by: Tex Killer on January 20, 2012, 08:46:14 pm
Your yVelocity variable should be changed every frame to simulate gravity.
Title: Jump doesn't realistically
Post by: dotty on January 21, 2012, 11:47:11 am
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?
Title: Jump doesn't realistically
Post by: Mario on January 21, 2012, 12:48:30 pm
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:
Code: [Select]
if(jump) dy -= 0.05;
dy += 0.001;
Title: Jump doesn't realistically
Post by: RedIrony on January 21, 2012, 06:29:57 pm
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:
Code: [Select]
velocity.y += acceleration.y; // acceleration.y would be negative, while velocity.y would initially be positive.
position.y += velocity.y;
Title: Jump doesn't realistically
Post by: TheEnigmist on January 23, 2012, 01:59:04 pm
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!
Title: Jump doesn't realistically
Post by: Orwel on January 23, 2012, 03:34:51 pm
this->yVelocity = -this->weight;  => this->yVelocity = -this->weight*delta_time;


Maybe  :roll: