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

Author Topic: KeyPressed Events Not Lining Up  (Read 1426 times)

0 Members and 1 Guest are viewing this topic.

Yaikwhed

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
KeyPressed Events Not Lining Up
« on: May 05, 2015, 07:13:07 pm »
Being new to SFML, my friend recommended my first step being making a simple pong game. I've been trying to figure it out on my own using only the SFML tutorials from the Learn section of the website, but I've run into an issue and I can't find a solution to it in all my googling. I'm trying to make both paddles movable to make it a 2-player game, but for some reason the paddles move at different speeds. The Top paddle is bound to Left and Right to move 10 pixels in either direction, and the bottom paddles are bound to 'A' and 'D' and are set to move the same distance, however they move twice as far on a single key press. When both paddles are moving at the same time, they move the same speed. It's only when they're moving at different times that the bottom paddle moves much quicker. Anyone have any words of insight for a newbie on the matter? This is what I'm using for the movement.

(click to show/hide)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: KeyPressed Events Not Lining Up
« Reply #1 on: May 05, 2015, 07:16:36 pm »
You need to take into account the frame time (and have a timestep in general).

http://gafferongames.com/game-physics/fix-your-timestep/
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: KeyPressed Events Not Lining Up
« Reply #2 on: May 05, 2015, 07:17:53 pm »
Speed is distance per time, so what time reference do you use? In other words, during which time does a paddle move 10 pixels?

This should be a fixed time amount, otherwise you won't be able to guarantee consistent speeds. A simple solution can be to use sf::RenderWindow::setFramerateLimit() or sf::RenderWindow::setVerticalSyncEnabled(), for more sophisticated time management you could have a look at fixed time steps.

See also here.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Yaikwhed

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: KeyPressed Events Not Lining Up
« Reply #3 on: May 05, 2015, 07:55:28 pm »
Thank you all for the help. Now, though, it works if I use keys other than Left and Right for the top panel (I changed it to numpad 4 and numpad 6 and the pads travel the same speed) but it's still not working correctly for Left and Right. Does SFML maybe just handle these keys differently?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: KeyPressed Events Not Lining Up
« Reply #4 on: May 05, 2015, 08:15:16 pm »
Now, though, it works if I use keys other than Left and Right
What does that mean? If you don't use a fixed time, then no, it won't work. It may appear to work in your specific scenario, but at the latest when the game is played on a different computer, you will get problems.

You really have to fix this, we haven't said it just for fun ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: