简体   繁体   中英

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:

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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