screen coordinates w/centered player

Started by
2 comments, last by Splinter of Chaos 12 years, 7 months ago
Hi,

I'm making a pretty simple 2d space game. Since I wanted the physics to be reasonably accurate, I'm maintaining position with floating point numbers, then converting to integer screen coordinates at draw time. I also always want the player's ship to be centered on the screen.

Everything looks good except for when the player's ship and another ship are moving slowly at a very similar velocity. Then the other ship will bounce around a fair bit with respect to the player (obviously bounce around means move back and forth by a pixel). I've tried all sorts of combinations of floor and round, but I've had no luck keeping slow moving ships equidistant from each other (or at least not jittering like crazy).

Presumably this is because the ships hit the next floor/round threshold at different times. Is there any solution?

Edit: I should mention that I've tried one other method that solves this problem but introduces another. If I base the screen on the player's position and round everything with respect to it, then slow moving ships work, but then stopped ships don't stay equidistant from each other as the player flies by.
Advertisement
If possible, use integers for movement also, and always update at for example 60 fps. Many 2D games have used that approach, especially older games.
Another way is to use 3D or anti-aliased graphics that can be drawn at non-integer positions and interpolation. This doesn't work for pixel-graphics however, as pixels will be interpolated and blurred when the position isn't an integer.
How are you converting from float to int? If you just cast, you're not rounding but rather throwing away everything after the decimal point. My advice would be to round properly when converting to screen space.

To round properly:

int rounding(float p)
{
int q = (int)p; // get the int portion of p

if ( p - q >= 0.5f ) // compare the decimal portion of p to 0.5.
{
++q; // numbers with a decimal of .5 or greater get rounded up
}
return q;
}
Or more simply:

int round( float f )
{
return f + 0.5f;
}

This topic is closed to new replies.

Advertisement