Andy Latham
Andy Latham

Reputation: 387

Smooth text, jittery sprites

with Monogame I am making a game that draws both graphics and text to the screen, with position modified by a camera matrix. If I set the position of text to the camera position, everything is fine, but when I set sprites to the camera position there is very noticeable jittering as the camera moves. I think this is because graphics are drawn with rectangles which require integer positional values. I guess text has no such requirement though.

How do I get my graphics to follow the camera movement smoothly like the text does?

If it's of use, this is my spriteBatch.Begin() call:

spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearClamp, null, null, null, camera.GetTransformation(graphics));

And this is my camera transformation:

public Matrix GetTransformation(GraphicsDeviceManager graphicsDevice)
        {
            Vector3 newVector = new Vector3(-GameInfo.info.cameraPosition.X, -GameInfo.info.cameraPosition.Y, 0);

            cameraTransformMatrix = Matrix.CreateTranslation(newVector) *
                                    Matrix.CreateRotationZ(rotation) *
                                    Matrix.CreateScale(new Vector3(zoom, zoom, 1)) *
                                    Matrix.CreateTranslation(new Vector3(GameInfo.info.resolutionWidth * 0.5f, GameInfo.info.resolutionHeight * 0.5f, 0));

            return cameraTransformMatrix;
        }

Thanks!

Upvotes: 0

Views: 147

Answers (1)

Andy Latham
Andy Latham

Reputation: 387

I have managed to fix the problem by using a different overload of SpriteBatch.Draw() which doesn't rely on a destination rectangle:

spriteBatch.Draw(texture, position, rSpriteSourceRectangle, Color.White);

For reference, this was my old one:

spriteBatch.Draw(texture, new Rectangle(drawX, drawY, frameWidth, frameHeight), rSpriteSourceRectangle, Color.White);

Upvotes: 0

Related Questions