You are on page 1of 3

Collision detection for arbitrary shapes is usually

quite tricky since you have to figure out if any pixel


collides.
This is actually easier with circles. If you have two
circles of radius r1 and r2, a collision has occurred if
the distance between the centers is less than r1+r2.
The distance between the two centers (x1,y1) and
(x2,y2) can be calculated and compared as:
d = sqrt((y2-y1) * (y2-y1) + (x2-x1) * (x2-
x1));
if (d < r1 + r2) { ... bang ... }
Or, as jfclavette points out, square roots are expensive
so it may be better to calculate using just simple
operations:
dsqrd = (y2-y1) * (y2-y1) + (x2-x1) * (x2-
x1);
if (dsqrd < (r1+r2)*(r1+r2)) { ... bang ...
}
The tricky bit comes in calculating the new
movement vectors (the rate at which (x,y) changes
over time for a given object) since you need to take
into account the current movement vectors and the
point of contact.
I think as a first cut, you should just reverse the
movement vectors to test if the collision detection
works first.
Then ask another question - it's better to keep
individual questions specific so answers can be
targeted.
edited Apr 23 '09 answered Apr 23 '09
at 3:57 at 3:29
link|edit|flag
gnovice paxdiablo
30.8k52856 127k12152383
3 Just a note: square roots are rather
costly operations, and you can
square both sides of the equation
since they are both positive. That
gives you d^2 = (y2-y1) * (y2-y1)
+ (x2-x1) * (x2-x1) and (d^2 <
(r1+r2)^2) as a test. – jfclavette
Apr 23 '09 at 3:37
Good point, @jfclavette,
especially if you want maximum
frames/sec, incorporated into
answer. – paxdiablo Apr 23 '09 at
3:48
780240
Detecting a collision is only the first
up vote 2 down vote step. Let's break that down.
The fastest thing to do is calculate their
square bounding boxes and see if those
collide. Two of the sides need to cross
(top of 1 and bottom or 2, and left of 1
and right of 2, or vice versa) in order
for the bounding boxes to overlap. No
overlap, no collision.
Now, when they do overlap, you need
to calculate the distance between them.
If this distance is more than the sums of
the radii of the balls, then no collision.
Okay! We have two balls colliding.
Now what? Well, they have to bounce
off each other. Which way they bounce
depends on a few factors.
The first is their elasticity. Two rubber
balls bouncing off each other rebound
differently than two glass balls.
The second is their initial velocity.
Inertia states that they'll want to keep
going in mostly the same direction they
started in.
The third is the mass of the balls. A ball
with smaller mass will rebound off a
much larger mass with a higher
velocity.
Let's deal with the second and third
factors first, since they are intertwined.
Two balls will rarely hit exactly dead
on. Glancing blows are far more likely.
In any case, the impact will happen
along the normal of the tangent where
the balls collide. You need to calculate
the vector component of both along this
normal given their initial velocities.
This will result in a pair of normal
velocities that both balls will bring to
the collision. Add up the sum and store
it somewhere handy.
Now we have to figure out what each
ball will take away from it. The
resulting normal velocity of each ball is
inversely proportional to the given ball's
mass. That is to say, take the reciprocal
of each ball's mass, add both masses
together, and then parcel out the
resultant normal velocity away from the
collision based on the ratio of the ball's
mass to the sum of the reciprocal of
both ball's masses. Then add the
tangential velocity to this, and you get
the resultant velocity of the ball.
Elasticity is mostly the same, except it
requires some basic calculus due to the
fact that the balls are still moving even
as they compress. I'll leave it to you to
find the relevant math.

For the case of two colliding bodies in two-dimensions, the overall velocity of each body
must be split into two perpendicular velocities: one tangent to the common normal surfaces of
the colliding bodies at the point of contact, the other along the line of collision. Since the
collision only imparts force along the line of collision, the velocities that are tangent to the
point of collision do not change. The velocities along the line of collision can then be used in
the same equations as a one-dimensional collision. The final velocities can then be calculated
from the two new component velocities and will depend on the point of collision. Studies of
two-dimensional collisions are conducted for many bodies in the framework of a two-
dimensional gas.

Two-dimensional elastic collision


In a center of momentum frame at any time the velocities of the two bodies are in opposite
directions, with magnitudes inversely proportional to the masses. In an elastic collision these
magnitudes do not change. The directions may change depending on the shapes of the bodies
and the point of impact. For example, in the case of spheres the angle depends on the distance
between the (parallel) paths of the centers of the two bodies. Any non-zero change of
direction is possible: if this distance is zero the velocities are reversed in the collision; if it is
close to the sum of the radii of the spheres the two bodies are only slightly deflected.
Assuming that the second particle is at rest before the collision, the angles of deflection of the

two particles, and , are related to the angle of deflection θ in the system of the center of
mass by [2]