2016-08-20 5 views

ответ

0

user3591040 его довольно сложно ....

Я надеюсь, что у вас есть данные связанные с кругом и линией.

equations and all polylines 

Вы хотите, чтобы найти эту точку

enter image description here **

  1. Рассмотрим этот образ Постараемся обнаружить любые столкновения

**

enter image description here Collision Detection Algorithm: попытаться реализовать этот алгоритм

Taking

E является отправной точкой луча, L является конечной точкой луча, C является центром сферы вы тестирование на r радиус этой сферы Compute:

d = L - E (Direction vector of ray, from start to end) 
f = E - C (Vector from center sphere to ray start) 

Тогда инте rsection находится по .. затыкать:

P = E + t * d 

Это параметрическое уравнение:

Px = Ex + tdx 
Py = Ey + tdy 

в

(x - h)2 + (y - k)2 = r2 
(h,k) = center of circle. 

Примечание: Мы упростили задачу 2D здесь, то решение, которое мы получаем, также применяется в 3D :

Увеличить

x2 - 2xh + h2 + y2 - 2yk + k2 - r2 = 0 

подключи

x = ex + tdx 
y = ey + tdy 
(ex + tdx)2 - 2(ex + tdx)h + h2 + (ey + tdy)2 - 2(ey + tdy)k + k2 - r2 = 0 
Explode 
ex2 + 2extdx + t2dx2 - 2exh - 2tdxh + h2 + ey2 + 2eytdy + t2dy2 - 2eyk - 2tdyk + k2 - r2 = 0 
Group 
t2(dx2 + dy2) + 2t(exdx + eydy - dxh - dyk) + ex2 + ey2 - 2exh - 2eyk + h2 + k2 - r2 = 0 
Finally, 
t2(_d * _d) + 2t(_e * _d - _d * _c) + _e * _e - 2(_e*_c) + _c * _c - r2 = 0 
*Where _d is the vector d and * is the dot product.* 
And then, 
t2(_d * _d) + 2t(_d * (_e - _c)) + (_e - _c) * (_e - _c) - r2 = 0 
Letting _f = _e - _c 
t2(_d * _d) + 2t(_d * _f) + _f * _f - r2 = 0 
So we get: 
t2 * (d DOT d) + 2t*(f DOT d) + (f DOT 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 ; 
    } 
Смежные вопросы