C# SAT中的碰撞仅在两个方向上工作
我试图通过XNA中的SAT解决碰撞问题,但它只在底部和右侧两个方向起作用。我认为这个问题是由于轴有负分量时,重叠未通过长度检查造成的 冲突解决方法:C# SAT中的碰撞仅在两个方向上工作,c#,math,xna,collision,C#,Math,Xna,Collision,我试图通过XNA中的SAT解决碰撞问题,但它只在底部和右侧两个方向起作用。我认为这个问题是由于轴有负分量时,重叠未通过长度检查造成的 冲突解决方法: public Vector2 Collides(Rect other, float selfr, float otherr) { Vector2 minTranslationVector = new Vector2(Single.PositiveInfinity, Single.PositiveInfinit
public Vector2 Collides(Rect other, float selfr, float otherr)
{
Vector2 minTranslationVector = new Vector2(Single.PositiveInfinity, Single.PositiveInfinity);
foreach (Vector2 v in GetRotatedNormals(selfr))
{
float[] minMaxA = ProjectRotatedAlongAxis(selfr, v);
float[] minMaxB = other.ProjectRotatedAlongAxis(otherr, v);
float minA = minMaxA[0];
float maxA = minMaxA[1];
float minB = minMaxB[0];
float maxB = minMaxB[1];
if (!(maxA > minB && maxB > minA))
return new Vector2(0, 0);
float overlap = Math.Min(maxA - minB, maxB - minA);
if (Math.Pow(overlap, 2) < minTranslationVector.LengthSquared())
{
minTranslationVector = v * overlap;
}
}
foreach (Vector2 v in other.GetRotatedNormals(otherr))
{
float[] minMaxA = ProjectRotatedAlongAxis(selfr, v);
float[] minMaxB = other.ProjectRotatedAlongAxis(otherr, v);
float minA = minMaxA[0];
float maxA = minMaxA[1];
float minB = minMaxB[0];
float maxB = minMaxB[1];
if (!(maxA > minB && maxB > minA))
return new Vector2(0, 0);
float overlap = Math.Min(maxA - minB, maxB - minA);
if (Math.Pow(overlap, 2) < minTranslationVector.LengthSquared())
{
minTranslationVector = v * overlap;
}
}
return minTranslationVector;
}
在我们帮助您之前,您需要解释变量的含义。在2D中使用旋转矩形正确吗?
foreach (GameObject obj in room.Objects)
{
if (!obj.HasCollisions)
continue;
Rect playerRect = new Rect(BoundingBox);
Vector2 minTranslationVector = playerRect.Collides(new Rect(obj.BoundingBox), 0, 0);
playerRect.topLeft += minTranslationVector;
toDraw += playerRect.isNegative;
Position = playerRect.topLeft;
BoundingBox = playerRect.ToRectangle();
}