//Is a line intersecting with a plane? private void LinePlane() { Vector3 planeNormal = planeTrans.forward; Vector3 planePos = planeTrans.position; Vector3 line_p1 = t1_p1_trans.position; Vector3 line_p2 = t1_p2_trans.position; //2d space MyVector2 planeNormal_2d = planeNormal.ToMyVector2(); MyVector2 planePos_2d = planePos.ToMyVector2(); MyVector2 line_p1_2d = line_p1.ToMyVector2(); MyVector2 line_p2_2d = line_p2.ToMyVector2(); bool isIntersecting = _Intersections.LinePlane(planePos_2d, planeNormal_2d, line_p1_2d, line_p2_2d); //Debug //TestAlgorithmsHelpMethods.DrawPlane(planePos_2d, planeNormal_2d, Color.blue); ////Line //Gizmos.color = Color.white; //Gizmos.DrawWireSphere(line_p1, 0.1f); //Gizmos.DrawWireSphere(line_p2, 0.1f); //if (isIntersecting) //{ // Gizmos.color = Color.red; // MyVector2 intersectionPoint = Intersections.GetLinePlaneIntersectionPoint(planePos_2d, planeNormal_2d, line_p1_2d, line_p2_2d); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Gizmos.DrawLine(line_p1, line_p2); //Display with mesh //Plane TestAlgorithmsHelpMethods.DisplayPlaneMesh(planePos_2d, planeNormal_2d, 0.5f, Color.blue); //Line TestAlgorithmsHelpMethods.DisplayLineMesh(line_p1_2d, line_p2_2d, 0.5f, Color.white); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetLinePlaneIntersectionPoint(planePos_2d, planeNormal_2d, line_p1_2d, line_p2_2d); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }
//Are two lines intersecting? private void LineLine() { MyVector2 l1_p1 = t1_p1_trans.position.ToMyVector2(); MyVector2 l1_p2 = t1_p2_trans.position.ToMyVector2(); MyVector2 l2_p1 = t2_p1_trans.position.ToMyVector2(); MyVector2 l2_p2 = t2_p2_trans.position.ToMyVector2(); Edge2 l1 = new Edge2(l1_p1, l1_p2); Edge2 l2 = new Edge2(l2_p1, l2_p2); bool isIntersecting = _Intersections.LineLine(l1, l2, includeEndPoints: true); //Display //Gizmos.DrawLine(l1_p1.ToVector3(), l1_p2.ToVector3()); //Gizmos.DrawLine(l2_p1.ToVector3(), l2_p2.ToVector3()); //if (isIntersecting) //{ // MyVector2 intersectionPoint = Intersections.GetLineLineIntersectionPoint(l1_p1, l1_p2, l2_p1, l2_p2); // //Gizmos.color = Color.red; // //Gizmos.DrawSphere(intersectionPoint.ToVector3(), 1f); //} //With mesh //Line TestAlgorithmsHelpMethods.DisplayLineMesh(l1_p1, l1_p2, 0.5f, Color.white); TestAlgorithmsHelpMethods.DisplayLineMesh(l2_p1, l2_p2, 0.5f, Color.white); //If they are intersecting we can also get the intersection point if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetLineLineIntersectionPoint(l1, l2); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }