首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >N游戏教程碰撞检测凸/凹

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

Stack Overflow用户
提问于 2011-06-20 16:26:12
回答 2查看 1.3K关注 0票数 1

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

我有矢量和独立轴实现的基本思想,但不是这个方法,我从本教程中了解到的是,整个N游戏世界是由5-8个不同的形状(瓷砖)组成的,每个瓷砖依次水平地/垂直地旋转,这四个组合面向左、右、上和下。这些面向信息存储在每个瓷砖,这是信号,y。

obj -是播放器(矩形)t-瓷砖x,y-包围盒投影。

实现:有人能解释一下这段代码到底在做什么吗?

代码语言:javascript
复制
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;

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-20 16:49:20

这是一个简单的球体碰撞。看他们做的那部分

代码语言:javascript
复制
var pen = len - rad;
if(0 < pen)

他们只是检查"t“对象半径减去当前对象半径是否是接触(=0)或交叉(<0)。

他们所做的部分

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-06-20 16:32:45

我认为更好的方法是展示底层的物理算法,这样您就可以更快地理解代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6414432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档