Rotating rectangles for collisions?

Just as the title says, is there any way that I can rotate the rectangle so that i can still call it for collisions? This is what I’ve got right now, for reference.

I have standard collisions working, but it’s a real pain for corners.

Well, I’m pretty sure that someone else can offer you advice about the actual rotation.

However, if you just need to do the collision check in a sort of quick and dirty way? Well, that’s pretty easy since you already know their positions, right?

So, you’ve got three sets of cases for collisions with rectangles:

  1. Rotated rect’s corner collides with a stationary: Check if any of the four corners appear inside of another rectangle.
  2. Stationary rect’s corner collides with rotated: Rotate the corner points of the stationary rect to adjust them be on the Axis Bounded to the rotated one, then check like in 1.
  3. Edge collision: Only happens when the rotatable rect is rotated at angel % 90 = 0. Handled like an AABB collision.

cubemaster21, what are you using to check the standard collisions, shape.intersects()? How are you drawing the rectangle, using the Rectangle2D class?

no, This is all with a standard Rectangle() class. And yes, for collisions, i’m using rectangle.intersects.

Try this:


         int degree = 90; //desired degree
         int rectX = 100, rectY = 100, rectWidth = 150, rectHeight = 100;
         Shape rect = new Rectangle(rectX, rectY, rectWidth, rectHeight); //creating the rectangle you want to rotate
         Rectangle rect2 = new Rectangle(400, 100, rectWidth, rectHeight); //creating other rectangle to check intersection
         AffineTransform transform = new AffineTransform();
         //rotate or do other things with the rectangle (shear, translate, scale and so on)
         transform.rotate(Math.toRadians(degree), rectX + rectWidth /2, rectY + rectHeight /2); //rotating in central axis
         //rect receiving the rectangle after rotate
         rect = transform.createTransformedShape(rect);
         g2d.draw(rect); //Graphics2D object drawing the rectangle on the screen

         //and then check the intersection
        if(rect.intersects(rect2)){
           //Do something...
        }