Hi,
Im tooling around with a 2d platformer engine. I-ve just started to implement my camera - using float positions for basically everything. The problem is, when i try to use int positions (aka Rectangle instead of RectangleF or Points instead of Vector2) I cant seem to get “slow” movements to work. They keep “stutter”.
As seen in the example below - i use a small “inner camera” bounds that the viewport will follow instead of the actual player. This works fine, the inner bound (white area) can do smooth and exact movements. But when i pass the large “camera bounds” (viewport) to the DrawMethod as “DestinationRectangle” it wont be able to follow the inner bounds smooth movements. If you look carefully at the gif - you will see the tiles “stutter” when the camera slows down. I believe this is because I calculate all positions as floats - then i have to round the camera to a XNA Rectangle in order to pass it as Destination Rectangle. Is it because of my parsing from float to int this occurs?
Also, Im drawing with x4 scale.
Some code example:
if (_movementBounds.Center() != _player.Bounds.Center() && _player.CurrentState == Actor.State.Idle)
{
var dir = _player.Bounds.Center().X - _movementBounds.Center().X;
_movementBounds.X += dir * 0.05f;
}
var cameraWidth = _cameraBounds.Width / _scale;
_cameraBounds.X = _movementBounds.Center().X - (cameraWidth / 2f);
if (_cameraBounds.X < 0)
{
_cameraBounds.X = 0;
}
_camera bounds is actually a RectangleF at this point. Later on when passing it to my TMX lib in my draw method, I will have to round it:
Matrix transform = Matrix.CreateScale(4);
Map.InitObjectDrawing(GraphicsDevice);
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, transform);
_spriteBatch.Draw(_backGround, Vector2.Zero, Color.White);
_spriteBatch.Draw(_playerTexture, playerPos, Color.White);
_map.DrawLayer(_spriteBatch, 0, Utillities.Utilities.Round(_camera.CameraBounds), 0f);
Any help would be appriciated. It must be a way to have smooth and slow movements with XNA.Rectangle (or integers in general) withouth having this effect.