我们以cocos2dx lua脚本来说明,原理才是重点,本文参照了两种方法
第一种方法
bool intersects(CircleType circle, RectType rect)
{
//1
circleDistance.x = abs(circle.x - rect.x);
circleDistance.y = abs(circle.y - rect.y);
//2
if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }
//3
if (circleDistance.x <= (rect.width/2)) { return true; }
if (circleDistance.y <= (rect.height/2)) { return true; }
//4
cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
(circleDistance.y - rect.height/2)^2;
return (cornerDistance_sq <= (circle.r^2));
}
circle.x, circle.y圆心 rect.x, rect.y 矩形的中心
Here's how it works:
1.The first pair of lines calculate the absolute values of