View Full Version : A simple trick

06-14-2002, 09:18 AM
I have a problem and am not able to figure out the solution. I have a model which has a large number of cubes, the number and position of which are determined only at run time. After displaying the cube, on detecting a mouse-click, I want to determine the cube on which the mouse was clicked. How do I do this? I am using the QT libraries as the GUI, and the only help that QT provides is - detection of the mouse-click and the x-y co-ordinates of the click.
Are there any functions which would return the world co-ordinates, given the x-y on the screen, or is there a more easier workaround?

06-14-2002, 11:42 AM
Well, you could estimate where the mouse in in the world by starting with the position of your camera. Then you would compare the mouse coordinates in the window to the width and height of your frustrum in world units (I'm pretty sure, but this is just off the top of my head).

Well, depending on how large the number of cubes you have is... as long as you have the coordinates of their faces vertices you could cast a ray from the mouse position in the world in the direction of (rougly) the camera's orientation. There's some good tutorials at flipcode.com about ray casting, particularly in their collision detection tutorials.

Not sure if this is the optimal way to do it, but it would work I think.

ALSO-- an easier method might be to use OpenGL "picking", where you can assign IDs to objects you draw. OpenGL does the ray casting for you. Look into that if you haven't already.