0
\$\begingroup\$

I'm following the algorithm on on this website for my line segment intersection test code. But when looking at my line segment and plane intersect it doesn't produce the correct point of intersection.

Here is an example of what I'm talking about. intersection test failure

Here is my code for the test

public static bool SegmentPlane(Vector3 p1, Vector3 p2, float distance, Vector3 normal, out float time, out Vector3 point)
{
    time = 1f;
    point = Vector3.zero;


    time = (Vector3.Dot(normal, normal * distance) - Vector3.Dot(normal, p1)) / Vector3.Dot(normal, (p2 - p1).normalized);
    if(time >= 0f && time <= 1f)
    {
        point = p1 + time * (p2 - p1).normalized;
        return true;
    }
    return false;
}

Display Code

public void OnDrawGizmos()
{
    plane = new Plane(transform.forward, transform.position);

    colliding = CollisionLibrary.SegmentPlane(p1.position,p2.position,Vector3.Distance(Vector3.zero,transform.position),transform.up,out time,out point);

    if (!colliding)
        Gizmos.color = Color.yellow;
    else
        Gizmos.color = Color.red;

    Gizmos.DrawLine(p1.position, p2.position);
    Gizmos.DrawSphere(point, .1f);

    Gizmos.color = Color.white;
    Gizmos.DrawLine(transform.position, transform.position+transform.up * 100);



}
\$\endgroup\$
0
\$\begingroup\$

Looking at the code you referenced in your link, and the code you wrote, I'm not sure how you came up with that to be honest... Especially the time variable which doesn't exist in the example you posted and what you do to calculate it isn't in the example you posted either... So I'm wondering whether I'm understanding your question correctly or whether you actually want to do something else.

But in case you actually wanted to implement the intersect3D_SegmentPlane function in your example, below is how you could do that in Unity3D.

bool SegmentPlane(Vector3 p0, Vector3 p1, Vector3 planeCenter, Vector3 planeNormal, out Vector3 I)
{
    I = Vector3.zero;

    Vector3 u = p1 - p0;
    Vector3 w = p0 - planeCenter;

    float D = Vector3.Dot(planeNormal, u);
    float N = -Vector3.Dot(planeNormal, w);

    if (Mathf.Abs(D) < Mathf.Epsilon)
    {
        // segment is parallel to plane
        if (N == 0)                         // segment lies in plane
        {
            I = p0;                         // We could return anything between p0 and p1 here, all points are on the plane
            return true;
        }
        else
        {
            return false;                   // no intersection
        }
    }

    // they are not parallel
    // compute intersect param
    float sI = N / D;
    if (sI < 0 || sI > 1)
        return false;                       // no intersection

    I = p0 + sI * u;                        // compute segment intersect point
    return true;
}

EDIT: Reading it again, I think I understand what you tried to do and just misinterpreted Pn.v0 to be the same as Plane.distance, while it instead is the center point of the plane.

  • p0 and p1 would be the 2 points of the line
  • planeCenter would be transform.position of the plane.
  • planeNormal would be transform.up of the plane.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.