我一直在使用一篇文章中的教程编写碰撞检测系统,但是我无论如何都不能让它完美地运行起来。以下是代码:
BoundingBox aBox;
aBox.Convert(a);
BoundingBox bBox;
bBox.Convert(b);
Vector2 aMin = aBox.GetTopLeft();
Vector2 aMax = aBox.GetBotRight();
Vector2 bMin = bBox.GetTopLeft();
Vector2 bMax = bBox.GetBotRight();
Vector2 minDistance;
float left = (bMin.x - aMax.x);
float right = (bMax.x - aMin.x);
float top = (bMin.y - aMax.y);
float bottom = (bMax.y - aMin.y);
// Check for intersection internally
if (left > 0 || right < 0) return;
if (top > 0 || bottom < 0) return;
// Find the minDistance
if (abs(left) < right)
minDistance.x = left;
else
minDistance.x = right;
if (abs(top) < bottom)
minDistance.y = top;
else
minDistance.y = bottom;
// Null axis with biggest value
if (abs(minDistance.x) < abs(minDistance.y))
minDistance.y = 0;
else
minDistance.x = 0;
目前的问题是,角色在走路并抱着一个块的顶部和底部时没问题,但一旦他必须向上走两个块,他就会被卡在它们之间。巧合的是,向下走完全没有问题。只是想知道是否有人有解决这个问题的方法,非常感谢!
以下是更好地展示问题的GIF: