Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite becomes blurred

Tags:

c#

xna-4.0

I am starting learning C# and XNA, and I want to display an animated sprite (moved by my keyboard).

I've got this sprite file:

basic sprite

To display only the part I need, I use this code:

Rectangle cuttedSprite = new Rectangle(
    this.W * (int)this.mCurSprite.X, 
    this.H * (int)this.mCurSprite.Y, 
    this.W, 
    this.H
);
spriteBatch.Draw(this.mSpriteTexture, this.mPosition, cuttedSprite, Color.White);

But my problem is that the rendered image is blurred after moving:

blur problem

I tried to fix this by changing the SamplerStates, but nothing changed. Does anyone have an idea to help me?

like image 583
PoulsQ Avatar asked Feb 11 '13 21:02

PoulsQ


1 Answers

Round the position of the sprite to the nearest integers.

If the destination rectangle of the sprite is offset by less than a pixel, the sampler in the pixel shader will calculate the color by interpolating between the neighbouring pixels.

Another option is changing the filter method of the sampler to nearest-neighbour interpolation. You can do that by specifying a SamplerState.PointWrap or SamplerState.PointClamp when calling SpriteBatch.Begin.

like image 111
Lucius Avatar answered Sep 28 '22 08:09

Lucius