Game Development Reference
In-Depth Information
The D3DXVec3TransformCoord and D3DXVec3TransformNormal
functions take 3D vectors as parameters, but observe that with the
D3DXVec3TransformCoord function there is an understood w =1
for the fourth component. Conversely, with the D3DXVec3Trans-
formNormal function there is an understood w = 0 for the fourth
component. Thus, we can use D3DXVec3TransformCoord to trans-
form points, and we can use D3DXVec3TransformNormal to
transform vectors.
15.4 Ray-Object Intersections
After we have the picking ray and the objects in the same coordinate
system, we are ready to test which object the ray will hit. Since we rep-
resent objects as triangle meshes, one approach would be the following.
For each object in the scene, iterate through its triangle list and test if
the ray intersects one of the triangles. If it does, it must have hit the
object that the triangle belongs to.
However, performing a ray intersection test for every triangle in
the scene adds up in computation time. A faster method, albeit less
accurate, is to approximate each object with a bounding sphere. Then
we can perform a ray intersection test with each bounding sphere, and
the sphere that gets intersected specifies the object that got picked.
Note: The picking ray may intersect multiple objects. However, the
object closest to the camera is the object that was picked, since the
closer object would have obscured the object behind it.
Given the center point c and the radius r of a sphere, we can test if a
point p is on the sphere using the following implicit equation:
p
c
r
0
where p is a point on the sphere if the equation is satisfied. See Figure
15.3.
Figure 15.3: The length of the vector formed
by p - c , denoted by
, is equal to the
radius of the sphere if p lies on the sphere.
Note that we use a circle in the figure for
simplicity, but the idea extends to three
dimensions.
p - c
Search WWH ::




Custom Search