I'm making a 2D sidescroller in XNA and I'm having an issue with the way the camera follows the player. When the player walks left or right, and its walk speed is a decimal number, there's jitter.
This is what it looks like. I had to slow the game down to 50% speed for the gif to catch the jitter frames.
(Ignore the random vertical twitching, that's just some strange graphical bug that happens when I scale the backbuffer up too far.)
I believe the cause of this is the relative speed between the camera and the player.
In the gif, the player's speed is at 1.5, so it will alternate between moving one pixel and two pixels per frame. This causes a sort of desync between the player and camera movement which causes the jitter.
I need to be able to use decimal speeds because first of all, a speed of 1 is a bit slow for the player, and 2 is a bit fast, but also, if I add acceleration/deceleration the player's speed won't always be a whole number.
- Nothing is rounded in-code. All positions and velocities are floats. Rounding isn't needed since nothing can move in sub-pixels. I reduced the backbuffer resolution so changing an object's position by 1 will move it one pixel. Everything is drawn at integer positions, of course.
- It's not the update order. The cycle is simple right now. The player moves, then the camera is updated. Switching the order doesn't fix the issue.
- I remembered that Cave Story has nice and smooth camera interpolation, and large pixels, so I fired it up to see what it looks like, and I was really surprised too see that it suffers the same problem. For roughly a second after the camera starts moving, the player jitters by one horizontal pixel.
My camera movement interpolation code is this:
if (camPos != playerPos) camPos += (playerPos - camPos) * strength;
Any ideas on how to fix this? I'm sure SOMEBODY has had this issue before other than me (and Pixel).
If I need to include any more relevant code snippets, just let me know. I can't really think of any that would help at the moment.
Edited by Magdev, 23 May 2013 - 03:40 AM.