ClosestPointsOnTwoLines() public static method

public static ClosestPointsOnTwoLines ( Vector3 &closestPointLine1, Vector3 &closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2 ) : bool
closestPointLine1 Vector3
closestPointLine2 Vector3
linePoint1 Vector3
lineVec1 Vector3
linePoint2 Vector3
lineVec2 Vector3
return bool
示例#1
0
        public static Vector3 Circumcenter(Vector3 pt1, Vector3 pt2, Vector3 pt3)
        {
            // Get the perpendicular bisector of pt1, pt2
            Vector3 v12           = pt2 - pt1;
            Vector3 v12normalized = v12.normalized;
            Vector3 v13           = pt3 - pt1;
            Vector3 v13normalized = v13.normalized;

            Vector3 v12perp = v13 - (Vector3.Dot(v13, v12normalized)) * v12normalized;

            v12perp.Normalize();

            // Get the perpendicular bisector of pt1, pt3
            Vector3 v13perp = v12 - (Vector3.Dot(v12, v13normalized)) * v13normalized;

            v13perp.Normalize();

            Vector3 v12base = (pt1 + pt2) / 2;
            Vector3 v13base = (pt1 + pt3) / 2;

            // Compute intersection of the two bisectors
            Vector3 closest1, closest2;

            Math3d.ClosestPointsOnTwoLines(out closest1, out closest2, v12base, v12perp, v13base, v13perp);

            return((closest1 + closest2) / 2);
        }
示例#2
0
    public static Vector3 Circumcentre2(Vector3 a, Vector3 b, Vector3 c, Vector3 d)
    {
        // Calculate plane for edge AB
        Vector3 planeABNormal = b - a;
        Vector3 planeABPoint  = Vector3.Lerp(a, b, 0.5f);

        // Calculate plane for edge AC
        Vector3 planeACNormal = c - a;
        Vector3 planeACPoint  = Vector3.Lerp(a, c, 0.5f);

        // Calculate plane for edge BD
        Vector3 planeBDNormal = d - b;
        Vector3 planeBDPoint  = Vector3.Lerp(b, d, 0.5f);

        // Calculate plane for edge CD
        Vector3 planeCDNormal = d - c;
        Vector3 planeCDPoint  = Vector3.Lerp(c, d, 0.5f);

        // Calculate line that is the plane-plane intersection between AB and AC
        Vector3 linePoint1;
        Vector3 lineDirection1;

        // Taken from: http://wiki.unity3d.com/index.php/3d_Math_functions
        Math3d.PlanePlaneIntersection(out linePoint1, out lineDirection1, planeABNormal, planeABPoint, planeACNormal, planeACPoint);

        Vector3 linePoint2;
        Vector3 lineDirection2;

        Math3d.PlanePlaneIntersection(out linePoint2, out lineDirection2, planeBDNormal, planeBDPoint, planeCDNormal, planeCDPoint);

        // Calculate the point that is the plane-line intersection between the above line and CD
        Vector3 intersection;

        // Floating point inaccuracy often causes these two lines to not intersect, in that case get the two closest points on each line
        // and average them
        // Taken from: http://wiki.unity3d.com/index.php/3d_Math_functions
        if (!Math3d.LineLineIntersection(out intersection, linePoint1, lineDirection1, linePoint2, lineDirection2))
        {
            Vector3 closestLine1;
            Vector3 closestLine2;

            // Taken from: http://wiki.unity3d.com/index.php/3d_Math_functions
            Math3d.ClosestPointsOnTwoLines(out closestLine1, out closestLine2, linePoint1, lineDirection1, linePoint2, lineDirection2);

            // Intersection is halfway between the closest two points on lines
            intersection = Vector3.Lerp(closestLine2, closestLine2, 0.5f);
        }

        return(intersection);
    }
示例#3
0
    public Vector3 AvoidCollisions()
    {
        //.find a good balance
        float seconds  = 1;
        float distance = rb.velocity.magnitude * seconds;

        foreach (Collider other in Physics.OverlapSphere(transform.position, distance))
        {
            Rigidbody rb2 = other.gameObject.GetComponent <Rigidbody> ();

            if (rb2 != null)
            {
                Vector3 p1;
                Vector3 p2;
                // if the lines aren't parallel
                if (Math3d.ClosestPointsOnTwoLines(out p1, out p2, transform.position, rb.velocity, other.gameObject.transform.position, rb2.velocity))
                {
                    float t1 = (p1 - transform.position).sqrMagnitude / rb.velocity.magnitude;
                    float t2 = (p2 - other.gameObject.transform.position).sqrMagnitude / rb2.velocity.magnitude;
                    // if the "collision" is at the same space-time
                    if (Mathf.Abs(t2 - t1) < 1)
                    {
                        // if the collision is close enough to detect
                        if ((p1 - transform.position).magnitude < distance)
                        {
                            if ((p2 - p1).magnitude < 1)                              //.use object radii instead
                            {
                                return(Evade(other.gameObject));
                            }
                        }
                    }
                }
            }
        }

        return(Vector3.zero);
    }
示例#4
0
    private void OnDrawGizmos()
    {
        if (compute)
        {
            Compute();
        }

        if (drawRays || drawHits || drawUnfold)
        {
            foreach (BouncyRay ray in rays)
            {
                Vector2 uv = ray.uv;
                Gizmos.color = ray.Hit ? new Color(uv.x, uv.y, 0, 0.25f) : new Color(1, 1, 1, 0.25f);

                if (drawRays || drawHits)
                {
                    Vector3[] hits = ray.GetPoints();
                    for (int p = 1; p < hits.Length; ++p)
                    {
                        if (drawRays)
                        {
                            Gizmos.DrawLine(hits[p - 1], hits[p]);
                        }
                        if (drawHits)
                        {
                            Gizmos.DrawSphere(hits[p], 0.25f);
                        }
                    }
                }
                if (drawUnfold)
                {
                    Ray unfolded = ray.Unfolded;
                    Gizmos.DrawLine(unfolded.origin, unfolded.origin + unfolded.direction * ray.Length);
                }
            }
        }
        //this is the optical center
        {
            idxRayOpticalCenter = (size - 1) / 2 + size * (size - 1) / 2;
            BouncyRay ray  = rays[idxRayOpticalCenter];
            Vector3[] hits = ray.GetPoints();
            Vector2   uv   = ray.uv;
            Gizmos.color = Color.blue;

            for (int p = 1; p < hits.Length; ++p)
            {
                Gizmos.DrawLine(hits[p - 1], hits[p]);
                Gizmos.DrawSphere(hits[p], 0.25f);
            }

            Ray unfolded = ray.Unfolded;
            Gizmos.DrawLine(unfolded.origin, unfolded.origin + unfolded.direction * ray.Length);
        }
        {
            BouncyRay ray  = physicalCenterBounce;
            Vector3[] hits = ray.GetPoints();
            Vector2   uv   = ray.uv;
            Gizmos.color = Color.white;

            for (int p = 1; p < hits.Length; ++p)
            {
                Gizmos.DrawLine(hits[p - 1], hits[p]);
                Gizmos.DrawSphere(hits[p], 0.25f);
            }

            Ray unfolded = ray.Unfolded;
            Gizmos.DrawLine(unfolded.origin, unfolded.origin + unfolded.direction * ray.Length);
        }

        Gizmos.color = Color.red;
        Vector3 center        = Vector3.zero;
        int     intersections = 0;

        for (int r1 = 0; r1 < rays.Length; ++r1)
        {
            BouncyRay raycast1 = rays[r1];
            if (raycast1.Hit)
            {
                Ray ray1 = raycast1.Unfolded;
                for (int r2 = r1 + 1; r2 < rays.Length; ++r2)
                {
                    BouncyRay raycast2 = rays[r2];
                    if (raycast2.Hit)
                    {
                        Ray     ray2 = raycast2.Unfolded;
                        Vector3 p1;
                        Vector3 p2;
                        if (Math3d.ClosestPointsOnTwoLines(out p1, out p2, ray1.origin, ray1.direction, ray2.origin, ray2.direction))
                        {
                            Gizmos.DrawLine(p1, p2);
                            center += (p1 + p2) / 2;
                            ++intersections;
                        }
                    }
                }
            }
        }
        Gizmos.DrawWireSphere(center / intersections, 0.1f);
    }