I am trying to check for collision between 2 rotated rectangles using SAT. However in certain angles, it doesn't work properly. (see here https://i.sstatic.net/DbetA.jpg)
Will help if anyone can point out my mistake. Here's my function:
bool SeperatingAxisThereom(std::vector<CVector2> rect1_points, std::vector<CVector2> rect2_points)
{
CVector2 axis;
float testMagnitude, min1, min2, max1, max2;
int pointA, pointB;
for ( int i = 0; i < rect1_points.size(); i++)
{
// Get an edge from rect1 and it's normal
pointA = i;
pointB = i+1;
if (pointB >= rect1_points.size())
pointB = 0;
axis = rect1_points.at(pointA) - rect1_points.at(pointB);
axis = axis.getPerpendicularVector();
axis.normalizeVector3D();
// Project rect 1
min1 = max1 = axis.dotVector3D(rect1_points[0]);
for ( int i = 1; i < rect1_points.size(); i++)
{
testMagnitude = axis.dotVector3D(rect1_points[i]);
if (testMagnitude > max1)
max1 = testMagnitude;
if (testMagnitude < min1)
min1 = testMagnitude;
}
// Project rect 2
min2 = max2 = axis.dotVector3D(rect2_points[0]);
for ( int i = 1; i < rect2_points.size(); i++)
{
testMagnitude = axis.dotVector3D(rect2_points[i]);
if (testMagnitude > max2)
max2 = testMagnitude;
if (testMagnitude < min2)
min2 = testMagnitude;
}
// Check for intersection
if (min1 < min2)
{
if (max1 < min2 )
{
printf("\n");
return false; // no collision
}
}
else if(min2 < min1)
{
if(max2 < min1)
{
printf("\n");
return false; // no collision
}
}
}
return true;
}
I am not quite sure, but i think you have to calculate the axes according to the normal of BOTH shapes, not just the first one.
Taken from here: http://www.codezealot.org/archives/55
"The axes you must test are the normals of each shape’s edges."
And it seems like you are not returning true anywhere?