Game Development Reference
In-Depth Information
r 1
r 2
+
+
d
Figure 6-10. Collision between spheres is a function of the distance between their centers.
The collision determination is simple if the class that represents the sphere declares fields
that store the location of the center of the sphere. In terms of computer code, the conditional
statement to evaluate is the following:
if ( d <= r1 + r2 ) {
// Collision has occurred.
}
It's also straightforward to determine whether a sphere has collided with a horizontal or
vertical surface. If the distance from the center of the circle to the surface is less than or equal
to the radius of the sphere, then a collision has occurred.
Another basic geometrical shape that is fairly easy to model is a cylinder. Consider the
vertical cylinder shown in Figure 6-11. To determine whether another object collides with the
cylinder, the circular criteria apply, but only over the vertical length of the cylinder. If the
distance in the x-y plane from the long axis of the cylinder to the surface of another object is
less than or equal to the radius of the cylinder, then a collision has occurred if the encounter
takes place within the vertical length of the cylinder.
z
y
x
Figure 6-11. Determining the collision with a cylinder uses the circular criteria.
The final shape we will consider in this section is a parallelogram. Once again, it is helpful
to define whether a collision occurs relative to the center of the parallelogram. If the normal
distance from the center of the parallelogram to the surface of another object is less than or
 
Search WWH ::




Custom Search