如何检查矩形是否与三个点之间的区域重叠? (C# 和 XNA)
How do I check if a Rectangle overlaps the area between three Points? (C# and XNA)
我正在用 C# 和 XNA 制作游戏,我使用三个点来表示敌方对象的碰撞箱。 Rectangle 变量用于表示玩家的碰撞箱。我为敌人使用了三个点而不是矩形,因为敌人的形状像三角形。
//Example coordinates
Point pointOne = new Point(0, 50);
Point pointTwo = new Point(50, 50);
Point pointThree = new Point(25, 0);
Rectangle rect = new Rectangle(0, 0, 10, 10);
我需要一种方法来确定矩形是否与所有点之间的区域重叠,如下图所示。任何人都可以向我展示可以帮助我完成此任务的方法或某种代码吗?
这是另一种实现方式:
class Triangle
{
Vector2 topPoint, rightPoint, leftPoint;
public Triangle(Vector2 p1, Vector2 p2, Vector2 p3)
{
topPoint = p1;
rightPoint = p2;
leftPoint = p3;
}
public bool IsRectIntersecting(List<Vector2> corners)//corners are the corners of the polygon being tested
{
if (AreAnyOfTheCornesInsideTriangleLineSegment(topPoint, rightPoint, corners) &&
AreAnyOfTheCornesInsideTriangleLineSegment(rightPoint, leftPoint, corners) &&
AreAnyOfTheCornesInsideTriangleLineSegment(leftPoint, topPoint, corners))
{
return true;
}
return false;
}
private bool AreAnyOfTheCornesInsideTriangleLineSegment(Vector2 pointA, Vector2 pointB, List<Vector2> corners)
{
Vector2 lineSegment = pointA - pointB;
Vector3 lineSegment3D = new Vector3(lineSegment, 0);
Vector3 normal3D = Vector3.Cross(lineSegment3D, Vector3.UnitZ);
Vector2 normal = new Vector2(normal3D.X, normal3D.Y);
foreach (Vector2 corner in corners)
{
if (Vector2.Dot(normal, corner - pointB) < 0)
{
return true;
}
}
return false;
}
}
我正在用 C# 和 XNA 制作游戏,我使用三个点来表示敌方对象的碰撞箱。 Rectangle 变量用于表示玩家的碰撞箱。我为敌人使用了三个点而不是矩形,因为敌人的形状像三角形。
//Example coordinates
Point pointOne = new Point(0, 50);
Point pointTwo = new Point(50, 50);
Point pointThree = new Point(25, 0);
Rectangle rect = new Rectangle(0, 0, 10, 10);
我需要一种方法来确定矩形是否与所有点之间的区域重叠,如下图所示。任何人都可以向我展示可以帮助我完成此任务的方法或某种代码吗?
这是另一种实现方式:
class Triangle
{
Vector2 topPoint, rightPoint, leftPoint;
public Triangle(Vector2 p1, Vector2 p2, Vector2 p3)
{
topPoint = p1;
rightPoint = p2;
leftPoint = p3;
}
public bool IsRectIntersecting(List<Vector2> corners)//corners are the corners of the polygon being tested
{
if (AreAnyOfTheCornesInsideTriangleLineSegment(topPoint, rightPoint, corners) &&
AreAnyOfTheCornesInsideTriangleLineSegment(rightPoint, leftPoint, corners) &&
AreAnyOfTheCornesInsideTriangleLineSegment(leftPoint, topPoint, corners))
{
return true;
}
return false;
}
private bool AreAnyOfTheCornesInsideTriangleLineSegment(Vector2 pointA, Vector2 pointB, List<Vector2> corners)
{
Vector2 lineSegment = pointA - pointB;
Vector3 lineSegment3D = new Vector3(lineSegment, 0);
Vector3 normal3D = Vector3.Cross(lineSegment3D, Vector3.UnitZ);
Vector2 normal = new Vector2(normal3D.X, normal3D.Y);
foreach (Vector2 corner in corners)
{
if (Vector2.Dot(normal, corner - pointB) < 0)
{
return true;
}
}
return false;
}
}