旋转矩形碰撞检测

我正在创造一个乒乓球比赛。 然而,在我的游戏中,桨叶能够绕其中心旋转。

这些桨由rectangle2D对象表示。 现在,这些矩形应该在它到达时击中球。 球由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) + Math.pow(circleCenterY-lineAY, 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 rectHeight, double rectRotation, double rectCenterX, double rectCenterY, double circleCenterX, double circleCenterY, 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, ty, circleRadius, cx-rectWidth/2, cy+rectHeight/2, cx+rectWidth/2, cy+rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx+rectWidth/2, cy+rectHeight/2, cx+rectWidth/2, cy-rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx+rectWidth/2, cy-rectHeight/2, cx-rectWidth/2, cy-rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx-rectWidth/2, cy-rectHeight/2, cx-rectWidth/2, cy+rectHeight/2); } 

这是测试圆(圆球)和矩形(可以旋转)之间碰撞的代码。

相交方法不起作用的原因是因为您实际上没有旋转Rectangle2D 。 您正在告诉图形上下文旋转,以便它绘制的任何内容(例如未旋转的Rectangle2D )被旋转绘制,但实际的Rectangle2D实例不会被修改。

也许您可以使用Polyon作为拨片而不是Rectangle2D ,并按照此答案旋转它们。 这样实际的实例将被旋转(因此您不需要旋转Graphics2D实例)并且碰撞检测应该正常工作。