分离轴定理时出错

时间:2017-05-08 20:38:30

标签: java collision-detection separating-axis-theorem

我一直在努力实施SAT很长一段时间,这是我的最后一招,在对我的代码进行目测检查之后,似乎我正确地得到了我正在检查的形状的面部法线(轴),并正确地将每个顶点投影到每个轴上,但SAT返回false是因为它没有检测到重叠,如果有人能指出我正确的方向,我将不胜感激。

SAT班级:( DevEntity)

public void obtainEdges(){

    for(int i = 0; i < vertices.length; i++){

        Vector2 e1 = vertices[i];

        Vector2 e2 = vertices[i + 1 == vertices.length ? 0 : i + 1];

        Vector2 edge = e1.subtract(e2);

        Vector2 perp = edge.perp(edge);

        axis[i] =  perp;
    }
}

public Vector2 projectAxis(Vector2 axis){
    float min = axis.dot(vertices[0]);
    float max = min;

    for(int i = 1; i < vertices.length; i++){
        float proj = axis.dot(vertices[i]);

        if(proj < min){
            min = proj;
        }

        if(proj > max){
            max = proj;
        }
    }

        proj = new Vector2(min,max);
        return proj;
}

public boolean separatingAxisTheorem(){
    obtainEdges();
    for(DevEntity e : handler.getDevWorld().getDevM().getDevEntities()){            
        if(e.equals(this)){
            return false;
        }


        Vector2[] axes1 = axis;
        Vector2[] axes2 = e.axis;

        for(int i = 0; i < axes1.length; i++){

            Vector2 p1 = projectAxis(axes1[i]);
            Vector2 p2 = e.projectAxis(axes1[i]);

            if(!p1.overlap(p2)){
                return false;
            }
        }

        for(int i = 0; i < axes2.length; i++){

            Vector2 p1 = projectAxis(axes2[i]);
            Vector2 p2 = e.projectAxis(axes2[i]);
            if(!p1.overlap(p2)){
                return false;
            }
        }

    }
    return true;
}

Vector Class:

public class Vector2 {

public float x, y;


public Vector2(float x, float y){
    this.x = x;
    this.y = y;

}

public Vector2(Vector2 vec){
    this.x = vec.x;
    this.y = vec.y;

}

public float dot(Vector2 b){
    float d = b.x * x + b.y * y;
    return d;
}

public Vector2 normalize(Vector2 vec){
    float mag = (float) Math.sqrt(vec.x * vec.x + vec.y * vec.y);
    Vector2 b = new Vector2(vec.x/mag, vec.y/mag);
    return b;
}

public Vector2 subtract(Vector2 vec){
    Vector2 s = new Vector2(x - vec.x, y - vec.y);
    return s;
}

public Vector2 subtract(float x, float y){
    return new Vector2(this.x - x, this.y - y);
}

public Vector2 perp(Vector2 vec){
    Vector2 p = new Vector2(-vec.y, vec.x);
    return p;
}

public boolean overlap(Vector2 vec){
        if(y > vec.x && vec.y > x){
            return true;
        }
    return false;
}

0 个答案:

没有答案