//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); } }
//Is a point intersecting with a circle? private void PointCircle() { MyVector2 testPoint = pointTrans.position.ToMyVector2(); MyVector2 circlePointA = t1_p1_trans.position.ToMyVector2(); MyVector2 circlePointB = t1_p2_trans.position.ToMyVector2(); MyVector2 circlePointC = t1_p3_trans.position.ToMyVector2(); //Is a point in a circle determines by three other points IntersectionCases intersectionCases = _Intersections.PointCircle(circlePointA, circlePointB, circlePointC, testPoint); //print(isPointInCircle); //Display the circle //if (intersectionCases == IntersectionCases.NoIntersection) //{ // Gizmos.color = Color.white; //} //if (intersectionCases == IntersectionCases.IsInside) //{ // Gizmos.color = Color.red; //} //if (intersectionCases == IntersectionCases.IsOnEdge) //{ // Gizmos.color = Color.blue; //} MyVector2 centerOfCicle = _Geometry.CalculateCircleCenter(circlePointA, circlePointB, circlePointC); float radius = MyVector2.Distance(centerOfCicle, circlePointA); //Gizmos.DrawWireSphere(centerOfCicle.ToVector3(), radius); ////Display the points //float pointRadius = 0.2f; //Gizmos.DrawWireSphere(pointTrans.position, pointRadius); //Gizmos.DrawWireSphere(t1_p1_trans.position, pointRadius); //Gizmos.DrawWireSphere(t1_p2_trans.position, pointRadius); //Gizmos.DrawWireSphere(t1_p3_trans.position, pointRadius); //With mesh //Big circle TestAlgorithmsHelpMethods.DisplayCircleMesh(centerOfCicle, radius, 60, Color.white); //Small circle Color circleColor = (intersectionCases == IntersectionCases.IsInside) ? Color.red : Color.white; TestAlgorithmsHelpMethods.DisplayCircleMesh(testPoint, 1f, 20, circleColor); }
//Is a plane intersecting with a plane private void PlanePlane() { Vector3 planeNormal_1 = planeTrans.forward; Vector3 planePos_1 = planeTrans.position; Vector3 planeNormal_2 = rayTrans.forward; Vector3 planePos_2 = rayTrans.position; //3d to 2d MyVector2 normal_1 = planeNormal_1.ToMyVector2(); MyVector2 normal_2 = planeNormal_2.ToMyVector2(); MyVector2 pos1 = planePos_1.ToMyVector2(); MyVector2 pos2 = planePos_2.ToMyVector2(); Plane2 plane_1 = new Plane2(pos1, normal_1); Plane2 plane_2 = new Plane2(pos2, normal_2); //Intersections bool isIntersecting = _Intersections.PlanePlane(plane_1, plane_2); Debug.Log("Are planes intersecting: " + isIntersecting); //Display //if (isIntersecting) //{ // MyVector2 intersectionPoint = Intersections.GetPlanePlaneIntersectionPoint(pos1, normal_1, pos2, normal_2); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Color planeColor = isIntersecting ? Color.red : Color.white; //TestAlgorithmsHelpMethods.DrawPlane(pos1, normal_1, planeColor); //TestAlgorithmsHelpMethods.DrawPlane(pos2, normal_2, planeColor); //Display with mesh TestAlgorithmsHelpMethods.DisplayPlaneMesh(pos1, normal_1, 0.5f, Color.blue); TestAlgorithmsHelpMethods.DisplayPlaneMesh(pos2, normal_2, 0.5f, Color.blue); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetPlanePlaneIntersectionPoint(plane_1, plane_2); 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); } }
//Is a point intersecting with a polygon private void PointPolygon() { List <Vector3> polygonPoints = GetVerticesFromParent(polygonPointsParentTrans); //To 2d List <MyVector2> polygonPoints_2d = new List <MyVector2>(); for (int i = 0; i < polygonPoints.Count; i++) { polygonPoints_2d.Add(polygonPoints[i].ToMyVector2()); } Vector3 testPoint = pointTrans.position; //Is the point inside the polygon bool isIntersecting = _Intersections.PointPolygon(polygonPoints_2d, testPoint.ToMyVector2()); //Display //Gizmos.color = isIntersecting ? Color.red : Color.white; //for (int i = 0; i < polygonPoints.Count; i++) //{ // int iPlusOne = MathUtility.ClampListIndex(i + 1, polygonPoints.Count); // Gizmos.DrawLine(polygonPoints[i], polygonPoints[iPlusOne]); //} //Gizmos.DrawWireSphere(testPoint, 0.1f); //With mesh to better see what's going on //Line TestAlgorithmsHelpMethods.DisplayConnectedLinesMesh(polygonPoints_2d, 0.5f, Color.white); //Point Color circleColor = isIntersecting ? Color.red : Color.white; TestAlgorithmsHelpMethods.DisplayCircleMesh(testPoint.ToMyVector2(), 1f, 20, circleColor); }
//Is a ray intersecting with a plane? private void RayPlane() { Vector3 planeNormal = planeTrans.forward; Vector3 planePos = planeTrans.position; Vector3 rayPos = rayTrans.position; Vector3 rayDir = rayTrans.forward; //2d space MyVector2 planeNormal_2d = planeNormal.ToMyVector2(); MyVector2 planePos_2d = planePos.ToMyVector2(); MyVector2 rayPos_2d = rayPos.ToMyVector2(); MyVector2 rayDir_2d = rayDir.ToMyVector2(); //Might as well test the distance from the point to the plane as well float distance = _Geometry.DistanceFromPointToPlane(planeNormal_2d, planePos_2d, rayPos_2d); Debug.Log(distance); bool isIntersecting = _Intersections.RayPlane(planePos_2d, planeNormal_2d, rayPos_2d, rayDir_2d); //Debug Gizmos.color = Color.blue; TestAlgorithmsHelpMethods.DrawPlane(planePos_2d, planeNormal_2d, Color.blue); //Ray //Gizmos.color = Color.white; //Gizmos.DrawWireSphere(rayPos, 0.1f); //if (isIntersecting) //{ // Gizmos.color = Color.red; // MyVector2 intersectionPoint = Intersections.GetRayPlaneIntersectionPoint(planePos_2d, planeNormal_2d, rayPos_2d, rayDir_2d); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Gizmos.DrawRay(rayPos, rayDir * 100f); //Display with mesh //Plane TestAlgorithmsHelpMethods.DisplayPlaneMesh(planePos_2d, planeNormal_2d, 0.5f, Color.blue); //Ray TestAlgorithmsHelpMethods.DisplayArrowMesh(rayPos_2d, rayPos_2d + rayDir_2d * 6f, 0.5f, 0.5f + 0.5f, Color.white); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetRayPlaneIntersectionPoint(planePos_2d, planeNormal_2d, rayPos_2d, rayDir_2d); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }