Skip to main content
GameDev.net gamedev.net
🔒 Locked

Moving a constant number of pixels per second

Started by Pasanova Sep 24, 2012 at 5:45 PM 5 replies 2.7k views
Original Post
Pasanova
Pasanova
Hello Everyone,

  • I update the position of my objects every time I go through my game loop
  • I have a function that calculates the number of loops per second, lets say there are 100
  • I want to move my object at a constant rate of 30 pixels per second (which is already quite fast)
  • This means that I should move my object 0.3 pixels per loop
  • This is a problem!

    How do you normally get around this? Thanks!
Pasanova
Pasanova
But surely you have to either move a pixel or not? You can't move
0.02 pixels?
jwezorek
jwezorek
First of all, I assume we are talking about a 2D game in which your objects are sprites, right? -- (because otherwise you wouldn't be talking about pixels).

So, yeah, use floats for position values like Cornstalks says above.

But what game framework or library are you using? Does it support drawing with subpixel accuracy? If not you have to round to the nearest pixel ... this tends to look jerky imho though. Basically do all your logic/updates in terms of floating point values and convert to the nearest pixel just before blitting. Or switch to a 2D framework that lets you draw to float locations.
Pasanova
Pasanova
Thanks smile.png Yes, its 2D I am using SDL. okay looks like
that is the way to go!

EDIT: Works a treat!
superman3275
superman3275
Move it using the frame time of your function.
That means that you multiply your speed variables by the frame time of your window. Since time is constant, it will move at the same speed on everyones screen.
EddieV223
EddieV223
When you place your object just cast the float to an int. When you cast it the decimal part will be ignored. So you can round it first if you wish, then anything over .5 should be +1.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.