[英]2D collision resolution troubles
因此,我正在基于一个碰撞系统进行研究:尽管该博客进行了一些细微的更改,但由于我对该代码有一些问题,因此它在所有情况下都可以很好地工作,除了下面的内容外,这里还有一些屏幕截图(绿色在分辨率之前,红色在分辨率之后): 这是问题所在:
这是我的代码(很抱歉,它凌乱且未优化,我计划在理解算法后重写它):
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;
}
}
如果有人能指出我的问题,那将是很大的,如果不是,我觉得我很了解,可以用一种简单的方式编写它,并可能找出原因。
谢谢,尼科
哎呀,这是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.