我的箱子没有碰撞

时间:2016-04-28 04:55:01

标签: java algorithm collision-detection game-engine physics

我在java中制作游戏引擎,并使用单独的轴定理进行碰撞。有时它没有记录碰撞的错误。有没有人发现我的碰撞程序有什么问题?

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;
    }

0 个答案:

没有答案