# Tile coords to world point

## Recommended Posts

How can I convert tile x & y to world point x, y, z?

##### Share on other sites

I can't find an actual function to do this, but try searching for the word "TILE_SCALE" in the game code (without the quotes). You'll get a lot of lines like this

```local x = (posX - mapWidth/2.0)*TILE_SCALE
local z = (posY - mapHeight/2.0)*TILE_SCALE```

##### Share on other sites

Would I use TheWorld.Map:GetSize() for map width & height?

##### Share on other sites

Probably

This line appears all over the place around the word "TILE_SCALE"

`local map_width, map_height = TheWorld.Map:GetSize()`

##### Share on other sites

I've just tried and it works perfectly, thanks so much.

To get a random point instead of the tile center would I use + math.random(-(TILE_SCALE -1), TILE_SCALE -1) ?

##### Share on other sites

Since it probably goes in both directions from the middle of the tile, I think you need to halve the scale.

```local HALF_TILE_SCALE = TILE_SCALE * 0.5
+ math.random(-(HALF_TILE_SCALE -1), HALF_TILE_SCALE -1)```

Btw, if you didn't know, Lua's math.random is inclusive of both given values, so

`math.random (4, 8)  --> (a number from 4 to 8)`

So you only need the -1 if you don't want the resulting position to be at the edges of a tile.

##### Share on other sites

This all works, thank you very much!

##### Share on other sites

You are very welcome