Flash Tutorial: Circular Collisions
January 15th 2008
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 We Talk Design
Comments
1
February 26th
MuhammadUmer
Good Wow Best For ciclres only
2
February 26th
Trimble Naze
Is very much good.
I am saying this to you now.
Circles are good for Flash. These make things true to scale.
3
June 22nd
Rohedin
Great tut!
thx
4
December 19th
AffeleExteple
А есть, какая нибудь альтернатива?
5
April 4th
Anonymous
interesting things!
6
April 9th
Matt
“Collision detection between circles is highly useful because it allows for more accurate collision detection with circles.”
Right, thanks.
7
May 2nd
boryusha
Free speech zone
8
June 10th
larunya
04. El pelito de nuevo iPhone
Say Something!