Question

I think it'll be enough if I understand what

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

does

Python code founded here 6. answer by @Fnord

I was looking at their doc, but I couldn't understand a thing it's even worst than in MSDN doc

EDIT:

I deleted my code as my assumptions were totally incorrect.

even if my original Question wasn't answered my original problem was solved.

Problem:

how to find 2 closest points between two skew lines in 3D

this gr8 tutorial could've helped me IF I'd understood how to learn programmatically what t and s is.

Was it helpful?

Solution

You don't need to rewrite these methods, as there are already equivalent methods built-in.

For example, to get the distance between two 3-dimensional vectors, you can use Vector3.Distance (Unity entry)

float distance = Vector3.Distance(someVector, anotherVector);

If you want to find the two closest points, you should be able to accomplish this using other vector methods as well.

Here is an example of how to use the methods to find the closest points on two difference lines (taken from the Unity wiki). It still uses the determinants to calculate this. For an explanation of why this works, you need to understand basic linear algebra for vectors.

//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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top