I’m using Vector2.Lerp to achieve a smooth camera, but it jitters with the players acceleration. I assume this is because the player moves one pixel, then the camera will catch up to that pixel a frame or so later. How do I avoid this?
Code: Vector2 entityPosition = new Vector2((int)(entity.position.X - Assets.gameWidth / 2 + entity.width / 2), (int)(entity.position.Y - Assets.gameHeight / 2 + entity.height / 2)); position = Vector2.Lerp(unRoundedPosition, entityPosition, 0.125f); unRoundedPosition = position; position = new Vector2((int)position.X, (int)position.Y);
You’re using a vector 2 to store the data, but you’re casting to an int. When you do this every frame you lose data for subsequent frames. Try only casting position in your draw where you actually use the camera to render your frame, in a new Vector2 object?
hmm, thanks for the response. Seems rather frustrating that nothing more can be done, it seems like a simple problem. I won’t mark this as the solution quite yet, in case someone has a ‘true’ fix, but thanks anyway
It was just a bit harder to follow without the complete code but I follow now.
What’s not shown here is how you’re scaling your scene. If you’re rendering the scene to a render target using the camera you configured, this issue makes sense because the render target can only render to pixel scale.
Maybe you could try rendering to a render target that is one pixel on all sides larger than your target resolution. Then calculating the sub-pixel camera offset and shifting your final render position (for the render target) by that offset.
It’s a bit of tom-foolery, but hopefully that makes sense. I suspect the problem is that when you’re calculating your camera position, you don’t actually have that sub-pixel data available to you. So you’ll need to do some work to preserve it and adjust your final render position accordingly.
EDIT: You might have to render this render target with the one pixel border to another render target that’s the correct size so you can clip the scene properly. Or mask it off in some other way.
You may find this article helpful. Something I wrote on lerping a while back. I use this technique a lot in Jetboard Joust, and for many things other than camera tracking.