我正在制作zamboni驾驶游戏,即时通讯正在执行碰撞检测。我正在尝试通过检查zamboni的一角是否在墙内来实现这一目标。我使用LWJGL在拐角位置绘制一个矩形。目前,我的角落位于zamboni的中心,但我希望它位于其左上角。我可以做到这一点,但是当我旋转zamboni时,拐角处的东西不会移到zamboni实际拐角的位置,而是停留在与不旋转zamboni时相同的位置。
这是我的代码:
cornerLocation.x = position.x + (float) Math.cos(Math.toRadians(angle + 90));
cornerLocation.y = position.y + (float) Math.sin(Math.toRadians(angle + 90));
position是我存储zamboni位置的向量。它的原点在中心,所以zamboni的左上角基本上位于position-size / 2。
我如何才能做到,即使我旋转它也始终将它放在zamboni的实际拐角处?
最佳答案
您需要两组坐标:
可以从zamboni角计算出Rectangle点。为了这:
您必须获取它们的“min-x”和“min-y”:
Point topLeftRect = new Point(Math.min(zamboniCorner1.x,zamboniCorner2.x,zamboniCorner3.x,zamboniCorner4.x),
Math.min(zamboniCorner1.y,zamboniCorner2.y,zamboniCorner3.y,zamboniCorner4.y));
Point bottomRightRect = new Point(Math.max(zamboniCorner1.x,zamboniCorner2.x,zamboniCorner3.x,zamboniCorner4.x),
Math.max(zamboniCorner1.y,zamboniCorner2.y,zamboniCorner3.y,zamboniCorner4.y));
Rectangle collisionDetectionRectangle =new Rectangle(topLeftRect,bottomRightRect);
检测冲突的矩形大小通常大于Zamboni大小。
旋转会发生什么?
步骤(许多可能的方法之一)
the 2d points {x,y} -> goes to 3d: {x, y, 1}
float[][] zamboniCorner1Point3d = {{zamboniCorner1.x,zamboniCorner1.y,1}};
...
float[][] zamboniCorner4Point3d = {{zamboniCorner4.x,zamboniCorner4.y,1}};
1.-您需要将zamboni的中心移至(0,0),然后将zamboni的角与中心放在一起:
您可以使用此3-d矩阵(1):
float[][] translationMatrix1 = {{1, 0,-zamboniCenter.x},{0, 1,-zamboniCenter.y},{0, 0, 1}};
float[][] zamboniCorner1Point3dNew = Matrix.cross(zamboniCorner1Point3d,translationMatrix1);
...
float[][] zamboniCorner4Point3dNew = Matrix.cross(zamboniCorner4Point3d,translationMatrix1);
Point' -> Point * Matrix1
2.-您需要旋转所有坐标(zamboni的中心不变,它是{{0,0,1})
You can used this 3-d matrix (2):
float[][] rotationMatrix2 = {Math.cos(rotationAngle), Math.sin(rotationAngle), 0 }, {-Math.sin(rotationAngle), Math.cos(rotationAngle), 0}, {0, 0, 1 }};
float[][] zamboniCorner1Point3dNew = Matrix.cross(zamboniCorner1Point3dNew,rotationMatrix2);
...
float[][] zamboniCorner4Point3dNew = Matrix.cross(zamboniCorner4Point3dNew,rotationMatrix2);
Point' -> Point * Matrix2
3.-您需要将zamboni的中心(从{0,0,1})移动到原始位置(首先在{{zamboniCenter.x,zamboniCenter.y,1}}的同一位置),并放下具有中心的角。
You can used a 3-d matrix(3):
float[][] translationMatrix3 = {{1, 0, zamboniCenter.x},{0, 1, zamboniCenter.y},{0, 0, 1}};
float[][] zamboniCorner1Point3dNew = Matrix.cross(zamboniCorner1Point3dNew,translationMatrix3);
...
float[][] zamboniCorner4Point3dNew = Matrix.cross(zamboniCorner1Point3dNew,translationMatrix3);
Point' -> Point * Matrix3
4.-设置新值。
zamboniCorner1.x = zamboniCorner1Point3dNew[0];
zamboniCorner1.y = zamboniCorner1Point3dNew[1];
...
zamboniCorner4.x = zamboniCorner4Point3dNew[0];
zamboniCorner4.y = zamboniCorner4Point3dNew[1];
5.-然后,获取:新的zamboni角的min-x,min-y,max-x和max-y,其中有新的碰撞检测矩形。
左上角:(min-x,min-y)右下角:(max-x,max-y)。
Point topLeftRect = new Point(Math.min(zamboniCorner1.x,zamboniCorner2.x,zamboniCorner3.x,zamboniCorner4.x),
Math.min(zamboniCorner1.y,zamboniCorner2.y,zamboniCorner3.y,zamboniCorner4.y));
Point bottomRightRect = new Point(Math.max(zamboniCorner1.x,zamboniCorner2.x,zamboniCorner3.x,zamboniCorner4.x),
Math.max(zamboniCorner1.y,zamboniCorner2.y,zamboniCorner3.y,zamboniCorner4.y));
Rectangle collisionDetectionRectangle =new Rectangle(topLeftRect,bottomRightRect);
步骤1、2和3;可以一起计算:
float[][] matrix = Matrix.cross(Matrix.cross(translationMatrix1,rotationMatrix2),translationMatrix3);
float[][] zamboniCorner1Point3dNew = Matrix.cross(zamboniCorner1Point3d,matrix);
...
float[][] zamboniCorner4Point3dNew = Matrix.cross(zamboniCorner4Point3d,matrix);
Point' -> Point * (Matrix-1 * Matrix-2 * Matrix-3)