# Why are there lines in between my tiles?

I'm making a top down XNA game. The maps are created by tiles. A problem I recently noticed is a 'grid' of lines between the tiles. It only shows up sometimes and only depending on the camera position. I let XNA handle the positioning, why are these lines showing up?

Here is a picture with the lines:

However, after moving my camera over just a bit, it magically goes back to how it is supposed to appear:

Thanks for any help!

EDIT:

This is the best I can do for source code, this is an extremely large project and the rendering is done it multiple areas:

Main Render Loop For dynamic elements such as tiles:

    spriteBatch.Begin(SpriteSortMode.Deferred, Nothing, SamplerState.LinearClamp, Nothing, Nothing, Nothing, Matrix.CreateTranslation(CameraX, CameraY, 0))
Mode.Render(spriteBatch, False)
For Each j As [Delegate] In DynamicInjectionList
j.DynamicInvoke(spriteBatch)
Next
DynamicInjectionList.Clear()
spriteBatch.End()


Mode.Render:

For x As Integer = 0 To GRIDMAX - 1
For y As Integer = 0 To GRIDMAX - 1
For z As Integer = 0 To 2
Dim ter As TiledTerrain = Terrain(x, y, z)
If ter IsNot Nothing Then
sb.Draw(CBaseClient.TextureLibrary("TileMap"), New Rectangle(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE), New Rectangle(ter.Texture_X, ter.Texture_Y, GRIDSIZE, GRIDSIZE), Color.White)
End If
Next
Next
Next


TiledTerrain.vb Class:

Imports ProtoBuf

<ProtoContract()> _
Public Class TiledTerrain

<ProtoMember(1)> _
Public Property X As Integer
<ProtoMember(2)> _
Public Property Y As Integer
<ProtoMember(3)> _
Public Property Z As Integer

<ProtoMember(4)> _
Public Property Texture_X As Integer
<ProtoMember(5)> _
Public Property Texture_Y As Integer

End Class


My Research:

I've done some searching into it and this is was I've found:

• This problem is called tearing (I believe?)
• It is commonly caused by using a floating point data type to hold the tile position (I'm using Integer so this shouldn't be a problem right?)
• How do you draw your tiles? Paste your drawing code and all other relevant code, we seriously need more information :) Problem could possibly have something to do with rounding errors/precision. – user9790 Mar 7 '12 at 1:32
• @JaakkoLipsanen, There, I've added some code :) – Freesnöw Mar 7 '12 at 1:39
• There was just recently a question on a similar topic that may be worth reading too. – Andrew Russell Mar 7 '12 at 3:49
• While the question about lines between your tiles has been answered, I noticed there are also lines on your tiles (i.e. bad grass texture). If you want to fix that too, read this: gas13.ru/v3/tutorials/sywtbapa_almighty_grass_tile.php – Dan May 15 '17 at 15:19

I assume that although your tile positions are defined in integers, your camera position may not be. So if you are trying to render pixel-perfect positions from a non-pixel-perfect position then the sampling may be off and cause lines between the tiles. In your SpriteBatch.Draw calls, I would suggesting changing the SamplerState to PointClamp to ensure that the textures are sampled on a point basis and it should remove those extra lines.

spriteBatch.Begin(
SpriteSortMode.Deferred,
Nothing,
SamplerState.PointClamp,
Nothing,
Nothing,
Nothing,
Matrix.CreateTranslation(CameraX, CameraY, 0))


I had a similar problem with DirectX 11 and this worked for me:

I changed the texture address mode in the sampler state description. Texture address mode is used to control what Direct3D does with texture coordinates outside of the range (in DirectX 11, this is [0.0f, 1.0f]). I changed the ADDRESS_U, ADDRESS_V, ADDRESS_W members to D3D11_TEXTURE_ADDRESS_CLAMP which clamps out-of-range values of texture coordinates into the range.

If there is an analogous parameter in your code than this might help.

I had a similar problem with lines showing up between my tiles too, but changing the sampler state to Point Clamp did not fix the problem.

Instead I found that turning off multi-sampling and synchronizing the vertical trace fixed the problem. This is in XNA 4.0:

public GraphicsDeviceManager _graphics;

_graphics = new GraphicsDeviceManager(this);
_graphics.PreferMultiSampling = false;
_graphics.SynchronizeWithVerticalRetrace = true;


Hope this helps someone out there!