所以我正在研究基于this blog的碰撞系统, 虽然有一些小的改动,因为我对该代码有一些问题,并且它的工作对所有情况都很好,除了从下面来, 这里有一些截图(绿色在分辨率之前,红色在之后): 以下是问题所在:
这是我的代码(对不起它的杂乱和未经优化,我计划在我理解算法后重写它):
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;
}
}
如果有人能指出我的问题,那就太好了,如果没有,我觉得我很清楚它能够以更简单的方式写出来,并且可能找出原因......
感谢, 尼科
答案 0 :(得分:0)
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));