【问题标题】:Triangles intersection三角形相交
【发布时间】:2021-09-27 16:48:16
【问题描述】:

我正在开发用于多边形的 2D 物理引擎。让我烦恼的是找到三角形 - 三角形的交点。我正在组合三个点来组成三角形(x1,y1), (x2,y2), (x3,y3) 或点a, b, and c,这对两个三角形都完成了。

如何使用这些点来确定两个三角形是否重叠?如果您能找到碰撞的位置并指明他们的行进方向,则可获得奖励积分。

【问题讨论】:

标签: math 2d physics trigonometry intersection


【解决方案1】:

我终于找到了一个不错的算法,可以找到两个一般三角形之间最近的路径。

GitHub 上查看完整的C# 代码以及下面的一些亮点

几何类

public readonly struct Triangle 
{
    public Vector2 A { get; }
    public Vector2 B { get; }
    public Vector2 C { get; }
    public bool Contains(Vector2 point) { }
    public Contact GetClosestPoints(Vector2 other) { }
    public Contact GetClosestPoints(Side other) { }
    public Contact GetClosestPoints(Triangle other) { }
}

public readonly struct Side
{
    public Vector2 A { get; }
    public Vector2 B { get; }
    public bool Contains(Vector2 point) { }
    public Contact GetClosestPoints(Vector2 other) { }
    public Contact GetClosestPoints(Side other) { }
}

public readonly struct Line
{
    public float A { get; }
    public float B { get; }
    public float C { get; }
    public bool Contains(Vector2 point) { }
    public Vector2 Project(Vector2 point) { }

}

public readonly struct Contact 
{
    public Contact(Vector2 source, Vector2 target)
    {
        Source = source;
        Target = target;
    }
    public Vector2 Source { get; }
    public Vector2 Target { get; }
    public Vector2 Direction { get => { }; }
    public float Distance { get => { }; }

    public Contact Flip() => new Contact(Target, Source);
}

算法

找到三角形之间的最近点,涉及以下步骤

  1. 从三角形 B 中找出三角形 A 与以下 6 个几何形状中的每一个最近的点
  • 取三角形 B 的每个顶点,找到离三角形 A 最近的点
    1. 求三角形 A 的每个顶点的距离
    2. 求三角形 A 各边的距离。
    3. 最近的点可能在一侧,或者在一侧的一端
  • 取三角形 B 的每一边,找到离三角形 A 最近的点
    1. 查找到三角形 A 的每个顶点的距离。最近的点可能在边上 或在一端
    2. 求边的端点到三角形 A 的距离
  1. 对 6 个距离中的每一个进行排序并选择最小值
  2. 将最小值与几何元素匹配。
  3. 使用匹配的最近点对创建一个Contact 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-21
    • 2023-03-19
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多