In a real-time environment such as an arcade game like
Asteroids, it is imperative that
the world is processed as quickly as possible.
This means that the collision detection
algorithm must be fast. The naive algorithm runs
in O(p*q) time, where p is the number
of edges in polygon P and q is the number of edges in
polygon Q. In a world containing
several moving polygonal objects, there is a lot of time
when there are no collisions, so
it is not always necessary to run a collision detection
algorithm. A method exists for testing
whether or not there is the possibility of a collision,
in constant time.
The minimum spanning circle, or bounding circle, of a
set of points is the smallest circle
which contains the set of points. If we are testing
for a collision between two moving
polygons, we can test to see if their bounding circles
intersect, in constant time, before
running the more accurate, and costlier, collision detection.
If their bounding circles do
not intersect, there is no possibility that a collision
exists.
Jon Rokne (1992) offers an algorithm to compute the bounding
circle of a set of points
in O(n^2) time.
1. Find p, the point with smallest y-coordinate,
and q, the point which minimizes
the angle between pq and the
x-axis.
2. Find r, the point which minimizes the angle prq.
3. If the angle pqr ( or qpr ) is obtuse, swap p ( q ) with r and go back to 2.
4. If the angle prq is obtuse, then the center of
the bounding circle is described by the
midpoint of pq, and the radius
is half the length of pq.
5. If all angles in triangle pqr are acute, then the circle is defined by the points p, q, r.
Collision detection can be further sped up by using a
faster algorithm for computing
the existence of the intersection of two polygons.
Chazelle and Dobkin (1987) offer
an algorithm that computes the existence of this intersection
in O(log n) time.
All operations described for the red polygon are also applied to the green polygon:
1. Select a vertex p1 of the red polygon.
Select some point interior to the green polygon.
Construct the line (yellow)
which connects these points. If p1 lies on the segment ab
the intersection exists.
2. If we haven't found an intersection yet, construct
the black lines. The upper black
line is defined by p1 and the
point on the portion of the green polygon above the
yellow line which maximizes
the angle formed at the intersection of the yellow and
black lines. The lower
black line is defined in a similar fashion, using the points of
the green polygon which are
below the yellow line. Repeating this procedure for
the green polygon yields the
following situation:
The problem has been reduced to testing the intersection
of the red and green chains captured
within the trapezoid.