Article Preview

Buy Now

COLUMN

## Collision Detection and Response

### Writing a simple bouncing ball simulator

**Issue:** 2.2 (October/November 2003)**Author:** Thomas Reed**Author Bio:** Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.**Article Description:** No description available.**Article Length (in bytes):** 10,259**Starting Page Number:** 32**Article Number:** 2214**Resource File(s):**

2214.zip Updated: 2013-03-11 19:07:57

**Related Link(s):** None

**Excerpt of article text...**

In many computer games, play is influenced by objects colliding with one another. For example, in an arcade game, a simple collision could be a missile intersecting an enemy ship; in this case, both may be destroyed and removed from play. Other collisions are more complex, such as games in which objects bounce off other objects with convincing trajectories.

The problem of handling collisions has two aspects: collision detection and collision response. In many games, collision detection involves determining if a point is inside a regular shape; this is a problem that can be solved easily. Collision response can be simple as well, often requiring nothing more complex than drawing an explosion and adding up some points.

Unfortunately, not all games can deal with collisions so easily. Consider the common problem of a bouncing ball. While this is a simple problem in concept, as it represents an intuitive real-world activity, it is neither simple nor intuitive to solve on a computer.

Consider the first half of the problem: detecting a collision. Let's assume that the ball is a perfect circle, moving among obstacles defined by polygons composed of any number of straight lines. In this situation, detecting a collision is merely a matter of geometry. The problem can be solved using a brute-force approach, by looping through each line of each polygon and checking for a collision with each one.

Before we discuss collision detection further, let's define a couple basic vector math terms. A

normalized vector is a vector with a length of one. Such a vector is excellent for indicating direction independent of magnitude, and will be used frequently. Thedot product of two vectors gives a single floating-point numeric value equal to the product of the length of both vectors and the cosine of the angle between them. This is represented by the equation a & b = |a||b|cos &&. Although you do not need to remember the equation, you should remember that it is useful for finding the component of one vector in the direction indicated by another, or projecting one vector onto another.There are several steps in checking for a collision with a line. The first is to find the predicted impact point on the ball for one line forming a polygon edge. This is easy if we represent the line using an arbitrary point anywhere on the line (lineOrigin) and a normalized vector perpendicular to the line (lineNormal). By reversing the lineNormal vector and multiplying it by the ball's radius, you get the point on the ball closest to the line. (See Figure 1.) This is the initial predicted impact point on the ball.

...End of Excerpt. Please purchase the magazine to read the full article.