# Monogame / XNA - Is there a limit when drawing at far position (> 1 billion ? = strange behavior)

So I'm making an "unlimited" 2D Tile-Engine with procedural generation (simple noise based) in Monogame, and I'm loading/unloading Chunck by Chunck as you go along. It's working pretty well now.

Only problem I encounter when I'm positioning my character at a high value like for example int.Maxvalue / 2 (and higher), drawing tiles and every sprites from a high Position becomes all glitchy. Tiles take random sizes with a higher width and height (see pictures).

Drawing tiles :

 spriteBatch.Draw(
textToDraw,
new Vector2(i * TILE_SIZE, j * TILE_SIZE),
new Rectangle(0, 0, TILE_SIZE, TILE_SIZE),
drawColor,
0,
new Vector2(0, 0),
1f,
SpriteEffects.None,
depth
);


Camera Transform :

public Matrix Transform(GraphicsDevice graphicsDevice)
{
m_Transform =
Matrix.CreateTranslation(new Vector3(-m_CameraPosition.X, -m_CameraPosition.Y, 0.0f)) *
Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f , graphicsDevice.Viewport.Height * 0.5f , 0.0f)); // on redivise par 0.5 à cause du renderTarget 720x450

return m_Transform;
}


Begin spriteBatch :

matrix = this.Manager.camera.Transform(this.GraphicsDevice);
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
SamplerState.PointClamp,
null,
null,
null,
matrix
);


Drawing at position x = 1 million, y = 1 million : Drawing at position x = 1 billion, y = 1 billion: Red tile is a random tile I draw in another color to be able to get the size. But all the tiles are glitchy.

To isolate the problem as it didn't seem to come from my code, I tested in an Internet XNA sample I modified (From xnagpa.net "Eyes of the Dragons" samples) working with a similar Camera and tile engine. I forced the Camera position to a high value (x = 1 billion, y, 1 billion) and drawing Tiles on the screen at that position. Same problem. So my newb guess is it has to do with calculation of the Matrix, but after that it gets too complicated for me.

Anyone had the problem ? Can confirm it is a limitation of some sort. I don't think it can be fixed, but I'm trying to determine what is the "limit" of my "unlimited world".

Code is from my code base, pictures are from the example I cited and modified

This is a floating point issue and yes, you will get artifacts at very large values. The reason is because as the size of the value to the left of the decimal in a floating point number increases, you lose precision on the right side (and vice versa).

The fix is to change your point of origin so that your offset positions are always within an acceptable range. I had to do this for a space simulator.

EG. With my space simulator I used multiple points of origin depending on the proximity of the objects. Either using a planet, the sun or center of the galaxy as the origin with different scales for related objects. This allowed me to work in centimeters, kilometers and lightyears respectively.

Another possibility to help alleviate the problem without having to move your origin, is to use doubles instead of floats but you will eventually still run into the same problem but at a much greater distance.

One more option is to used fixed-point numbers or a big-number library but you will still need to do the matrix and vector math with floats/doubles, so it can get a bit complicated that way.

• That was my guess that it had something to do with floating point, heard several developers having difficulties with them. Thank you for the explanation, it's much clearer now.
– Giw
Aug 24 '15 at 22:05