ESSENTIAL INGREDIENTS OF A COLLISION DETECTION TECHNIQUE
There are in general six main elements (challenges) need to be addressed effectively by any Collision Detection
technique in a 2D platform game environment:
A) To allow use of Arbitrary Geometry (this in plain English means that any kind of obstruction can be literarily
anywhere on the screen).
B) To allow Free-form Player Movement (FPM) in every possible direction and jumping (meaning that the player can
move freely on a real per-pixel basis or what we have set to represent the pixel basic unit in our game environment).
C) To handle effectively and realistically all possible collisions between the main character (the player controlled
character in our game) and the geometry dictated by the game environment.
D) To allow effective and realistic application of Gravity.
E) To effectively and realistically address the so-called “Jitter” problem.
F) To effectively and realistically address the so-called “Bullet-Through-Paper” problem.
Despite the fact that we all tend to regard present computer games status as the most advanced and the only one really
worthwhile to spend time on studying, the fact remains and will always remain that all started back in the 80s. Inevitably,
most of the classic techniques well-known for their combination of simplicity and efficiency are originating from this in
many respects ground-breaking decade.
The “Grid-based” Collision Detection Technique
In “Grid-based” platform games every level of the game is arranged as on a rectangular grid. Each grid square contains a
platform or a background tile. The main and all the other characters in the game always reside in exactly one square, with
especially the main character largely animated to give the appearance of being half on one square and half on another.
Nevertheless, when the main character moves left or right the movement always takes place in such a way as it ends up
exactly in the next grid square.
Collision Detection-wise the alignment of the game environment on a grid simplifies things a lot. The game environment is
“reproduced” first in a 2D array in RAM and then we check with every frame if the main character’s desired movement will
cause him to end up in a platform or other foreground tile; if this is so we prevent the movement from happening. The good
news about this technique are there’s absolutely no need for any sort of complicated “Pixel-based” Collision Detection and
there are no “Angular Collisions” to worry about since our main character can only move in one of four directions at a time.
Figure 1: “Bomberman” is a “Grid-based” 2D platform game originally released in 1983.
The “Pixel Colour” Collision Detection Technique
This technique was originally invented to take advantage of graphics cards capable of reproducing a limited number of
colour combinations (8 or 16 colour combinations). Under this technique, all the background elements in the game
environment were to be plotted in one colour, while all other game elements in other colours of choice. The main
character is allowed to move around in every possible direction but the colour of adjacent pixels is tested before
movement is allowed. The idea here is to check first if the movement will cause the main character to “Collide” with
a platform and/or wall. Apart from the fairly simple idea behind this technique and its considerably straight forward
implementation, the technique possesses the disadvantage of developers never being able to use the platforms’ colour
for other purposes within the game environment or in the making of other characters, or even in the background.
Figure 2: “Background Tiles”, “Hill Tiles” & “Solid Ground Tiles” in a “Pixel Colour” Collision Detection arrangement.
The “Pixel Tile” Collision Detection Technique
For this technique to effectively solve the Collision Detection problem what is required is for the available platforms to
be limited to a pre-defined selection of square tiles. The main character can move freely in every direction and a number
of “Collision Points” are defined around the main character sprite (his feet, shoulders, knees, head etcetera…) and for
each tile type. In a 2D array we store information indicating what’s to be done if a collision occurs on a specific pixel
of the tile.
For example, in a 32×32 pixel tile where half of it is representing a hill sloping down from top-left to bottom-right the
collision array must specify that when moving left, if a collision occurs between the main character’s feet and the solid
part of the tile, the main character must be moved up by one pixel. In the opposite case, the main character must be moved
down by one pixel when moving right. That way, what actions required in response to a collision are encoded in the tile
information.
The “Mask” Collision Detection Technique
This technique requires a copy of the level (or what’s currently visible of it to the player) to be kept stored in RAM in
Black and White colours. For this technique, White colour represents platforms or any other obstructions we have introduced
into our level, while Black colour represents literarily anything else. In the same sense, on a respective copy of the main
character sprite in RAM, also stored in Black and White colours, White colour represents all the Solid Points on the main
character’s figure where collisions must be considered and Black coloured pixels are completely ignored.
A Colour-coded image like this (binary colour representation, namely in Black and White colours only), of a colour image is
called a “Mask”. The net effect is that when the main character attempts to move, each White coloured pixel in the main
character’s “Mask” is compared with each pixel in the level “Mask” at the corresponding position of the main character in
the level. In the case that two pixels are both found to be White colour pixels, then a collision has occurred.
The main advantage of this technique is that the player can move freely in every direction, and the platforms and/or the
scenery can be of any shape with no restrictions at all. What’s more, the Collision Detection algorithm if implemented
correctly is accurate to the single pixel but contains the inherent deficiency of being potentially very expensive in
terms of computational system resources consumption because large Bitmap images may need to be stored in RAM. This
technique is one of those that either in its original form or in variations seeking to improve the technique at particular
points according to specific needs, is still in use today by a surprisingly large number of games.
The “Bounding Box” Collision Detection Technique
In this technique, the “Bounding Box” term refers to the smallest possible rectangle that can be made to entirely contain
a piece of geometry. The “Bounding Box” of the main character is compared with the “Bounding Box” of each platform tile
and/or enemy characters (the ones within a particular distance from the main character). In the case that the “Bounding
Boxes” intersect, then a collision has occurred.
The technique works perfectly fine for Horizontal or Vertical (flat) platforms and under the condition that the main
character and the enemies are mostly rectangular in shape. On the contrary, it fails miserably when the game environment
is made of sloped surfaces, abstract shapes and/or shapes being or close to circular. For these reasons the “Bounding Box”
technique is nowadays used as the basis and/or optimisation component of other more modern techniques. This is so because
of the fact that checking the “Bounding Boxes” of the level scenery against the “Bounding Box” of the main character is a
very quick way of narrowing down the number of pieces of geometry in need of further testing for achieving a more accurate
Collision Detection result.
The “Discrete Collision Detection” Technique
Many modern computer games employ this technique in order to realise an effective and accurate Collision Detection algorithm.
The technique’s main advantage is that can easily deal with many simultaneously moving objects within a game environment
colliding with one another as in a single frame, in contrary to the “one-object-colliding-with-a-static-set-of-geometry”
situation.
For this to become possible the technique demands the calculation of the total “Bounding Box” for each moving object, in
respect to both its Current and Expected position after the current frame. This is called an “Axis-aligned Bounding Box”
(AABB). The simple rule governing the technique is that any “Axis-aligned Bounding Boxes” overlapping are considered to
be at risk of their objects colliding (as their paths may, or not, cross each other). Each set of overlapping “Axis-aligned
Bounding Boxes” is then grouped together into a so-called “Time of Impact Island” (TOI Island).
At a second step, potential collisions in each “Time of Impact Island” are then handled sequentially using a so-called
“Contact Solver” (or “Collision Solver”) an algorithm using complex mathematics to determine the “Time of Impact”
ordering of each object in the island and its new trajectory. By thinking about it a bit more deeply, will eventually
become apparent that solving all the collisions and changing the trajectory of objects can cause inevitably new
collisions. To come around this side effect, the whole process of the “Axis-aligned Bounding Box” calculation, the one
of the “Time of Impact Island” calculation and the contact solving are repeated for a Programmer-defined standardised
arbitrary number of times. This approach to the problem increases the visible stability of the objects, since
over-corrections and constant toggling of an object against a surface between colliding and non-colliding can give rise
to the so-called “Jitter” problem.
The good news is that the “Time of Impact Island” ordering is generally only needed for simulations rather than 2D
platform games. In computer games, “Axis-aligned Bounding Boxes” are calculated to check if a main character or any
other object at this respect is about to hit some scenery element with the aim of identifying what is the amount of
movement beyond which the result will be for the main character or any other object to find itself in a rather awkward
situation (inside a wall or stuck in the ground). This is called “Penetration Resolution”.
The “Speculative Contacts” Collision Detection Technique
This represents the “latest-and-greatest” in Collision Detection techniques. Usage of this technique in the latest computer
games has given it a rather prestigious place among all the other techniques currently available. The original intention
behind its development was to effectively solve the so-called “Bullet-Through-Paper” problem, present in “Discrete Collision
Detection” whereby a fast-moving object can pass completely through a piece of geometry. This becomes possible because in
“Discrete Collision Detection” only the initial and final position of the object during each frame is considered and not all
of the possible in-between positions that the object would occupy during its movement. For example, if we consider a main
character made of a single-pixel, falling at 50 pixels per frame with a 5-pixel tall platform 30 pixels below him, then
according to our description no collision will be detected as both the main character’s current position and his position 50
pixels further down do not touch the platform at all. The only way to solve this problem is to examine the in-between positions,
as well as the initial and final positions, on a per-frame movement basis. The “Speculative Contacts” Collision Detection
technique aims to do exactly this.