What's a good way to check that a player has clicked on an object in a 3D game?

Posted by imja on Game Development See other posts from Game Development or by imja
Published on 2011-03-12T19:05:57Z Indexed on 2011/03/13 0:19 UTC
Read the original article Hit count: 188

Filed under:
|
|

I'm programming a 3D game (using C++ and OpenGL), and I have a few 3D objects in the scene, we can say they are boxes for this example. I want to let the player click on those boxes to select them (ie. they might change color) with the typical restriction like if more than one box is located where the user clicked, only the one closest to the camera would get selected.

What would be the best way to do this? The fact that these objects go through several transforms before getting to window coordinates is what makes this a bit tricky.

One approach I thought about was that if the player clicks on the screen, I could normalize the x,y coordinates of mouse click and then transform the bounding box coordinates of the objects into clip-space so that I could compare then to the normalized mouse coordinates. I guess I could then do some sort of ray-box collision test to see if any objects lie as the path of the mouse click. I'm afraid I might be over complicating it. Any better methods out there?

© Game Development or respective owner

Related posts about c++

Related posts about 3d