1

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;
    }
user2350459
  • 351
  • 4
  • 18
  • [Every good bug report needs exactly three things](http://www.joelonsoftware.com/articles/fog0000000029.html): Steps to reproduce; expected result; actual result. You have 1/3 here. – AakashM Apr 28 '16 at 08:11
  • Well i expect the boxes to collide, but they don't. It seems kind of random when they do or don't collide. https://github.com/quiteaniceguy This is my github account if you wanna run the program and see the collision issue – user2350459 Apr 28 '16 at 13:42
  • all these Vector2D Polygon2D ProjectPolygon come from where?? if they work fine I wil say THEY WORK FINE!! – gpasch Apr 28 '16 at 15:12
  • Oh, I made them lol. You can take a look on them on my github account if you want. Should I upload that too? I was worried my post would be too long. – user2350459 Apr 28 '16 at 15:14

0 Answers0