I’ve encountered a small but annoying graphical glitch in my platformer’s engine.
I’m using Slick.
What happens is:
While the player is jumping and falling back down, moving the camera with it, the game’s map tiles sometimes display a slight flicker about 1 pixel high on their bottom side.
Currently, the player is a green rectangle which happens to be the same colour as this flicker, and experimenting with changing the colour of the player has confirmed that it is definitely a small part of the player sprite “bleeding” through. I’ve made a simple diagram to help picture it better. It does happen on the white squares too, it’s just not as noticeable as the brown ones. Also, for some reason it seems as though this problem isn’t affecting some brown tiles, seemingly corner tiles but that’s probably not important:
http://img51.imageshack.us/img51/771/platformflicker.png
What I think is happening:
Through a bit of experimentation and thinking the problem through, I’ve determined the problem seems to come from the use of floating points to store position/velocity etc. which obviously must result in partial pixels. The main culprit appears to be the addition of the gravity value (multiplied by delta), which is the very small and exact value of 0.00075f when the player leaves the ground. Given the player’s position is now being altered by tiny amounts, which then causes the camera to instantly snap based on the player’s location, which then causes the game world’s objects to be translated relative to the camera’s rather exacting floating point coordinates… I imagine that’s causing a problem for the tiny 8x8 map tiles, screwing up a line of 1 pixel or so.
As an experiment… I kept everything the way it was, but I rounded the player’s drawing coordinates to an int, then did the same thing to the camera coordinates when being used to translate the game world w.r.t. the camera. That seemed to completely get rid of the problem.
My question:
Now, I’m just wondering… I suppose I have to use floats for object coordinates if I want to use gravity like I am while using a framerate independent of logic updates and the idea of using floats instead of ints seems like a useful one, so what should I do to prevent this flickering?
Should I simply keep logical coordinates as floats the way they are, and then cast them to int when it comes to rendering? Is that good practice, or a bit sloppy and error-prone? Or is there something else that would be better?