Esempio n. 1
0
        public void Vector2AbsTest()
        {
            Vector2 v1 = new Vector2(-2.5f, 2.0f);
            Vector2 v3 = Vector2.Abs(new Vector2(0.0f, Single.NegativeInfinity));
            Vector2 v  = Vector2.Abs(v1);

            Assert.Equal(2.5f, v.X);
            Assert.Equal(2.0f, v.Y);
            Assert.Equal(0.0f, v3.X);
            Assert.Equal(Single.PositiveInfinity, v3.Y);
        }
Esempio n. 2
0
        public bool RayCast(out RayCastOutput output, RayCastInput input)
        {
            output = default;
            var tmin = -Settings.MaxFloat;
            var tmax = Settings.MaxFloat;

            var p    = input.P1;
            var d    = input.P2 - input.P1;
            var absD = Vector2.Abs(d);

            var normal = new Vector2();

            {
                if (absD.X < Settings.Epsilon)
                {
                    // Parallel.
                    if (p.X < LowerBound.X || UpperBound.X < p.X)
                    {
                        return(false);
                    }
                }
                else
                {
                    var invD = 1.0f / d.X;
                    var t1   = (LowerBound.X - p.X) * invD;
                    var t2   = (UpperBound.X - p.X) * invD;

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

                    if (t1 > t2)
                    {
                        MathUtils.Swap(ref t1, ref t2);
                        s = 1.0f;
                    }

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

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

                    if (tmin > tmax)
                    {
                        return(false);
                    }
                }
            }
            {
                if (absD.Y < Settings.Epsilon)
                {
                    // Parallel.
                    if (p.Y < LowerBound.Y || UpperBound.Y < p.Y)
                    {
                        return(false);
                    }
                }
                else
                {
                    var invD = 1.0f / d.Y;
                    var t1   = (LowerBound.Y - p.Y) * invD;
                    var t2   = (UpperBound.Y - p.Y) * invD;

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

                    if (t1 > t2)
                    {
                        MathUtils.Swap(ref t1, ref t2);
                        s = 1.0f;
                    }

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

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

                    if (tmin > tmax)
                    {
                        return(false);
                    }
                }
            }

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

            // Intersection.
            output = new RayCastOutput {
                Fraction = tmin, Normal = normal
            };

            return(true);
        }