gpt4 book ai didi

algorithm - 圆线段碰撞检测算法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:13 24 4
gpt4 key购买 nike

我有一条从 A 到 B 的直线和一个位于 C 的半径为 R 的圆。

Image

检查直线是否与圆相交的好算法是什么?它发生在沿圆圈边缘的什么坐标处?

最佳答案

服用

  1. E是射线的起点,
  2. L是射线的终点,
  3. C 是您要测试的球体的中心
  4. r 是那个球体的半径

计算:
d = L - E(射线的方向向量,从开始到结束)
f = E - C(从中心球体到光线起点的向量)

然后通过..找到交集
堵漏:
P = E + t * d
这是一个参数方程:
Px = Ex + tdx
Py = Ey + tdy
进入
(x - h)2 + (y - k)2 = r2
(h,k) = 圆心。

Note: We've simplified the problem to 2D here, the solution we get applies also in 3D

获得:

  1. 展开x2 - 2xh + h2 + y2 - 2yk + k2 - r2 = 0
  2. 插入x = ex + tdx
    y = ey + tdy
    ( ex + tdx )2 - 2( ex + tdx> )h + h2 +( ey + tdy )2 - 2( ey + tdy> )k + k2 - r2 = 0
  3. 展开ex2 + 2extdx + t2d x2 - 2exh - 2tdxh + h2 +ey2 + 2eytdy + t2d y2 - 2eyk - 2tdyk + k2 - r 2 = 0
  4. t2( dx2 + dy2 ) +2t( exdx + eydy - dxh - dyk ) +ex2 + ey2 -2exh - 2eyk + h2 + k2 - r2 = 0
  5. 最后,t2( d · d ) + 2t( e · d - < strong>d · c ) + e · e - 2( e · c ) + c · c - r2 = 0
    其中,d 是向量 d,· 是点积。
  6. 然后,t2( d · d ) + 2t( d · ( e - c ) ) + ( e - c ) · ( e - c ) - r2 = 0
  7. f = e - ct2( d · d ) + 2t( d · f ) + f · f - r2 = 0

所以我们得到:
t2 * (d · d) + 2t*( f · d ) + ( f · f - r2 ) = 0

所以求解二次方程:

float a = d.Dot( d ) ;
float b = 2*f.Dot( d ) ;
float c = f.Dot( f ) - r*r ;

float discriminant = b*b-4*a*c;
if( discriminant < 0 )
{
// no intersection
}
else
{
// ray didn't totally miss sphere,
// so there is a solution to
// the equation.

discriminant = sqrt( discriminant );

// either solution may be on or off the ray so need to test both
// t1 is always the smaller value, because BOTH discriminant and
// a are nonnegative.
float t1 = (-b - discriminant)/(2*a);
float t2 = (-b + discriminant)/(2*a);

// 3x HIT cases:
// -o-> --|--> | | --|->
// Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit),

// 3x MISS cases:
// -> o o -> | -> |
// FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1)

if( t1 >= 0 && t1 <= 1 )
{
// t1 is the intersection, and it's closer than t2
// (since t1 uses -b - discriminant)
// Impale, Poke
return true ;
}

// here t1 didn't intersect so we are either started
// inside the sphere or completely past it
if( t2 >= 0 && t2 <= 1 )
{
// ExitWound
return true ;
}

// no intn: FallShort, Past, CompletelyInside
return false ;
}

关于algorithm - 圆线段碰撞检测算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1073336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com