Game Development Reference

In-Depth Information

internal struct PickingRay

{

internal Vector3 Origin;

internal Vector3 Direction;

public PickingRay(Vector3 direction, Matrix transform)

{

Origin = new Vector3(0.0F, 0.0F, 0.0F);

Direction = direction;

Origin.TransformCoordinate(transform);

Direction.TransformNormal(transform);

Direction.Normalize();

}

}

Note

Vector3.TransformCoordinate()
is used to transform points because it sets the fourth com-

ponent to
W = 1
, whereas
Vector3.TransformNormal()
is used to transform vectors because

it sets the fourth component to
W = 0
.

Bounding Sphere Intersection Tests

At this point, we are converting screen space coordinates to world space and com-

puting a ray that will be used for picking. Intersection tests will be performed

against objects in the scene using the computed ray to determine which objects the

user has selected.

In order to perform intersection tests, we need a 3D shape to test against.

Bounding spheres are common because their approximated nature makes them

fast to compute and use. Each object is represented by a bounding sphere that

describes the approximated volume of the object.

A sphere is represented by its center
c
and its radius
r
. Points can be tested for

whether they belong to a sphere if their distance from the center is equal to the

radius, shown by the following equation.

Intersecting a ray with a sphere can be found with the following equation, where

p
is substituted with

to represent the ray.