Game Development Reference
In-Depth Information
Figure 15.5 shows a screen shot of the sample application for this chap-
ter. The teapot moves around the screen, and you can try to click on it
with the mouse. If you click on the bounding sphere of the teapot, a
message box will pop up indicating that you hit it. We handle the mouse
click event by testing for a WM_LBUTTONDOWN message:
case WM_LBUTTONDOWN:
// compute the ray in view space given the clicked screen point
d3d::Ray ray = CalcPickingRay(LOWORD(lParam), HIWORD(lParam));
// transform the ray to world space
D3DXMATRIX view;
Device->GetTransform(D3DTS_VIEW, &view);
D3DXMATRIX viewInverse;
D3DXMatrixInverse(&viewInverse, 0, &view);
TransformRay(&ray, &viewInverse);
// test for a hit
if( RaySphereIntTest(&ray, &BSphere) )
::MessageBox(0, "Hit!", "HIT", 0);
break;
15.6 Summary
Picking is the technique used to determine the 3D object that cor-
responds to the 2D projected object displayed on the screen that
the user clicked on with the mouse.
The picking ray is found by shooting a ray, originating at the origin
of the view space, through the point on the projection window that
corresponds to the clicked screen point.
We can transform a ray r ( t )= p 0 + t u by transforming its origin p 0
and direction u by a transformation matrix. Note that the origin is
transformed as a point ( w = 1) and the direction is treated as a vec-
tor ( w = 0).
To test if the ray has intersected an object, we can test if the ray
intersected a triangle that composes the object or test if the ray
intersects a bounding volume of the object, such as a bounding
sphere.
Search WWH ::




Custom Search