当矩形从一个位置移动到另一个位置时,如果它错过了行进距离中的碰撞,则进行游戏碰撞检查

时间:2013-12-19 20:59:26

标签: java android collision-detection game-physics raycasting

我知道这个问题与其他人类似,但如果我有一个矩形有界游戏对象。哪个移动位置。 如果它与中间的任何项目相交,我该怎么检查它?

在极端情况下。 [x = 2,x = 1,宽度= 1,高度= 1] A移动到[x = 4,y = 1,宽度= 1,高度= 1]。如果矩形B存在于[3,1,0.5,0.5]那么它将被遗漏。

我已经阅读过关于标量和交叉产品的内容,但如果我正确阅读它们就是单行。这是因为Android游戏开发速度慢,设备速度慢。我让它落入物体。我使用下面的代码检查交叉点。

public boolean testIntersection(GameVector lowerLeftMain, float mainWidth, float            mainHeight, GameVector lowerLeftCollider,
float colliderWidth, float colliderHeight){

    boolean intersect = false;

    if(lowerLeftMain.x < lowerLeftCollider.x + colliderWidth+0.08f && //checks left collision
            lowerLeftMain.x + mainWidth > lowerLeftCollider.x-0.08f && //checks right collision
            lowerLeftMain.y < lowerLeftCollider.y + colliderHeight+0.08f &&//checks top collision
            lowerLeftMain.y + mainHeight > lowerLeftCollider.y-0.08f )//checks bottom collision
        intersect = true;
    return intersect;
}

如果我放弃矩形并专注于射线投射线碰撞风格,请有人指出我正确的方向吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

感谢链接很棒的链接会发布我的代码以帮助其他人。

我在java中的分离轴定理。仅测试是否重叠。由于效率和潜力可以看到最小和最大重叠矢量,我选择了这种算法。

public GameVector[] getVertices(GameObject obj){
    final GameVector topLeft = new GameVector( obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y+0.06f +(obj.mHeight/2) );
    final GameVector topRight = new GameVector(   obj.mPosition.x+0.06f + (obj.mWidth/2),obj.mPosition.y+0.06f +(obj.mHeight/2) );
    final GameVector bottomLeft = new GameVector(  obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));
    final GameVector bottomRight = new GameVector(  obj.mPosition.x+0.06f + (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));

    //order here matters
    GameVector[] vertices = { topLeft, topRight, bottomRight, bottomLeft }; 
    return vertices;
}

public GameVector[] getAxis(GameObject shape){

    GameVector[] vertices = getVertices(shape);

    GameVector[] axes = new GameVector[vertices.length];
    // loop over the vertices
    for (int i = 0; i < vertices.length; i++) {
        // get the current vertex
        GameVector p1 = vertices[i];
        // get the next vertex if i+1 == vertices length set back to vertices [0]
        GameVector p2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
        // subtract the two to get the edge vector
        GameVector edge = p1.subtract(p2.x, p2.y);
        // get either perpendicular vector
        GameVector normal;
        //get the left side normal of the vector due to clock wise positions
        normal = new GameVector(edge.y, -edge.x);//edge.perp();
        axes[i] = normal;
    }
    return axes;
}

public float dotProduct(GameVector a, GameVector b){
    float dp = a.x*b.x + a.y*b.y;
    return dp;
}

public class Projection {

    private final float min;
    private final float max;

    public Projection(float min, float max) {
        this.min = min;
        this.max = max;
    }

    public boolean doesOverlap(final Projection other) {
        return !(this.min > other.max || other.min > this.max);

    }
}