Mouse picking and tile maps

Recommended Posts

Hi, I'm currently working on a new game which will be isometric turn based strategy. I'm looking around for some effective code to help me determine what tile has been clicked on. My tiles are the usual quad based type. I've had a look at gluUnProject but I'm not too sure of how to deal with the data that is returned. Does anyone have effective code I could look at or borrow..? Cheers

Share on other sites
[google] knows everything. There's loads and loads of tutorials on isometric tilemaps out there.

Share on other sites
Quote:
 I've had a look at gluUnProject but I'm not too sure of how to deal with the data that is returned.

Could you elaborate a bit more on what you are having trouble with. Is it using gluUnProject or is it what to do with the object coordinates once you have them? If it is the former then this is an example of how to use gluUnProject to get some obect coordinates. It uses GLUT and gets the object coordinates on the near viewing plane of a mouse click.

void mouse(int button, int state, int x, int y){    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {        GLdouble modelviewMatrix[16];        GLdouble projectionMatrix[16];        GLint viewport[4];        GLdouble objX, objY, objZ;                glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);        glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);        glGetIntegerv(GL_VIEWPORT, viewport);                        gluUnProject((GLdouble) x, (GLdouble)y , 0.0, modelviewMatrix, projectionMatrix,                 viewport, &objX, &objY, &objZ);                printf("Left click at windows coordinates: %i, %i\n", x, y);        printf("Object coordnates %lf, %lf, %lf\n", objX, objY, objZ);    }}

If it is the latter then here are some pointers. Assuming that your game is a fully 3D tilebased game then one of the methods to find the clicked tile is as follows:

1. Find the object coordinates of the mouse click on the near plane (point A).
2. Find the object coordinates on the far plane (point B).
3. Use A and B to calculate a vector V.
4. Point A (or B) together with V constitute a line L.
5. For each tile T calculate where L intersects the plane that T lies upon. Call this point P
6. Test whether P is within T. If it is then T has been clicked on [*]

This method assumes a level of generality that your tilemap may not have. There are simpler methods if you have a camera that cannot be rotated at all, if your tiles are all of one height or if you have no zoom facility.

[*] This assumes that you have restricted the viewing options so that it is not possible for one tile to be hidden behind another. If you haven't then you need to find the closest tile (that intersects with L) to the camera.

Share on other sites
Cheers, that's very clear.

Right now the camera cannot be rotated but you can change the angle of the map a certain amount (not so much so you can hide tiles though).

I've read about grabbing the Z depth using glReadPixels which I'm thinking may help with determining tiles and other items.

Thanks for the point about the lines and vectors, this seems like the best route to take for my project. :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
628277
• Total Posts
2981770

• 10
• 11
• 17
• 10
• 9