Graphics Reference
In-Depth Information
v , needed to determine if the sphere is moving
toward the plane, reoccur in the expression for t and should not be recalculated in an
implementation. This intersection test can now be implemented as follows.
Note that the terms ( n
·
C )
d and n
·
// Intersect sphere s with movement vector v with plane p. If intersecting
// return time t of collision and point q at which sphere hits plane
int IntersectMovingSpherePlane(Sphere s, Vector v, Plane p, float &t, Point &q)
{
// Compute distance of sphere center to plane
float dist = Dot(p.n, s.c) - p.d;
if (Abs(dist) <= s.r) {
// The sphere is already overlapping the plane. Set time of
// intersection to zero and q to sphere center
t = 0.0f;
q = s.c;
return 1;
} else {
float denom = Dot(p.n, v);
if (denom * dist >= 0.0f) {
// No intersection as sphere moving parallel to or away from plane
return 0;
} else {
// Sphere is moving towards the plane
// Use +r in computations if sphere in front of plane, else -r
float r = dist > 0.0f ? s.r : -s.r;
t = (r - dist) / denom;
q=s.c+t*v-r*p.n;
return 1;
}
}
}
Here, the t value returned on a successful intersection may lie beyond the [0, 1]
interval. When this happens, t corresponds to the future time at which the sphere
would intersect the plane assuming the same motion. It is left to the caller to test t
for inclusion in the [0, 1] interval.
Just testing whether a moving sphere intersects a plane without computing where
the sphere strikes the plane is done with much less effort. The sphere intersects the
plane if it starts and ends on different sides of the plane or if it overlaps the plane at
its start or end positions. This test can be implemented as follows.
 
Search WWH ::




Custom Search