Java 2d多边形碰撞响应

时间:2012-07-22 23:02:48

标签: java 2d physics collision

我做了一个使用凸多边形的游戏。我的计划是让这些多边形根据它们的质量和速度碰撞并反弹。但首先我需要确保它们不重叠。此代码检查多边形A的每个边缘,以查看多边形B的顶点是否在垂直于边缘的轴上重叠。整个方法返回修复Polygon A所需的结果Vector:

    /**Calculates adjustment vector for EntityPolygon A*/
public Vector calculateCollision(EntityPolygon A, EntityPolygon B) {

    //this is a large number so the first comparison of overlap is true
    double overlap = 10000;

            //this is the angle of the axis to apply the overlap vector
    double angle = 0;

            //I ran a for loop for every edge of the polygon
    for(int x = 0; x <= A.numPoint - 1; x++) {

                    //create variables
        Vector edge;
        Vector axis;
        double centerA;
        double centerB;
        double maxA;
        double maxB;
        double minA;
        double minB;

                    //this if statement finds this point and the next point
                    //to make a Vector of the edge
        if(x != A.numPoint - 1) {
            edge = new Vector(A.point[x], A.point[x + 1]);
        } else {
            edge = new Vector(A.point[x], A.point[0]);
        }

                    //this finds the axis perpendicular axis of the edge
        axis = edge.getRightNormal();

        //finds the location of both polygon's centers when projected onto
                    //the velocity(projectionOnVelocity() projects the point on the                                                           
                    //new axis)
        centerA = A.getLocation().getProjectionOnVelocity(axis);
        centerB = B.getLocation().getProjectionOnVelocity(axis);

                    //finds the location of polygons A and B on the axis by
                    //setting the min and max of their highest and lowest points
        maxA = findMax(A, axis);
        maxB = findMax(B, axis);
        minA = findMin(A, axis);
        minB = findMin(B,axis);

        //final comparison to find overlapping vector.
        if(centerA > centerB) {//if A is above B on the axis
            if(maxB > minA) {//if the max point on B is above min on A
                double m = maxB - minA;
                if(m < overlap) {
                    overlap = m;
                    angle = axis.angle;
                }
            } else {
                                    //(0,0) vector
                return Vector.getDefault();
            }
        } else if(centerB > centerA) {//if B is above A on axis
            if(maxA > minB) {//if the max point on A is above min on B
                double m = maxA - minB;
                if(m < overlap) {
                    overlap = m;
                    angle = axis.angle + Math.PI;
                }
            } else {
                                    //(0,0) vector
                return Vector.getDefault();
            }
        }
    }
            //if the overlap value has been set by the edges of Polygon A
    if(overlap != 10000) {
                    //returns the adjustment vector along overlap edge axis
        return new Vector(angle, overlap, true);
    } else {
                    (0,0) vector
        return Vector.getDefault();
    }
}

此代码有一个错误,在一个非常扁平的部分高于正方形块的情况下会导致问题,扁平部分认为它比实际情况下更远。结果,扁平件和方块相撞,将它们推到下图中的当前位置

这是初步阶段实际游戏的图片。右侧的蓝色方块在触摸之前已被顶部的挡块移动。这种情况发生在程序的开头,当时正方形位于屏幕的最左侧。

1 个答案:

答案 0 :(得分:0)

我同意Beta,你的算法看起来不正确(主要是因为我很确定多边形交叉点不是那么容易。只是为了检查两个形状的交集(没有任何智能),你必须检查是否任何边缘与另一个形状的任何边缘相交,但即使是中等简单的形状,这也很慢。

矩形交点很容易做到,三角交点不应该太难,如果你可以将你的形状分成其中一个(或者实际上任何一组具有相当简单公式的形状)它会让事情变得容易

您还可以查看this question

多边形交叉点是一个经过充分研究的问题,只有谷歌它你应该得到很多选择。