[英]translate Python in to Unity C# - Maths OR how to find Shortest distance between two skew lines in 3D space
您不需要重寫這些方法,因為已經內置了等效的方法。
例如,要獲取兩個3維向量之間的距離,可以使用Vector3.Distance
( Unity條目 )
float distance = Vector3.Distance(someVector, anotherVector);
如果要找到兩個最接近的點,則也應該可以使用其他矢量方法來完成。
這是一個示例,說明如何使用這些方法在兩條差異線上找到最接近的點( 摘自Unity Wiki )。 它仍然使用行列式來計算。 為了解釋為什么這樣做,您需要了解向量的基本線性代數。
//Two non-parallel lines which may or may not touch each other have a point on each line which are closest
//to each other. This function finds those two points. If the lines are not parallel, the function
//outputs true, otherwise false.
public static bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
closestPointLine1 = Vector3.zero;
closestPointLine2 = Vector3.zero;
float a = Vector3.Dot(lineVec1, lineVec1);
float b = Vector3.Dot(lineVec1, lineVec2);
float e = Vector3.Dot(lineVec2, lineVec2);
float d = a*e - b*b;
//lines are not parallel
if(d != 0.0f){
Vector3 r = linePoint1 - linePoint2;
float c = Vector3.Dot(lineVec1, r);
float f = Vector3.Dot(lineVec2, r);
float s = (b*f - c*e) / d;
float t = (a*f - c*b) / d;
closestPointLine1 = linePoint1 + lineVec1 * s;
closestPointLine2 = linePoint2 + lineVec2 * t;
return true;
}
else{
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.