SECTION 0: General Introduction
--= Collision Detection in Games =--
Typically, collision detection in games is carried out into two steps:
(1) determine which pairs of shapes need to be tested for collision (broad phase)
(2) determine collision results for each pair identified in step (1) (narrow phase)
In N, step (1) is implemented using a uniform "loose" grid of square cells; each shape is stored in the cell which contains its center, and each shape is collided against any shapes in its current cell, or the 8 cells touching the current cell.
Later tutorials will explain this system in greater detail; this tutorial will explain how step (2) was implemented in N.
The algorithms we used to handle this step are of use to any game which requires fast collision detection that provides more than a simple boolean result.
--= Collision Response via Projection =--
Before thinking about how to detect collisions, we should first think about what should happen to two objects which collide.
Given only the fact that objects
a and
b overlap each other, a simple response would be to destroy one or both objects, or move one or both back to their previous positions.
While this might be sufficient for some types of objects, we'd also like physically simulated objects which behave in a more realistic manner. In order to support this feature, we'll need more information than simply "
a and
b are overlapping". Specifically, information about the
nature of the overlap is needed.
Projection
[Jakobsen] is one physically-plausible method for dealing with overlapping ("penetrating") objects. The basic idea is to move the objects out of penetration using the smallest possible displacement. Solving the collision then becomes equivalent to finding the vector
v which moves the two objects out of penetration by the shortest distance possible.
Some other ways to deal with collision are using
penalty-force or
impulse-based methods. Penalty methods use spring forces to
pull objects out of collision. Impulse-based methods use instantaneous
impulses (changes in velocity) to prevent objects from interpenetrating.
Another way to look at the different collision-response methods is in terms of the object's position. Projection modifies the position of objects
directly; impulse-based methods modify the
first derivative of the positions (i.e velocities), and penalty-methods modify the
second derivative of the positions (i.e accelerations, cause from spring forces) -- all three methods are trying to move the objects to some target position.
In this case, we want to move the objects so that they're not penetrating each other. All three collision response methods can be used with the collision detection methods we implemented; we chose to use projection because it was the simplest method, and it seemed to work well in test cases.
So, not only do we need a boolean result from our collision detection routines, we also need a
projection vector. Note that this projection vector can be described as a (unit) direction vector and a (scalar) penetration depth.
--= Bounce and Friction =--
Once we've projected our two objects so that they no longer collide, we'd also like to change their velocities to model physical phenomena such as "bounciness" and friction.
While the model used in N is quite simple and not realistic, we also developed a more realistic model for bouncy objects with friction -- we then discovered that this realistic model wasn't as fun-feeling as the simple model.
Our collision response method was:
. project out of collision
. split velocity vector into two components: one parallel and one perpendicular to the collision surface
. calculate bounce using the perpendicular component
. calculate friction using the parallel component
[
埋込みオブジェクト:diagrams/A-1_particle_collision.swf]
instructions:
drag the red and blue sliders to change the coefficients of friction and bounce .
comments:
the resulting velocity of the particle is determined by scaling the parallel and perpendicular components of the incoming velocity by the coefficients of friction and bounce , respectively.
Figure 0. The Path of a Particle as it Collides with a Horizontal Surface
--= NOTE =--
For a brief review of a few concepts of
2D geometry you'll need to know in order to understand the source code, see
Appendix A.
We're assuming you know what a vector is, how to scale and add vectors, and what a dot product is; that's pretty much all you'll need to know.
[ back to table of contents ]