游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2138|回复: 3

关于用面积求线段交点的问题

[复制链接]

1

主题

2

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2013-5-26 13:06:03 | 显示全部楼层 |阅读模式
请教一个问题,实时碰撞检测算法技术一书中,用面积求两线段交点,求告之参数 t 的推导过程
// Test if segments ab and cd overlap. If they do, compute and return
// intersection t value along ab and intersection position p
int Test2DSegmentSegment(Point a, Point b, Point c, Point d, float &t, Point &p)
{
  // Sign of areas correspond to which side of ab points c and d are
  float a1 = Signed2DTriArea(a, b, d); // Compute winding of abd (+ or -)
  float a2 = Signed2DTriArea(a, b, c); // To intersect, must have sign opposite of a1
  // If c and d are on different sides of ab, areas have different signs
  if (a1 * a2 < 0.0f)
  {
    // Compute signs for a and b with respect to segment cd
    float a3 = Signed2DTriArea(c, d, a); // Compute winding of cda (+ or -)
    // Since area is constant a1 - a2 = a3 - a4, or a4 = a3 + a2 - a1
    // float a4 = Signed2DTriArea(c, d, b); // Must have opposite sign of a3
    float a4 = a3 + a2 - a1;
    // Points a and b on different sides of cd if areas have different signs
    if (a3 * a4 < 0.0f)
    {
      // Segments intersect. Find intersection point along L(t) = a + t * (b - a).
      // Given height h1 of a over cd and height h2 of b over cd,
      // t = h1 / (h1 - h2) = (b*h1/2) / (b*h1/2 - b*h2/2) = a3 / (a3 - a4),
      // where b (the base of the triangles cda and cdb, i.e., the length
      // of cd) cancels out.
      t = a3 / (a3 - a4);// 此公式是怎么推导出来的?
      p = a + t * (b - a);
      return 1;
    }
  }
  // Segments not intersecting (or collinear)
  return 0;
}

0

主题

8

帖子

62

积分

注册会员

Rank: 2

积分
62
发表于 2013-5-26 18:02:03 | 显示全部楼层
t = h1 / (h1 - h2) = (b*h1/2) / (b*h1/2 - b*h2/2) = a3 / (a3 - a4)
那个b指的是点c和点d的距离。h1是三角形cda的高,h2是三角形cdb的高。因为这两个三角形有相同的底,所以可以用面积来算,免得把h1和h2算出来。

1

主题

2

帖子

7

积分

新手上路

Rank: 1

积分
7
 楼主| 发表于 2013-5-26 20:47:20 | 显示全部楼层
stuartc 发表于 2013-5-26 18:02
t = h1 / (h1 - h2) = (b*h1/2) / (b*h1/2 - b*h2/2) = a3 / (a3 - a4)
那个b指的是点c和点d的距离。h1是三 ...

关键是怎么推导出 t = h1 / ( h1- h2)的?为什么参数t可以用h1和h2来表示?

0

主题

8

帖子

62

积分

注册会员

Rank: 2

积分
62
发表于 2013-5-27 12:37:35 | 显示全部楼层
那是因为相似三角形的原因。把那两个三角形的高画出来,然后那两个里面的直角三角形相似,对应边的长成比例。设l1、l2为斜边的长就能推导出t = l1 / (l1 + l2)。因为 l1/l2 = h1/h2,也可以用h1和h2来表示。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-26 19:07

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表