Hold a square in the corner of a rotated square in Java

I make a zamboni game and I do collision detection. I try to do this by checking to see if the zamboni corner is inside the wall. I draw a rectangle in the corner using LWJGL. At the moment, I have a corner located in the center of the zamboni, but I want it to be in the upper left corner. I can do this, but when I rotate the zamboni, the corner thing does not go to the place of the actual angle of the zamboni, but instead remains in the same position as when the zamboni does not rotate.

Here is my code:

cornerLocation.x = position.x + (float) Math.cos(Math.toRadians(angle + 90));
cornerLocation.y = position.y + (float) Math.sin(Math.toRadians(angle + 90));

position is the vector in which I store the location of the zamboni. Its origin is in the center, so the upper left corner of the zamboni is mainly in the position-size / 2.

How can I make it so that it is always in the actual corner of the zamboni, even when I rotated it?

+4
source share
1 answer

You will need two sets of coordinates:

  • Zamboni corner points.
  • Rectangular points for collision detection.

Rectangle points can be calculated from zamboni corners. To do this: You should get "min-x" and "min-y" from them:

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);

The detection detection rectangle size is usually larger than the Zamboni size.

What happens to rotation?

Steps (one of many possible ways)

2d points {x, y} → goes into 3d: {x, y, 1}

float[][] zamboniCorner1Point3d = {{zamboniCorner1.x,zamboniCorner1.y,1}};
...
float[][] zamboniCorner4Point3d = {{zamboniCorner4.x,zamboniCorner4.y,1}};

1.- You need to move the zamboni center to (0,0) and you will drop the zamboni corners with the center:

You can use this 3-dimensional matrix (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.- You need to turn all the cordins (the center of the zamboni does not change, it {{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.- : min-x, min-y, max-x max-y , .   top-lef: (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)
+1

All Articles