Game Development Reference

In-Depth Information

In order to handle mouse-clicking, the application must be able to read mouse click

events, extract the coordinate (X, Y), and perform an intersection test against a par-

ticular control to see if it was activated. Thankfully, this process is automatically

handled by Win32 user interface controls, but we are not so lucky in the 3D world.

This chapter will cover the math and implementation behind converting coordi-

nates in screen space to world space and performing intersection tests, otherwise

known as
picking
.

Transforming Screen Coordinates

Performing intersecting tests against Win32 controls is very simple, since the

mouse coordinates and the control bounds are both in screen space (X, Y).

Intersection tests from screen space coordinates to world space bounds (X, Y, Z)

require a bit of math since we do not know the relationship between the 3D object

and its projection. It is also important to state that we are using a left-handed coor-

dinate system, which is the default for Direct3D. OpenGL uses a right-handed

coordinate system, so the math and code will have to be adapted to get it working.

Take a look at the source code for the
gluUnproject
function of
OpenGL
if you are

unsure of how to do this.

Figure 27.1 shows the relationship between the origin of projection (screen space)

and the projection window (world space).

Figure 27.1
Relationship between the origin of project and the projection window.