Sprite becomes blurred
Asked Answered
F

2

7

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?

Formwork answered 11/2, 2013 at 21:43 Comment(2)
Please try it with a SamplerState.PointWrap. Also, since mPosition is probably a Vector2, try rounding mPosition.X and mPosition.Y to the nearest integers.Spikenard
You were right ! I tried to display my image on a none entire pixels when i move in diagonal ... Ok post this answer and i valid it !Formwork
S
5

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.

Spikenard answered 11/2, 2013 at 22:25 Comment(1)
Depending on how you're handling things, you may want to perform the rounding only for rendering purposes. If you're using physics, for example, you want to leave the precise coordinates in the solver otherwise it's going to try and correct for them repeatedly, so only round the actual draw call.Steno
K
0

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

Kythera answered 22/9, 2013 at 21:31 Comment(1)
The question was answered almost a year ago, no need to bump it unless there's something new to addTabernacle

© 2022 - 2024 — McMap. All rights reserved.