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

Author Topic: Smooth movement in SFML 2.0?  (Read 3704 times)

0 Members and 1 Guest are viewing this topic.

sircuddles

  • Newbie
  • *
  • Posts: 10
    • View Profile
Smooth movement in SFML 2.0?
« on: March 15, 2012, 10:01:02 am »
I'm a beginner and I'm having some trouble getting smooth movement in SFML.  I made a Pong game Allegro, and movement was handled as follows.  keyState being an array of bools and keypressed being an enum with an element for each key.  (sorry if this pseudo-code is terrible)

Code: [Select]

Poll for events {
    if key is down, keyState[keypressed] = true;
    if key is released, keyState[keypressed] = false;
   
    if (timer ticks) {  // (timer was set to 1.0 / 60)
        redraw = true;
        handle player movement based on keyState[keypressed] values;
    if (redraw && event queue is empty)
         redraw = false;
         draw everything;
}

clear display;
flip display;


This resulted in the timer ticking 60 times per second, redrawing at 60 FPS and handling the movement smoothly.  With SFML 2.0, I'm having troubles replicating what I did.  I've read about time-based movement (i.e move by x * deltaT), but for some reason the implementation and logic behind it is completely eluding me.  

Right now I have movement handled within the event polling, and it moves fine, but there is a delay if you hold the key down between the first movement and the repeated movement, and it's jumpy.

Can anyone help explain how to handle smooth movement in SFML 2.0?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Smooth movement in SFML 2.0?
« Reply #1 on: March 15, 2012, 10:11:55 am »
Use sf::Keyboard::isKeyDown instead of events.
Laurent Gomila - SFML developer

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Smooth movement in SFML 2.0?
« Reply #2 on: March 15, 2012, 05:29:45 pm »
the way I did it was make two bools, IsMoving and IsMovingUp, and then change them based on key presses in pollEvent, then outside that you make it move based on speed multiplied by the time since the last frame or however you are doing it, this makes it smooth since it doesn't have to recheck all events each frame to make it move.