0
我正在實施一個簡單的遊戲,它包含了三維邏輯。爲了檢測移動的球體是否與圓柱碰撞,程序或/和相關方程是什麼?我的圓柱體是靜態的正圓形,並與z軸對齊。我讀氣缸的在這種情況下方程爲(x - 一)2 +(Z - B)2 = R 2,其中碰撞檢測 - 移動球體和圓柱體之間的響應
(A,B):氣缸 爲r的中心:圓柱體的半徑
如何我可以用它來找出這兩個物體是否相交,另外,我怎樣才能應用相應的球反應?
謝謝
我正在實施一個簡單的遊戲,它包含了三維邏輯。爲了檢測移動的球體是否與圓柱碰撞,程序或/和相關方程是什麼?我的圓柱體是靜態的正圓形,並與z軸對齊。我讀氣缸的在這種情況下方程爲(x - 一)2 +(Z - B)2 = R 2,其中碰撞檢測 - 移動球體和圓柱體之間的響應
(A,B):氣缸 爲r的中心:圓柱體的半徑
如何我可以用它來找出這兩個物體是否相交,另外,我怎樣才能應用相應的球反應?
謝謝
通常,一個氣缸到球體的測試可以被簡化爲一個點到線段測試,其中:
妥協與這是它使它有效地球囊測試而不是球體圓柱體。如果你能忍受這種妥協,這裏有一些僞代碼。
vector cylCenterVector = endPoint2 - endpoint1;
float distanceFactorFromEP1 = Dot(sphereCenter - endPoint1)/Dot(cylCenterVector , cylCenterVector);
if(distanceFactorFromEP1 < 0) distanceFactorFromEP1 = 0;// clamp to endpoints if neccesary
if(distanceFactorFromEP1 > 1) distanceFactorFromEP1 = 1;
vector closestPoint = endPoint1 + (cylCenterVector * distanceFactorFromEP1);
vector collisionVector = sphereCenter - closestPoint;
float distance = collisionVector.Length();
vector collisionNormal = collisionVector/distance;
if(distance < sphereRadius + cylRadius)
{
//collision occurred. use collisionNormal to reflect sphere off cyl
float factor = Dot(velocity, collisionNormal);
velocity = velocity - (2 * factor * collisionNormal);
}
你是什麼意思的膠囊?你的意思是圓柱體的頂部和底部會有半個球體嗎? – CoachNono 2013-09-18 18:53:54
@ CoachNono是的,這是正確的。 – 2013-09-18 20:29:44
只是指出了一個錯誤,我相信在第一個dot產品中應該有一個cylCenterVector:點(sphereCenter - endPoint1,cylCenterVector)/點(cylCenterVector,cylCenterVector) – 2015-12-19 14:37:56