# mouse detection

This topic is 4697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the best way to tell what tile the mouse has clicked on in a 3D tile based map? The only thing I could think of was giving a color to each tile and then rendering the colored map and seeing what color was pressed on. Does anyone have better methods? Thanks.

##### Share on other sites
Well, if you use the depth buffer you can use something like gluUnProject() (since it seems you're using OpenGL from your other posts). With that you can probably pretty easily map the 3D coordinate to a tile.

##### Share on other sites
But even if I find the 3D coordinate for the mouse position, how do I relate that to a certain tile?
Thanks.

##### Share on other sites
Seeing as you've said nothing about your tile system it's hard to say. I don't know if you're using square tiles, triangular tiles, hexagonal tiles, rectangular tiles, pentagonal tiles or tesselated cow tiles. I don't know if you have tiles set in a single flat layer, multiple parallel equidistant flat layers, a single layer with sloped tiiles, multiple sloped tile layers or a tile projection as a surface of a klein bottle.

##### Share on other sites
I have a 128X128 square tile map, the map is implemented in 3D with a hight map, the x and z coordinates are the coordinates of the actual tiles, and the y axis is the height. I hope that I made myself clear, please ask if I didn't.
Thanks.

##### Share on other sites
Think about it. You can use gluUnproject() to get the x, y and z coordinates from the depth buffer and mouse position. Your tiles are defined by their x and z coordinates.

##### Share on other sites
So I have to search through all of the tiles untill I find one with the right coordinates? My tiles are represented by an array (map[128*128]). To draw it I'm just getting the y from the height map and the (x,z) by looping through all the rows and multipying the number by a constant. For example, the (x,z) of tile map[5*120] would be (a*5, b*120). Should I de drawing it another way? Sorry for not catching on.
Thanks.

##### Share on other sites
What do you get if you divide the x and z of the returned spatial coordinates by the size of your tiles?

##### Share on other sites
Oh I see, I should divide the x and z by the constants and then round off to the nearest one? Can you confirm that?
Thanks.

##### Share on other sites
Ok, so now I tried the code: (In the test I'm drawing the map along the x,y coords)

GLdouble modelMatrix[16];GLdouble projMatrix[16];GLint viewport[4];GLdouble objx,objy,objz;glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX,projMatrix); glGetIntegerv(GL_VIEWPORT,viewport);  gluUnProject( mouse_x, 480-mouse_y, 0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);

to draw my map from the map[i*j] in 3D I do:
for(i=0;i<MAP_SIZE;i+=1)    for(j=0;j<MAP_SIZE;j+=1)   {    glLoadIdentity();    glTranslatef(-0.55 + 0.01*i, -0.55 + 0.01*j,-1.5f);    Object(0.005,0.005);  //draw square   }

but when I put the mouse over for example the (0,0) part (bottomleft) of the map, the mouse coord is (108,29) and for the 3D coord (after adding 0.55 and divideing by 0.01 both the x and y) I get (73.41,73.58,0.5).
First of all, why do I get such big numbers? Also, why 0.5?
And another question - If I call a glLoadMatrix() after drawing the map will it mess things up?

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633698
• Total Posts
3013412
×