Search code examples
graphicsxnatexture2d

Texture appears to be shifted when drawn on screen (XNA)


Why do my texture's edges contain unwanted colored lines? Texture looks shifted by a part of a pixel.


Solution

  • Texture2ds can be seen as shifted or misplaced sometimes when you're not drawing the whole texture, but just a part of it via SourceRect parameter and the texture's position (Vector2) has nonintegral coordinates. It may look like undesired texels showing at its edges.

    If you have a texture with 1px purple border, the actual image can appear with sligthly purple edges. You can avoid that by making the texture coordinates integral.

    If this code causes trouble…

    Texture.Position.X = 4.9876f; // 4.9876f is an example of actual value
    Texture.Position.Y = 5.1234f;
    

    …try adding a cast:

    Texture.Position.X = (int)4.9876f;
    Texture.Position.Y = (int)5.1234f;