Saturday, August 23, 2008

Collision Detection with terrain

Collision detection and reaction has got to be one of the most problematic aspects of programming a game, especially when uneven terrains are concerned. In order to properly tackle this issue, I have to first figure out the kind of collision detection I want for the game.

In the case of White Angel, I figured using lines to define the terrain would be a much better choice than to say, use a tile-based collision system, since the former would be much more flexible, and level creation would also be a lot faster.

The collision detection algorithm in used is based on the following article:
Simple Intersection Tests for Games

The first step is to create a "Plane" class (or to be more accurate, "Line" class). A MovieClip symbol was then derived from this class. This MovieClip, which I shall call the CollisionPlane, is defined by 2 other MovieClips within that represents the 2 points of the plane. Upon EnterFrame, the global positions of the 2 points will be calculated, along with other variables like gradient, normal etc. These variables will not only be useful for collision detection, but also the collision reaction and perhaps the game logic as well.

The above is just a very rough outline of what I did. Perhaps an image or two will help clarify the doubts. (Note: The following images shows a work-in-progress and are in no way representing what the final product would be.)

Notice it's a circle-to-line collision. The "terrain" is defined by the 3 CollisionPlanes.

Hide the collision shapes!

No comments: