2D camera issue


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);

        _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.

I can assure you that I have smooth motion of camera and everything in my game, so you can at least rest easy knowing monogame can deliver in this area :slight_smile:

Also, I have had stutter in a previous game… And in that case it had to do with moving integer-rectangles with big-texture moving back-grounds.

Have you tried handling everything update-wise using floats, and then just setting your rectangle positions from those floats? … That way the float retains small values that still add up over time, which is lost once you round off to an int… Once you do your math with floats, you can then DRAW on Integer postions, since your screen has pixels anyway.

I will try. I thought it was that I was doing already? By rounding my rectangleF to a xna rectangle just before the draw.

Also, I’m using quite low pixels 16x16, scaled up to 64x64

Dude, it worked! I changed draw method to the one using Vector2 as position and also taking in a “sourceRect”. Thanks!

1 Like