Esempio n. 1
0
        /// <summary>
        /// From Real-time Collision Detection, p179.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="input"></param>
        /// <param name="argPool"></param>
        public bool Raycast(RayCastOutput output, RayCastInput input, IWorldPool argPool)
        {
            //UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Float.MIN_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            float tmin = Single.Epsilon;
            float tmax = Single.MaxValue;

            Vec2 p      = argPool.PopVec2();
            Vec2 d      = argPool.PopVec2();
            Vec2 absD   = argPool.PopVec2();
            Vec2 normal = argPool.PopVec2();

            p.Set(input.P1);
            d.Set(input.P2).SubLocal(input.P1);
            Vec2.AbsToOut(d, absD);

            // x then y
            if (absD.X < Settings.EPSILON)
            {
                // Parallel.
                if (p.X < LowerBound.X || UpperBound.X < p.X)
                {
                    argPool.PushVec2(4);
                    return(false);
                }
            }
            else
            {
                float inv_d = 1.0f / d.X;
                float t1    = (LowerBound.X - p.X) * inv_d;
                float t2    = (UpperBound.X - p.X) * inv_d;

                // Sign of the normal vector.
                float s = -1.0f;

                if (t1 > t2)
                {
                    float temp = t1;
                    t1 = t2;
                    t2 = temp;
                    s  = 1.0f;
                }

                // Push the min up
                if (t1 > tmin)
                {
                    normal.SetZero();
                    normal.X = s;
                    tmin     = t1;
                }

                // Pull the max down
                tmax = MathUtils.Min(tmax, t2);

                if (tmin > tmax)
                {
                    argPool.PushVec2(4);
                    return(false);
                }
            }

            if (absD.Y < Settings.EPSILON)
            {
                // Parallel.
                if (p.Y < LowerBound.Y || UpperBound.Y < p.Y)
                {
                    argPool.PushVec2(4);
                    return(false);
                }
            }
            else
            {
                float inv_d = 1.0f / d.Y;
                float t1    = (LowerBound.Y - p.Y) * inv_d;
                float t2    = (UpperBound.Y - p.Y) * inv_d;

                // Sign of the normal vector.
                float s = -1.0f;

                if (t1 > t2)
                {
                    float temp = t1;
                    t1 = t2;
                    t2 = temp;
                    s  = 1.0f;
                }

                // Push the min up
                if (t1 > tmin)
                {
                    normal.SetZero();
                    normal.Y = s;
                    tmin     = t1;
                }

                // Pull the max down
                tmax = MathUtils.Min(tmax, t2);

                if (tmin > tmax)
                {
                    argPool.PushVec2(4);
                    return(false);
                }
            }

            // Does the ray start inside the box?
            // Does the ray intersect beyond the max fraction?
            if (tmin < 0.0f || input.MaxFraction < tmin)
            {
                argPool.PushVec2(4);
                return(false);
            }

            // Intersection.
            output.Fraction = tmin;
            output.Normal.X = normal.X;
            output.Normal.Y = normal.Y;
            argPool.PushVec2(4);
            return(true);
        }