如何解决旋转矩形的碰撞检测
| 我正在创建一个乒乓球游戏。但是,在我的游戏中,桨板可以绕其中心旋转。 这些桨由angle2D对象表示。现在,这些矩形应该会碰到它们。球由一个circle2D对象表示,当球撞击桨叶时(通过使用矩形的相交方法完成),球会反转方向。当不旋转桨时,这很好用,但是当桨旋转时,相交方法不起作用。当我使用以下语句时:paddle2.intersects(xLocation,yLocation,diameter,diameter)
(其中paddle2是其中一个矩形的名称,传递给它的参数代表x坐标,y坐标和圆的半径)
圆就像矩形没有旋转一样。也就是说,它将从矩形的原始位置反弹。
我可能应该提到,我正在使用仿射变换来旋转矩形。这是我用来使矩形看起来旋转的命令:
g2.rotate(Math.toRadians(angle),xLocation+(width/2),yLocation+(height/2));
(其中的参数是矩形的旋转角度以及中心的x和y坐标)。
然后,我将我的g2对象的仿射变换重置为常规仿射变换。
我已经研究了这个问题已有一段时间了,并且已经找到了关于这个问题的一些讨论。但是,它们似乎在我头上,并且似乎在处理矩阵数学(作为一个从未学习过必要数学的人,我很迷茫)。因此,我希望有人能够提供一个简单的解决方案,或者引导我完成所需的数学运算。
谢谢!
解决方法
也许这是一个古老的问题,但是我想为其他将要阅读此帖子的人提供解决方案:
/** Rectangle To Point. */
boolean testRectangleToPoint(double rectWidth,double rectHeight,double rectRotation,double rectCenterX,double rectCenterY,double pointX,double pointY) {
if(rectRotation == 0) // Higher Efficiency for Rectangles with 0 rotation.
return Math.abs(rectCenterX-pointX) < rectWidth/2 && Math.abs(rectCenterY-pointY) < rectHeight/2;
double tx = Math.cos(rectRotation)*pointX - Math.sin(rectRotation)*pointY;
double ty = Math.cos(rectRotation)*pointY + Math.sin(rectRotation)*pointX;
double cx = Math.cos(rectRotation)*rectCenterX - Math.sin(rectRotation)*rectCenterY;
double cy = Math.cos(rectRotation)*rectCenterY + Math.sin(rectRotation)*rectCenterX;
return Math.abs(cx-tx) < rectWidth/2 && Math.abs(cy-ty) < rectHeight/2;
}
/** Circle To Segment. */
boolean testCircleToSegment(double circleCenterX,double circleCenterY,double circleRadius,double lineAX,double lineAY,double lineBX,double lineBY) {
double lineSize = Math.sqrt(Math.pow(lineAX-lineBX,2) + Math.pow(lineAY-lineBY,2));
double distance;
if (lineSize == 0) {
distance = Math.sqrt(Math.pow(circleCenterX-lineAX,2) + Math.pow(circleCenterY-lineAY,2));
return distance < circleRadius;
}
double u = ((circleCenterX - lineAX) * (lineBX - lineAX) + (circleCenterY - lineAY) * (lineBY - lineAY)) / (lineSize * lineSize);
if (u < 0) {
distance = Math.sqrt(Math.pow(circleCenterX-lineAX,2));
} else if (u > 1) {
distance = Math.sqrt(Math.pow(circleCenterX-lineBX,2) + Math.pow(circleCenterY-lineBY,2));
} else {
double ix = lineAX + u * (lineBX - lineAX);
double iy = lineAY + u * (lineBY - lineAY);
distance = Math.sqrt(Math.pow(circleCenterX-ix,2) + Math.pow(circleCenterY-iy,2));
}
return distance < circleRadius;
}
/** Rectangle To Circle. */
boolean testRectangleToCircle(double rectWidth,double circleCenterX,double circleRadius) {
double tx,ty,cx,cy;
if(rectRotation == 0) { // Higher Efficiency for Rectangles with 0 rotation.
tx = circleCenterX;
ty = circleCenterY;
cx = rectCenterX;
cy = rectCenterY;
} else {
tx = Math.cos(rectRotation)*circleCenterX - Math.sin(rectRotation)*circleCenterY;
ty = Math.cos(rectRotation)*circleCenterY + Math.sin(rectRotation)*circleCenterX;
cx = Math.cos(rectRotation)*rectCenterX - Math.sin(rectRotation)*rectCenterY;
cy = Math.cos(rectRotation)*rectCenterY + Math.sin(rectRotation)*rectCenterX;
}
return testRectangleToPoint(rectWidth,rectHeight,rectRotation,rectCenterX,rectCenterY,circleCenterX,circleCenterY) ||
testCircleToSegment(tx,circleRadius,cx-rectWidth/2,cy+rectHeight/2,cx+rectWidth/2,cy+rectHeight/2) ||
testCircleToSegment(tx,cy-rectHeight/2) ||
testCircleToSegment(tx,cy-rectHeight/2,cy+rectHeight/2);
}
这是测试圆形(球形)和矩形(可旋转)之间的碰撞的代码。
,相交方法不起作用的原因是,您实际上并未旋转Rectangle2D
。您正在告诉图形上下文旋转,以便它绘制的所有内容(例如未旋转的Rectangle2D
)都被旋转绘制,但实际的Rectangle2D
实例并未被修改。
也许您可以使用Polyon
代替Rectangle2D
,并按照此答案旋转它们。这样,实际实例将被旋转(因此您无需旋转Graphics2D
实例),并且碰撞检测应该可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。