3

I want to know when four Vector 3 positions intersect, as in the image, from point to point a1 to a2 and b1 to b2 if they intersect each other from each of their positions. Does anyone have any idea how to do this?

enter image description here

  • 2
    This sounds more like a general maths question than c# or unity specific. You could probably rather post it on the [Maths](https://math.stackexchange.com) page – derHugo Dec 23 '19 at 09:51
  • @derHugo I'm new to programming, my skills are game design, but I'll be more aware, thanks for the tip – victor oliveira Dec 23 '19 at 15:22

1 Answers1

7

The Unity community wiki has a page of math functions that includes a very helpful procedure for this. Copied (and edited slightly) below:

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 parallel
    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;
    }
}

So, in your situation, you could use that, then check if the intersection point is between a1 and a2 and b1 and b2:

Vector3 intersection;
Vector3 aDiff = a2-a1;
Vector3 bDiff = b2-b1;
if (LineLineIntersection(out intersection, a1, aDiff, b1, bDiff))
{
    float aSqrMagnitude = aDiff.sqrMagnitude;
    float bSqrMagnitude = bDiff.sqrMagnitude;

    if (    (intersection - a1).sqrMagnitude <= aSqrMagnitude  
         && (intersection - a2).sqrMagnitude <= aSqrMagnitude  
         && (intersection - b1).sqrMagnitude <= bSqrMagnitude 
         && (intersection - b2).sqrMagnitude <= bSqrMagnitude)
    {
        // there is an intersection between the two segments and 
        //   it is at intersection
    }
}
Ruzihm
  • 19,133
  • 4
  • 34
  • 44
  • While your answer is super useful, through trial and error I found out that Unity's function "Mathf.Approximately(a, b)" doesn't always return a correct value as can be seen on this thread here: https://stackoverflow.com/a/58604874/4952623 ... Therefore sticking to the Unity's community wiki is the solution on this case: "if(Mathf.Abs(planarFactor) < 0.0001f && crossVec1and2.sqrMagnitude > 0.0001f)" – MalokuS May 15 '21 at 20:44