Sign in to follow this  
Pasanova

Moving a constant number of pixels per second

Recommended Posts

Hello Everyone,[list]
[*]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!
[/list]
How do you normally get around this? Thanks!

Share this post


Link to post
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 this post


Link to post
Share on other sites
Thanks [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Yes, its 2D I am using SDL. okay looks like
that is the way to go!

EDIT: Works a treat! Edited by Pasanova

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this