Public Group

# Moving a constant number of pixels per second

This topic is 2211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
Use floating point data types instead of integers?

##### Share on other sites
But surely you have to either move a pixel or not? You can't move
0.02 pixels?

##### Share on other sites
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. Edited by jwezorek

##### Share on other sites
Thanks Yes, its 2D I am using SDL. okay looks like
that is the way to go!

EDIT: Works a treat! Edited by Pasanova

##### Share on other sites
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.

##### Share on other sites
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. Edited by EddieV223

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 9
• 10
• 13
• 19
• 14
• ### Forum Statistics

• Total Topics
632944
• Total Posts
3009356
• ### Who's Online (See full list)

There are no registered users currently online

×