簡體   English   中英

將Python轉換為Unity C#-數學或如何在3D空間中找到兩條偏斜線之間的最短距離

[英]translate Python in to Unity C# - Maths OR how to find Shortest distance between two skew lines in 3D space

我想如果我了解什么就足夠了

np.linalg.norm(A)
np.linalg.det([t, _A, cross])
np.cross(_A, _B)

在此處建立Python代碼6. @Fnord回答

我當時在看他們的文檔 ,但我不明白它比MSDN文檔還差

編輯:

我刪除了代碼,因為我的假設完全不正確。

即使我的原始問題沒有得到回答,我的原始問題也已解決。

問題:

如何在3D中的兩條偏斜線之間找到2個最近的點

如果我了解如何以編程方式學習t和s是什么,那么這份gr8 教程可以對我有所幫助。

您不需要重寫這些方法,因為已經內置了等效的方法。

例如,要獲取兩個3維向量之間的距離,可以使用Vector3.DistanceUnity條目

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.

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