P-155: Phoenix Engine Update 7: Click Detection
I have successfully implemented click detection and object selection in the game engine.
Originally, I used the “official” method of first creating a mouse callback, and then using code to select the objects close to the position that the player clicked. I used glLoadName() to identify the world objects. This didn’t work well at all, I would guess because the code was not able to precisely determine a vector from the 2D screen coordinates into the 3D world. It used glPerspective() to create a narrow view frustum around the players click coordinates, and returned the glLoadName() ids of all of the objects inside. However, that view frustum would often return the wrong object, or a large number of objects in the region of the mouse click.
I then implemented what sounded like a much less effective method, but which ended up working out much better. This method relies on assigning every object in the scene a unique colour value, getting the colour value of the pixel that the player has clicked on, and then looking up that colour value to get the id of the object that has been selected. The player never sees the colour values changing, since you don’t call swapbuffers(), sit it is invisible. It is supposed to be quite a slow technique, but it seems to be working well. THIS link provides information on this technique.
I can now easily select unique objects in the scene.
