I am trying to tell if two rectangles
are colliding. I have no issue if the rectangles are not rotated, but i am having some issues in my logic when they are both rotated. Here is my method i am currently using :
public static void car_on_ai_collision( AI ai, Entity e ){
//rotation of each rectangle in radians
double ai_rot = ai.getAIEntity().getRotation().y;
double car_rot = e.getRotation().y;
//stores the center point of the rectangles
Vector3f ai_loc = ai.getAIEntity().getLocation();
Vector3f car_loc = e.getLocation();
//here i am lining the square for my car up to a axis by making it have no rotation
ai_rot -= car_rot;
car_rot = 0;
//creating rectangles with the size of the car
Rectangle car = new Rectangle(175, 70);
Rectangle ai_rec = new Rectangle(175, 70);
car.translate((int) ((int) car_loc.x-87.5), (int) car_loc.z-35);
//rotation for rectangle
AffineTransform aiAT = new AffineTransform();
aiAT.translate((int) ai_loc.x - 87.5, (int) ai_loc.z-35);
aiAT.rotate( Math.toDegrees(ai_rot), ai_loc.x, ai_loc.z);
Area a = new Area(ai_rec);
a.transform(aiAT);
//testing for collision
if(a.getBounds2D().intersects(car)){
System.out.println("Collision!");
}
}
The collision detection doesn't seem to be anywhere close to being right, from my understanding one of the axis need to be aligned. I am attempting to align one of the axis and then test for a collision with a a AffineTransform
but i have seen online something about rotating more then 90 degrees causes a issue. How can i fix this problem to test for a collision between two rotated rectangles? Any help is appreciated.