簡體   English   中英

找到兩個球體之間的單個交點

[英]finding single intersection point between two spheres

我正在研究一個 C++ 問題,我正在嘗試創建一個實用函數 func(),它將 3d 空間 [(x,y,z) 和半徑 r] 中的兩條線段起點作為輸入。 如果這些段的方向可以使它們在同一點結束,則該函數應返回 true 並打印出該點。 如果有多個方向會產生一個公共端點,則函數應該選擇在hint_direction 指示的方向上最遠的方向。

該函數接收以下值:

bool func(
point3d position_0,               // origin of first line segment.
float length_0,                   // length of first line segment.
point3d position_1,               // origin of second line segment.
float length_1,                   // length of second line segment.
vector3d hint_direction,          // in the event there are multiple solutions, return the one furthest in this direction.
point3d *out_common_end_position) // if result is true, point where both line segments can be oriented to end. otherwise uninitialized.

我正在研究第一個邊緣情況; 有一個交點的地方,如下圖所示。

(圖片鏈接 = https://i.stack.imgur.com/dh9Vr.png

我的代碼正確識別了這種邊緣情況,但我不確定如何以編程方式找到這個交點。

//calling function with example from image above, cords, radius, and hint
bool result = func({1, 1, 0}, 1.0, {3, 1, 0}, 1.0, {0, 0, 1}, &common_end_position);

bool func(point3d position_0, float length_0, point3d position_1, float length_1,vector3d hint_direction,point3d *out_common_end_position){

    //if statement detecting single intersection
    if(length_0 + length_1 == d){
        printf("intersection at a single point\n");
        //find single intersection point (?)
    }

我一直在關注一些在線指南,其中列出了如何執行此操作,例如: https : //gamedev.stackexchange.com/questions/75756/sphere-sphere-intersection-and-circle-sphere-intersection其中說:“如果r_1 + r_2 == d,則交點為單點,位於 c_1 到 c_2 直線上距離 r_1 處,或:c_i = c_1 + (c_2 - c_1) * r_1/d"

自從我做這樣的幾何以來已經很長時間了,如果我想找到單個交點,我該如何使用上面的等式 "c_i = c_1 + (c_2 - c_1) * r_1/d" ? 我知道 c_2 - c_1 是兩個中心之間的距離,我之前在我的程序中計算為 float d,但我確定“c_1 +”是什么意思,因為 c_1 指的是整組繩索 (x,y ,z)。

總的來說,我正在嘗試找到一種方法來獲得單個交點,例如在我的圖像中,有人可以幫助他們理解上面的鏈接解決方案嗎? 在此期間,我將繼續研究解決方案。 謝謝你。

我知道 c_2 - c_1 是兩個中心之間的距離

不正確。 c_2 - c_1是描述從c_1c_2的方向並且具有大小d的向量(在幾何意義上,而不是 c++ 意義上)。 (c_2 - c_1)/d因此是描述從c_1c_2方向的單位向量。

(c_2 - c_1)/d * r_1因此是c_1c_2之間的線上的點,即距離c_1 r_1距離,或者,圓的交點。

但我確定“c_1 +”是什么意思,因為 c_1 指的是整套繩索(x,y,z)。

c_1不是圓上的全部點集。 c_1是圓 1 的中心。

還要記住,您正在處理浮動。 因此,它容易出現浮點錯誤 在這種情況下尋找完全相等會出錯。 考慮查看d是否在r1 + r2某個可接受的容錯范圍內可能更合適

((r1 + r2 - e) <= d && d <= (r1 + r2 + e)) 

代替

d == r1 + r2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM