How can I use SpriteBatch without pixel perfect

hello.

I had problem for SpriteBatch draw.

currently, SpriteBatch only can use Rectangle to using draw.
but Rectangle only using int types.

I want to make smoother positioning and scaling sprites.

Have any solution for this?

That can happen with vector2 positioning as well and even more so.

Simple answer…
The draw widths and heights must vary to fit i.e. contract or expand.

More formally…
For two sprites to adjoin pixel perfectly. The width of one sprite must be calculated from two found integer positions, that is after the floating point error has occurred.

For example.
You are drawing a grid or a row were tiles are calculated to be positioned via a index and tile.width such as.

positionRectangle.X = ix * tileWidth; // were ix is the variable in a for loop.
positionRectangle.Width = tileWidth;

This is were you would get floating point errors above.
To counter this you calculate the actual integer positions then find the width as shown below.

positionRectangle.Width = ( (ix + 1 ) * tileWidth) - (ix * tileWidth);

This accounts for the variations that arise from floating point errors by adjusting the actual visual width of each drawn tile or sprite in a contiguous set, by the start position of one tile to start of the next tile position.

Otherwise use Quads.

I think there are overloads for SpriteBatch.Draw that have floating point parameters for position and scale. Have you tried those?

Also, you don;t neccessarily have to use SpriteBatch. You could make your own sprite renderer. You just have to create the sprite geometry (quads) yourself, and draw them using a custom shader.

1 Like

Yeah, this is the way to go. I think the XNA developers wanted to keep things simple by sticking to 1 rectangle class. You can easily write an Extension method that takes a RectangleF destination and translates it to a Draw call with position and scale if that’s what you prefer.

1 Like