N游戏教程碰撞检测凸/凹

时间:2011-06-20 16:26:12

标签: math collision-detection physics game-physics

在我的游戏之一处理碰撞检测的N游戏教程之后,有一点不清楚这个方法是他们在这个页面上讨论单独的轴定理但是如果你在实现中看到(教程A)没有哪里我可以看到单独的轴被处理。从下面的URL,--= round shapes =--部分讨论了如何处理AABB与凸/凹形状之间的碰撞。 http://www.metanetsoftware.com/technique/tutorialA.html#section2

我有矢量和分离轴实现的基本思想但不是这种方法,我从本教程中理解的是整个N游戏世界由5-8种不同的形状(平铺)构成,每个平铺依次水平/垂直旋转它提供了面向左,右,顶部和顶部的4个组合。底部。这些面向信息存储在每个瓦片中,即signx,y。

obj - 是玩家(矩形) t - 瓷砖 x,y - 边界框投影

实施:有人可以解释这段代码的确切含​​义吗?

function ProjAABB_Concave(x,y,obj,t)
{       
    //if distance from "innermost" corner of AABB is further than tile radius,
    //collision is occuring and we need to project

    var signx = t.signx;
    var signy = t.signy;

    var ox = (t.pos.x + (signx*t.xw)) - (obj.pos.x - (signx*obj.xw));//(ox,oy) is the vector form the innermost AABB corner to the
    var oy = (t.pos.y + (signy*t.yw))- (obj.pos.y - (signy*obj.yw));//circle's center

    var twid = t.xw*2;
    var rad = Math.sqrt(twid*twid + 0);//this gives us the radius of a circle centered on the tile's corner and extending to the opposite edge of the tile;
                                        //note that this should be precomputed at compile-time since it's constant

    var len = Math.sqrt(ox*ox + oy*oy);
    var pen = len - rad;
    if(0 < pen)
    {
        //collision; we need to either project along the axes, or project along corner->circlecenter vector

        var lenP = Math.sqrt(x*x + y*y);
        if(lenP < pen)
        {
            //it's shorter to move along axis directions
            obj.ReportCollisionVsWorld(x,y,x/lenP, y/lenP, t);

            return COL_AXIS;
        }
        else
        {
            //project along corner->circle vector
            ox /= len;//len should never be 0, since if it IS 0, rad should be > than len
            oy /= len;//and we should never reach here

            obj.ReportCollisionVsWorld(ox*pen, oy*pen, ox, oy, t);

            return COL_OTHER;
        }

    }

    return COL_NONE;

}

2 个答案:

答案 0 :(得分:1)

这是一个简单的球体 - 球体碰撞。查看他们所在的部分

var pen = len - rad;
if(0 < pen)

他们只是检查“t”物体半径减去当前物体半径是否接触(= 0)或交叉(&lt; 0)。

他们所做的部分

var ox = (t.pos.x + (signx*t.xw)) - (obj.pos.x - (signx*obj.xw));//(ox,oy) is the vector form the innermost AABB corner to the
var oy = (t.pos.y + (signy*t.yw))- (obj.pos.y - (signy*obj.yw));//circle's center

他们正在将“t”对象移动到“obj”参考框架。

答案 1 :(得分:0)

我认为展示底层物理算法会更好,然后你可以更快地理解代码