Пример #1
0
    //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);
        }
    }