Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

mouse detection

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?
Thanks for your patiance!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!