简体   繁体   English

2D碰撞解决麻烦

[英]2D collision resolution troubles

So I'm working on a collision system based off: this blog , although with some minor changes, as I had some issues with that code, and its working pretty good for all cases, except coming up from below, here are some screenshots(green is before resolution, red is after): 因此,我正在基于一个碰撞系统进行研究:尽管该博客进行了一些细微的更改,但由于我对该代码有一些问题,因此它在所有情况下都可以很好地工作,除了下面的内容外,这里还有一些屏幕截图(绿色在分辨率之前,红色在分辨率之后): 对最佳剩下 and here is the problem: 这是问题所在: 底部

this is my code (sorry its messy and unoptimized, I was planning to rewrite it once I understood the algorithm): 这是我的代码(很抱歉,它凌乱且未优化,我计划在理解算法后重写它):

public function ResolveCollision(a : CollisionComponent, b : CollisionComponent)
    {
        var aAABB = new AABB(new Point(a.GetBounds().x, a.GetBounds().y), 
                            new Point(a.GetBounds().x + a.GetBounds().width, 
                            a.GetBounds().y + a.GetBounds().height));
        var bAABB = new AABB(new Point(b.GetBounds().x, b.GetBounds().y), 
                            new Point(b.GetBounds().x + b.GetBounds().width, 
                            a.GetBounds().y + a.GetBounds().height));

        var direction : Point = new Point();
        direction.x = aAABB.topLeft.x - bAABB.topLeft.x;
        direction.y = aAABB.topLeft.y - bAABB.topLeft.y;

        var end : AABB = new AABB();    
        end.bottomRight.x = Math.min(aAABB.bottomRight.x, bAABB.bottomRight.x);
        end.bottomRight.y = Math.min(aAABB.bottomRight.y, bAABB.bottomRight.y);

        end.topLeft.x = Math.max(aAABB.topLeft.x, bAABB.topLeft.x);
        end.topLeft.y = Math.max(aAABB.topLeft.y, bAABB.topLeft.y);

        var overlap : Point = new Point();
        overlap.x = end.bottomRight.x - end.topLeft.x;
        overlap.y = end.bottomRight.y - end.topLeft.y;

        var moveAxis : Int; //0:x, 1:y
        if (overlap.x < overlap.y)
        {
            moveAxis = 0;
        }
        else
        {
            moveAxis = 1;
        }

        if (moveAxis == 0)
        {
            a.Move(new Point(sign(direction.x) * overlap.x, 0));
        }
        else if (moveAxis == 1)
        {
            a.Move(new Point(0, sign(direction.y) * overlap.y));
        }
    }

    private function sign(i : Float) : Int
    {
        if (i < 0)
        {   
            return -1;
        }
        else if ( i > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

if anyone could point me to the problem, that would be great, if not, I feel like I understand it well enough to write it in a simpler way, and possibly figure out the cause... 如果有人能指出我的问题,那将是很大的,如果不是,我觉得我很了解,可以用一种简单的方式编写它,并可能找出原因。

thanks, Nico 谢谢,尼科

oops it was a dumb mistake in the AABB creation (where of course I didn't check), I was initializing some of the "b" boxes properties to those of the "a" box: 哎呀,这是AABB创建中的一个愚蠢的错误(当然我没有检查),我正在将一些“ b”框属性初始化为“ a”框的属性:

 var bAABB = new AABB(new Point(b.GetBounds().x, b.GetBounds().y), 
                            new Point(b.GetBounds().x + b.GetBounds().width, 
                            **a.GetBounds()**.y + **a.GetBounds()**.height));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM