I am making a game engine in java, and am using the separate axis theorem to do collision. There is something wrong where it sometimes doesn't register the collisions. Does anyone see anything wrong with my collision program?
public PolygonCollisionResult PolygonCollision( Polygon2D polygonB) {
Polygon2D polygonA=getPolygon();
double d=Math.sqrt((polygonA.getCenter().y-polygonB.getCenter().y)^2+(polygonA.getCenter().x-polygonB.getCenter().x)^2);
PolygonCollisionResult result = new PolygonCollisionResult();
result.Intersect = true;
result.WillIntersect = true;
int edgeCountA = polygonA.npoints;
int edgeCountB = polygonB.npoints;
double minIntervalDistance = Double.POSITIVE_INFINITY;
Vector2D translationAxis = new Vector2D();
Vector2D edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) {
if (edgeIndex < edgeCountA) {
edge = polygonA.returnEdge(edgeIndex);
} else {
edge = polygonB.returnEdge(edgeIndex - edgeCountA);
}
// ===== 1. Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector2D axis = new Vector2D(-edge.y, edge.x);
axis.normalize();
// Find the projection of the polygon on the current axis
float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
double[] minmaxA=ProjectPolygon(axis, polygonA, d);
minA=(int)minmaxA[0];
maxA=(int)minmaxA[1];
double[] minmaxB=ProjectPolygon(axis, polygonB, d);
minB=(int)minmaxB[0];
maxB=(int)minmaxB[1];
// Check if the polygon projections are currentlty intersecting
if (IntervalDistance(minA, maxA, minB, maxB) > 0)result.Intersect = false;
}
return result;
}