PoulsQ
PoulsQ

Reputation: 2016

Sprite becomes blurred

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?

Upvotes: 7

Views: 1049

Answers (2)

Mbs
Mbs

Reputation: 1

The most easy is cast to (int)Position.X and (int)Position.Y when the movement button is released

Upvotes: 0

Lucius
Lucius

Reputation: 3745

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.

Upvotes: 5

Related Questions