Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting Sprite to Jump  (Read 3009 times)

0 Members and 1 Guest are viewing this topic.

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Getting Sprite to Jump
« on: January 15, 2012, 07:28:17 pm »
I am having a problem with getting my sprite to jump. I can move the sprite fine but I cant get it to jump. If I use Sprite.move instead of SetPosition it will move across the screen by a greater distance every time I press space
but not vertical.I know i probably dont have the falling part right either but I need to get jumping right first.

Code: [Select]


float velocity = 0;  
float gravity = 0;
float uprate = 0;
bool jumping = false;
bool falling = false;

if(Event.Key.Code == sf::Keyboard::Right)
Sprite2.Move(sprite2Speed * Window.GetFrameTime() /1000.f, 0.f);
if(Event.Key.Code == sf::Keyboard::Left)
Sprite2.Move(-sprite2Speed * Window.GetFrameTime() / 1000.f, 0.f);
if(Event.Key.Code == sf::Keyboard::Up)
Sprite2.Move(0.f, -sprite2Speed * Window.GetFrameTime() / 1000.f);
if(Event.Key.Code == sf::Keyboard::Down)
Sprite2.Move(0.f, sprite2Speed * Window.GetFrameTime() / 1000.f);
if(Event.Key.Code == sf::Keyboard::Space && jumping == false && falling == false)
{
velocity = 50.0f;
gravity = 0;
jumping = true;
}
if(jumping)
{
velocity = velocity - uprate;
Sprite2.SetPosition(Sprite2.GetPosition().x,Sprite2.GetPosition().y - (velocity));
if(velocity == -50.0f)
{
jumping = false;
falling = true;
gravity = 2;
}
if(falling)
velocity = velocity + gravity;
Sprite2.SetPosition(Sprite2.GetPosition().x,Sprite2.GetPosition().y+(velocity));
if(Sprite2.GetPosition().y + Sprite2.GetSize().y > Ground.GetPosition().y)
{
falling = false;
velocity =0;
gravity = 0;
}
}

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Getting Sprite to Jump
« Reply #1 on: January 15, 2012, 07:42:37 pm »
Dont compare floats using equal (chances are good you'll "miss" your value due to missing precision).

I'd do something like this (pseudo code):
Code: [Select]
float dx = 0, dy = 0;

...

if(key_left)
dx = max(dx - ax * frametime, -maxdx); // accelerate
else if(key_right)
dx = min(dx + ax * frametime, maxdx); // deaccelerate
else
dx *= pow(0.9, frametime); // slow down

if(key_jump && !falling && ! jumping)
dy = -djump; // jump
else
dy = min(dy + gravity * frametime, maxdy); // fall

sprite.Move(dx * frametime, dy * frametime)


To make sure you don't forget your time scaling, just think of the real value representations. A velocity is "distance/time", so you have to scale it once. A acceleration is "distance/(time*time)" so you have to scale the value twice in the end. Or in other words: Input some made up values with real si units, then see if you get some distance only - not a time or velocity

krrice

  • Newbie
  • *
  • Posts: 26
    • View Profile
Getting Sprite to Jump
« Reply #2 on: January 16, 2012, 04:37:37 am »
Quote from: "Mario"
Dont compare floats using equal (chances are good you'll "miss" your value due to missing precision).

I'd do something like this (pseudo code):
Code: [Select]
float dx = 0, dy = 0;

...

if(key_left)
dx = max(dx - ax * frametime, -maxdx); // accelerate
else if(key_right)
dx = min(dx + ax * frametime, maxdx); // deaccelerate
else
dx *= pow(0.9, frametime); // slow down

if(key_jump && !falling && ! jumping)
dy = -djump; // jump
else
dy = min(dy + gravity * frametime, maxdy); // fall

sprite.Move(dx * frametime, dy * frametime)


To make sure you don't forget your time scaling, just think of the real value representations. A velocity is "distance/time", so you have to scale it once. A acceleration is "distance/(time*time)" so you have to scale the value twice in the end. Or in other words: Input some made up values with real si units, then see if you get some distance only - not a time or velocity


Thanks for the reply.I have tried to change my code around but still cant get it right.I can get the sprite to jump up but not fall back down and Probably dont have the jump part just right,do you have a code snippet you can post or does anyone else have some info.I am new to SFML2 and any help would be appreciated.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Getting Sprite to Jump
« Reply #3 on: January 16, 2012, 05:51:24 am »
krrice, the only thing Mario didn't covered with his pseudo code (which was pretty great) was the jumping and falling checks on the last if. You just have to figure that out. It is not that hard to keep track of what is happening on the game and check later.

 

anything