15,615,280 members
See more:
I have two Rotated images that rotate at a random degree and I want to know when they collide with each other. I tried a simple collision detection for rectangles but it doesn't work when they are rotated. I would really appreciate some help.

What I have tried:

// take two images and check if they overlap
function rectIntersect(x1, y1, w1, h1, x2, y2, w2, h2) {
// Check x and y for overlap
if (x2 > w1 + x1 || x1 > w2 + x2 || y2 > h1 + y1 || y1 > h2 + y2) {
return false;
}
return true;
}
Posted
Gerry Schmitz 29-May-22 10:50am
You're comparing "polygons"; not "images". You use a matrix transform to rotate the corners; then you use the corners of of the polygons to see if "one point (corner) of one polygon is inside the other".

https://math.stackexchange.com/questions/1917449/rotate-polygon-around-center-and-get-the-coordinates

https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/
0x01AA 29-May-22 11:41am
I don't think checking the corners is enough ;)
Gerry Schmitz 29-May-22 13:21pm
How do you intersect two rectangles without at least one corner? Think about it.
0x01AA 29-May-22 13:32pm
Yes think about it. Take two squares, rotate one for 45°....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 155 Richard MacCutchan 125 raddevus 75 CHill60 60 Rick York 30
 OriginalGriff 3,036 Richard MacCutchan 1,286 Graeme_Grant 965 Dave Kreskowiak 649 CHill60 360

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900