Example #1
0
        /// <summary>Attempt to find an intersection with a given ray and the plane.</summary>
        /// <param name="ray">The ray to find an intersection with</param>
        /// <param name="intersection">The intersection point, if found</param>
        /// <returns><b>true</b> if we found an intersection, <b>false</b> if we didn't.</returns>
        public bool TryRayIntersection(UnityEngine.Ray ray, out Vector3 intersection)
        {
            var start     = ray.origin;
            var end       = ray.origin + ray.direction * 1000.0f;
            var distanceA = Distance(start);

            if (float.IsInfinity(distanceA) || float.IsNaN(distanceA))
            {
                intersection = MathConstants.zeroVector3;
                return(false);
            }
            var distanceB = Distance(end);

            if (float.IsInfinity(distanceB) || float.IsNaN(distanceB))
            {
                intersection = MathConstants.zeroVector3;
                return(false);
            }

            Vector3 vector = end - start;
            float   length = distanceB - distanceA;
            float   delta  = distanceB / length;

            intersection = end - (delta * vector);
            if (float.IsInfinity(intersection.x) || float.IsNaN(intersection.x) ||
                float.IsInfinity(intersection.y) || float.IsNaN(intersection.y) ||
                float.IsInfinity(intersection.z) || float.IsNaN(intersection.z))
            {
                intersection = MathConstants.zeroVector3;
                return(false);
            }
            return(true);
        }
Example #2
0
        /// <summary>Calculate  an intersection with a given ray and the plane.</summary>
        /// <param name="ray">The ray to find an intersection with</param>
        /// <returns>The intersection point.</returns>
        public Vector3 RayIntersection(UnityEngine.Ray ray)
        {
            var start_x = (double)ray.origin.x;
            var start_y = (double)ray.origin.y;
            var start_z = (double)ray.origin.z;

            var direction_x = (double)ray.direction.x;
            var direction_y = (double)ray.direction.y;
            var direction_z = (double)ray.direction.z;

            var distanceA = (a * start_x) +
                            (b * start_y) +
                            (c * start_z) -
                            (d);
            var length = (a * direction_x) +
                         (b * direction_y) +
                         (c * direction_z);
            var delta = distanceA / length;

            var x = start_x - (delta * direction_x);
            var y = start_y - (delta * direction_y);
            var z = start_z - (delta * direction_z);

            return(new Vector3((float)x, (float)y, (float)z));
        }
Example #3
0
    private void Update()
    {
        // Generate a raycast on a mouse click from the main thread
        if (Input.GetMouseButtonDown(0))
        {
            UnityEngine.Ray ray         = Camera.main.ScreenPointToRay(Input.mousePosition);
            float           rayDistance = 100f;

            Debug.Log(Raycast(ray.origin, ray.direction * rayDistance));
        }
    }
    // Update is called once per frame
    void Update()
    {
        float minX = Random.Range(0, 10);
        float minY = Random.Range(0, 10);
        float minZ = Random.Range(0, 10);

        float maxX = Random.Range(10.1f, 20);
        float maxY = Random.Range(10.1f, 20);
        float maxZ = Random.Range(10.1f, 20);

        float originX = Random.Range(0, 10);
        float originY = Random.Range(0, 10);
        float originZ = Random.Range(0, 10);

        float directionX = Random.Range(1, 10);
        float directionY = Random.Range(1, 10);
        float directionZ = Random.Range(1, 10);

        MBounds myB = new MBounds(MVector3.zero, MVector3.zero);

        myB.SetMinMax(new MVector3(minX, minY, minZ), new MVector3(maxX, maxY, maxZ));
        float mD   = 0;
        var   mRay = new MRay(new MVector3(originX, originY, originZ), new MVector3(directionX, directionY, directionZ));
        bool  mR   = myB.IntersectRay(mRay, out mD);

        Bounds B = new Bounds(Vector3.zero, Vector3.zero);

        B.SetMinMax(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ));
        float D    = 0;
        var   uRay = new UnityEngine.Ray(new Vector3(originX, originY, originZ),
                                         new Vector3(directionX, directionY, directionZ));
        bool R = B.IntersectRay(uRay, out D);

        if (Mathf.Abs(mD - D) < 0.00001f && mR == R)
        {
        }
        else
        {
            if (originX < minX || originX > maxX || originY < minY || originY > maxY || originZ < minZ || originZ > maxZ)
            {
                mR = myB.IntersectRay(mRay, out mD);
            }
        }

        float normalX  = Random.Range(1, 10);
        float normalY  = Random.Range(1, 10);
        float normalZ  = Random.Range(1, 10);
        float distance = Random.Range(1, 10);
        var   ray      = new Ray(new Vector3(originX, originY, originZ), new Vector3(directionX, directionY, directionZ));
        var   mray     = new MRay(new MVector3(originX, originY, originZ), new MVector3(directionX, directionY, directionZ));
        var   plane    = new Plane(new Vector3(normalX, normalY, normalZ), -distance);
        var   mplane   = new MPlane(new MVector3(normalX, normalY, normalZ), distance);

        D  = 0;
        mD = 0;
        R  = false;
        mR = false;

        R  = plane.Raycast(ray, out D);
        mR = mplane.Raycast(mray, out mD);
        if (Mathf.Abs(mD - D) < 0.00001f && mR == R)
        {
        }
        else
        {
            var a = 1;
        }


        Matrix44 rotate = Matrix44.AngleAxis(mAngle, mAxis);

        for (int i = 0; i < mVertices.Length; i++)
        {
            var item = mVertices[i];
            var v    = new MVector3(item.x, item.y, item.z);
            var t    = v * rotate;
            mVertices[i].x = t.x;
            mVertices[i].y = t.y;
            mVertices[i].z = t.z;

            var item2 = mNormals[i];
            var v2    = new MVector3(item2.x, item2.y, item2.z);
            var t2    = v2 * rotate;
            mNormals[i].x = t2.x;
            mNormals[i].y = t2.y;
            mNormals[i].z = t2.z;
        }
        mMesh.vertices = mVertices;
        mMesh.normals  = mNormals;
    }