# Help with the object of the mouse algorithm

I wasn't sure what to call this. I'm building a tile based game where the user can click on a tile. it is a 2d c++ vector of tiles. right now I have an algorithm that positions them like this:

``````[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
```
```

they are positioned based on their size and my tile class holds each of their x and y coordinates, and I have 2 variables for the height and width.

Basically I'd like to find an algorithm that can return the tile my mouse is on based on the mouse's position.

For example: if my mouse (relative to the game window)'s position is 37,0 and each of my tiles is 10 * 10 in size and they are each tilewidth apart then by having my mouse over 37,0 it should return (4,1) meaning the 4th element on the x and the first element on the y. I thought about doing bounding box checks against each rectangle but I think that will use up too much cpu if i check 60 times / second.

Thanks

If you're not using an event based environment, you do need to code your own object intersection, mouse detection, etc.

Here's the formula in pseudo code for what you want to get:

``````tile_width = 10;
tile_height = 10;

x_tile = Math.ceil(mousex/tile_width);
y_tile = Math.ceil(mousey/tile_height);
```
```