/**********************************************
***********************************************
** **
** Flash Collision Detection Functions **
** **
** ----------------------------------------- **
** This file declares collision detection **
** functions for several different types of **
** collisions involving circles, rects, etc **
** ----------------------------------------- **
** **
** Jermo2004 **
** **
***********************************************
**********************************************/
//
// this function tests for the intersection of 2 circles
// returns a boolean value of true if there is a collision
//
// usage: circleTest( [X1] , [Y1] , [Radius1] , [X2] , [Y2] , [Radius2] )
//
// test collision between a dot and a circle using a radius of 0 for the dot
//
//////////////////////////////////////////////////////////////////////////////
_root.circleHit = function(nAX, nAY, nAR, nBX, nBY, nBR)
{
var xDist = (nAX - nBX);
var yDist = (nAY - nBY);
var nDist = Math.sqrt((xDist * xDist) + (yDist * yDist));
return (nDist < (nAR + nBR));
}
//
// this function tests for the intersection of a circle with a rectangle
// returns a boolean value of true if there is a collision
//
// usage: circleIntersectRect( [CircleX] , [CircleY] , [CircleRadius] ,
// [RectLeft] , [RectTop] , [RectRight] , [RectBottom] )
//
///////////////////////////////////////////////////////////////////////////////////////
_root.circleIntersectRect = function(nCX, nCY, nCR, nRLeft, nRTop, nRRight, nRBottom)
{
var nCloseX = 0; //holds the closest X position inside the rectangle
var nCloseY = 0; //holds the closest Y position inside the rectangle
// determine the closest X position inside the rect
// and if the X is totally out of range, return false
if (nCX >= (nRLeft - nCR) && nCX <= (nRRight + nCR))
{
if (nCX >= nRLeft && nCX <= nRRight)
nCloseX = nCX;
else if (nCX < nRLeft)
nCloseX = nRLeft;
else
nCloseX = nRRight;
}
else
return false;
// determine the closest Y position inside the rect
// and if the Y is totally out of range, return false
if (nCY >= (nRTop - nCR) && nCY <= (nRBottom + nCR))
{
if (nCY >= nRTop && nCY <= nRBottom)
nCloseY = nCY;
else if (nCY < nRTop)
nCloseY = nRTop;
else
nCloseY = nRBottom;
}
else
return false;
//do a collision detection with the closest dot on the rectangle with the circle
return _root.circleHit(nCX, nCY, nCR, nCloseX, nCloseY, 0);
}