Get the point of the intersection between two lines not working

Hello

I am using this bit of code with a method provided by the wiki - but it gives the wrong intersection point.

public Vector3 A = new Vector3(15,0,5);
public Vector3 B = new Vector3(15,0,10);
public Vector3 C = new Vector3(10,0,8);
public Vector3 D = new Vector3(20,0,8);
public Vector3 intersectPoint;


private void Update()
{
       Debug.Log(Maths.LineLineIntersection(out intersectPoint, A, B, C, D));
}

The result gives True, but the intersection point is: (33.75, 0 , 17.5) which is obviously wrong.

This is the method i am using:

public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2)
{
        Vector3 lineVec3 = linePoint2 - linePoint1;
        Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2);
        Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2);

        float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);

        //is coplanar, and not parrallel
        if (Mathf.Abs(planarFactor) < 0.0001f && crossVec1and2.sqrMagnitude > 0.0001f)
        {
            float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;
            intersection = linePoint1 + (lineVec1 * s);
            return true;
        }
        else
        {
            intersection = Vector3.zero;
            return false;
        }
}

The code is from:
http://wiki.unity3d.com/index.php/3d_Math_functions

Why doesn’t it give the correct point of intersection ?

1 Like

That looks like the correct intersection point to me. Adding the direction vectors to the position vectors gives a very close value to both each other and the supposed intersection point.

But .x = 33.75 is no where near any of the lines so how does that work ?

The function takes 5 arguments.

  1. Variable to store the intersection point
  2. Start position of line 1
  3. Direction of line 1
  4. Start position of line 2
  5. Direction of line 2

A + B = (30, 0, 15)
C + D = (30, 0, 16)

It stands to reason that the intersection point would be very close to those values, which (33.75, 0, 17.5) is.

Oh it takes a direction ? Ah i was supplying 4 points, 2 for each line to represent the line, is there a way to change it so it takes 4 points and not a direction.

I’m confused, so i need to supply the point of the line and the sum of the first and second point of the line ? For both lines… right?

yup

This does not seem to be working, its acting as if the lines are longer than the points i provide are.

I have this:

Maths.LineLineIntersection(out result, lineApoint1, lineApoint2-lineApoint1, lineBpoint1, lineBpoint2 - lineBpoint1);

Even if the lines don’t intersect i still get true. I’m fairly convinced its related to do the direction but not sure how to just input 4 points rather than a direction to make life easier.

Correct, it assumes that the lines are infinite in length.

You will need to take the result and check that it is between your supplied points.

I have never used the library you are - but check if there is something along the lines of LineSegmentIntersection.

In math, lines are infinite. Testing the intersection of line segments is what you are after.

Sorry for late reply but do you happen to know how to do it for line segments, i can only ever fine infinite line calculations which is driving me crazy.

I imagine it is the same as lines, except you have an additional check to make sure the returned point is within your line segments.

why don’t you use Debug.DrawLine to give it a visual representation in the scene.