# Get position of tiles in a grid

So I have a 7x7 grid made using a for loop and instantiating the sprites. Now via code, let us say l placed my rectangular sprite,which takes 3 cells on the grid, somewhere on that grid. How can I get the position of the cells on which this rectangular sprite is on?

• Show us your for loop. – DMGregory Aug 10 '19 at 13:35

I will answer for an item that takes 1 cell in the grid. Given the world coordinates (x, y) of the center of this item, you can get the grid coordinates (a, b) by doing the following calculation:
a = ceil((x / cellSize))

I'm assuming that the cells are square and that their side is equal to cellSize
You can calculate the grid coordinates of the left part of your rectangle. You will then know that the cell of coordinates (a, b) is occupied. By consequent, the cells of coordinates (a + 1, b) and (a + 2, b) are occupied too.