January 15th 2008
Flash Tutorial: Circular Collisions
Introduction
Collision detection between circles is highly useful because it allows for more accurate collision detection with circles and other objects that require a collision detection other than ‘hitTest’. In this tutorial I will explain how to create collision detections with circles.
Tutorial
We must first find the distance between the circles. To find this we use:
distx = circle1._x-circle2._x
disty= circle1._y-circle2._y
distance = Math.sqrt((distx*distx)+(disty*disty))
The first line of code finds the distance between the _x values of the circles and the second finds the distance between the _y values. The third line finds the distance between the two circles (If you are wondering why: The Pythagorean Theorem allows us to find the third line, or the distance in the triangle created by the two distances by finding the root of (x*x)+(y*y). ) Now that we have the distance between the circles, we have to find the minimal distance between the circles before there is a collision.

In this diagram you can see that the closest possible distance they can get before touching is the sum of the radii. Therefore, we know that the minimal distance can be found with:
mindist = (circle1._width/2) + (circle2._width/2)
So with these variables we know the actual test is:
if(distance<mindist){
//collision
}
Final Code
distx = circle1._x-circle2._x
disty= circle1._y-circle2._y
distance = Math.sqrt((distx*distx)+(disty*disty))
mindist = (circle1._width/2) + (circle2._width/2)
if(distance<mindist){
//collision
}

Subscribe to Design Apex
On February 26th, 2008 MuhammadUmer Said:
Good Wow Best For ciclres only
On February 26th, 2008 Trimble Naze Said:
Is very much good.
I am saying this to you now.
Circles are good for Flash. These make things true to scale.
On June 22nd, 2008 Rohedin Said:
Great tut!
thx