With the Pythagoeran theorem you can detect collision with 2 circles.
You just have to get the rays of 2 circles with the width property divided by 2.
Actually the width property returns the diameter of the circle. So, you can calculte the ray like this:
ray = diamter / 2
ray = circle.width / 2
Therefore if you get the distance between the 2 circles you can detect the collision like this:
if distance < ( circle1.width/2+ circle2.width/2 ) is true => Collision
[swf:/wp-content/uploads/2010/12/Detect_collision_with_2_circles.swf 550 400]
// Start enter frame event
this.addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(pEvent:Event):void {
// Get distance between the 2 MovieClips
var dist:int = getDistance(myCircleBlu,myCircleRed);
// Test the distance and compare with the 2 rays
if (dist < myCircleRed.width/2 + myCircleBlu.width/2) {
// If collision is detected change the opacity
myCircleRed.alpha = 0.5;
myCircleBlu.alpha = 0.5;
} else {
myCircleRed.alpha = 1;
myCircleBlu.alpha = 1;
}
}
function getDistance(pObj1:MovieClip,pObj2:MovieClip):Number {
var distX:Number = pObj1.x - pObj2.x;
var distY:Number = pObj1.y - pObj2.y;
return Math.sqrt(distX * distX + distY * distY);
}
