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

Author Topic: Sprite help  (Read 1250 times)

0 Members and 1 Guest are viewing this topic.

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Sprite help
« on: May 03, 2015, 12:12:42 am »
I'm only a newbie, but how could I make a sprite 'jump', where when a key is pressed it goes up then down? I'm not sure how one would do this (maybe theres an obvious method :P). I tried this code but it didn't really work...

if (sf::Keyboard::isKeyPressed(sf::keyboard::Space))
{
sprite.move(0, -0.5);
Sleep(1000);
sprite.move(0, 0.5);
}




 
I know it's obvious what I tried to do wouldnt work, but I'm a bit stuck on this :-/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sprite help
« Reply #1 on: May 03, 2015, 08:51:21 am »
In a nutshell:

You keep track of your sprites direction vector and when you detect that it should jump you change it to point in an upwards direction (apply an impulse by adding a new upwards pointing vector). Then you check how much time has passed since the last frame and move the sprite a corresponding distance along its direction vector and draw it there. Next frame you move it a little more and draw it, etc etc, until it reaches the top of the jump (which is nicely taken care of if you constantly apply a downwards gravity vector).

Some links that may help:

http://gafferongames.com/game-physics/fix-your-timestep/

http://www.koonsolo.com/news/dewitters-gameloop/

http://gameprogrammingpatterns.com/

http://www.redblobgames.com/

http://gamedev.stackexchange.com/questions/29617/how-to-make-a-character-jump

https://www.mathsisfun.com/algebra/vectors.html

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Sprite help
« Reply #2 on: May 03, 2015, 11:39:30 am »
Ok, I think I got it now thanks  ;)