SAT Question
Posted: Thu May 27, 2010 2:37 pm
I'm trying to implement the SAT and have been having some troubles. Here's some code:
This method returns true no matter what.
This is how I set up the polygons.
When explaining, please keep in mind that I'm only 13 so I barely understand this.
This method returns true no matter what.
Code: Select all
//Collision.cpp
bool Collision::PolygonPolygonCollision(Polygon &a, Polygon &b)
{
//The axis to project onto
Vector2 axisToProjectOnto;
//The minimum and maximum projections of the edges on the axes
float minA = a.getPoint(0).projectVector(axisToProjectOnto),
maxA = a.getPoint(0).projectVector(axisToProjectOnto),
minB = b.getPoint(0).projectVector(axisToProjectOnto),
maxB = b.getPoint(0).projectVector(axisToProjectOnto);
//For all of the points in a
for (int i = 0; i < a.getNumberOfPoints(); i++)
{
//Calculate the axes to project onto
axisToProjectOnto.x = a.getPoint(i).y;
axisToProjectOnto.y = -a.getPoint(i).x;
//Project the edges of a onto the axes
//For All of the points in a
for (int j = 0; j < a.getNumberOfPoints(); j++)
{
//Project the edge onto the axis and figure out the minimum and
//maximum values for a
if (a.getPoint(j).projectVector(axisToProjectOnto) < minA)
{
minA = a.getPoint(j).projectVector(axisToProjectOnto);
}
else if (a.getPoint(j).projectVector(axisToProjectOnto) > maxA)
{
maxA = a.getPoint(j).projectVector(axisToProjectOnto);
}
}
if (minA > maxB || maxA < minB)
{
return false;
}
}
//For all of the points in b
for (int i = 0; i < b.getNumberOfPoints(); i++)
{
//Calculate the axes to project onto
axisToProjectOnto.x = b.getPoint(i).y;
axisToProjectOnto.y = -b.getPoint(i).x;
//Project the edges of a onto the axes
//For All of the points in b
for (int j = 0; j < b.getNumberOfPoints(); j++)
{
//Project the edge onto the axis and figure out the minimum and
//maximum values for b
if (b.getPoint(j).projectVector(axisToProjectOnto) < minB)
{
minB = b.getPoint(j).projectVector(axisToProjectOnto);
}
else if (b.getPoint(j).projectVector(axisToProjectOnto) > maxB)
{
maxB = b.getPoint(j).projectVector(axisToProjectOnto);
}
}
if (minA > maxB || maxA < minB)
{
return false;
}
}
return true;
}
Code: Select all
//Main.cpp
polygon.addPoint(Vector2(imageX, imageY));
polygon.addPoint(Vector2(imageX, imageY));
polygon.addPoint(Vector2(imageX+width, imageY+height));
polygon2.addPoint(Vector2(image2X, image2Y));
polygon2.addPoint(Vector2(image2X, image2Y+height2));
polygon2.addPoint(Vector2(image2X+width, image2Y));
//Polygon.cpp
void Polygon::addPoint(Vector2 point)
{
point.normalize();
_points.push_back(point);
_numberOfPoints++;
}