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
}
discuss this topic to forum
