Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2d多边形碰撞响应_Java_2d_Physics_Collision - Fatal编程技术网

Java 2d多边形碰撞响应

Java 2d多边形碰撞响应,java,2d,physics,collision,Java,2d,Physics,Collision,我做了一个使用凸多边形的游戏。我的计划是让这些多边形根据它们的质量和速度碰撞和反弹。但首先我需要确保它们没有重叠。此代码检查多边形A的每条边,以查看多边形B的任何顶点是否在垂直于边的轴上重叠。整个方法返回修复多边形A所需的结果向量: /**Calculates adjustment vector for EntityPolygon A*/ public Vector calculateCollision(EntityPolygon A, EntityPolygon B) { /

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

    /**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();
    }
}
/**计算整个多边形A的调整向量*/
公共向量计算冲突(实体多边形A、实体多边形B){
//这是一个很大的数字,因此重叠的第一次比较是正确的
双重叠=10000;
//这是应用重叠向量的轴的角度
双角度=0;
//我为多边形的每一条边运行了一个for循环

对于(int x=0;x我同意Beta,您的算法看起来不正确(主要是因为我非常确定多边形相交不会那么容易。仅用于检查两个形状的相交(没有任何智能),则必须检查是否有任何边与其他形状的任何边相交,但即使是中等简单的形状,速度也很慢

矩形相交很容易,三角形相交应该不会太难,如果你能把你的形状分割成其中一个(或者任何一组有一个合理简单公式的形状),事情就会简单得多

你也可以退房


多边形相交是一个研究得很好的问题,只要用谷歌搜索一下,你就会得到很多选项。

看起来你的算法好像是错的。如果你给我们一些数字作为例子,解释起来会更容易。你提到了游戏中的图片,但我没有看到。