Example #1
0
        public readonly bool Intersects(Box2 box, out float distance, out Vector2 hitPos)
        {
            hitPos   = Vector2.Zero;
            distance = 0;

            var         tmin    = 0.0f;           // set to -FLT_MAX to get first hit on line
            var         tmax    = float.MaxValue; // set to max distance ray can travel (for segment)
            const float epsilon = 1.0E-07f;

            // X axis slab
            {
                if (MathF.Abs(Direction.X) < epsilon)
                {
                    // ray is parallel to this slab, it will never hit unless ray is inside box
                    if (Position.X < MathF.Min(box.Left, box.Right) || Position.X > MathF.Max(box.Left, box.Right))
                    {
                        return(false);
                    }
                }

                // calculate intersection t value of ray with near and far plane of slab
                var ood = 1.0f / Direction.X;
                var t1  = (MathF.Min(box.Left, box.Right) - Position.X) * ood;
                var t2  = (MathF.Max(box.Left, box.Right) - Position.X) * ood;

                // Make t1 be the intersection with near plane, t2 with far plane
                if (t1 > t2)
                {
                    MathHelper.Swap(ref t1, ref t2);
                }

                // Compute the intersection of slab intersection intervals
                tmin = MathF.Max(t1, tmin);
                tmax = MathF.Min(t2, tmax); // Is this Min (SE) or Max(Textbook)

                // Exit with no collision as soon as slab intersection becomes empty
                if (tmin > tmax)
                {
                    return(false);
                }
            }

            // Y axis slab
            {
                if (MathF.Abs(Direction.Y) < epsilon)
                {
                    // ray is parallel to this slab, it will never hit unless ray is inside box
                    if (Position.Y < MathF.Min(box.Top, box.Bottom) || Position.Y > MathF.Max(box.Top, box.Bottom))
                    {
                        return(false);
                    }
                }

                // calculate intersection t value of ray with near and far plane of slab
                var ood = 1.0f / Direction.Y;
                var t1  = (MathF.Min(box.Top, box.Bottom) - Position.Y) * ood;
                var t2  = (MathF.Max(box.Top, box.Bottom) - Position.Y) * ood;

                // Make t1 be the intersection with near plane, t2 with far plane
                if (t1 > t2)
                {
                    MathHelper.Swap(ref t1, ref t2);
                }

                // Compute the intersection of slab intersection intervals
                tmin = MathF.Max(t1, tmin);
                tmax = MathF.Min(t2, tmax); // Is this Min (SE) or Max(Textbook)

                // Exit with no collision as soon as slab intersection becomes empty
                if (tmin > tmax)
                {
                    return(false);
                }
            }

            // Ray intersects all slabs. Return point and intersection t value
            hitPos   = Position + Direction * tmin;
            distance = tmin;
            return(true);
        }
Example #2
0
 public Box2Rotated(Box2 box, Angle rotation)
     : this(box, rotation, Vector2.Zero)
 {
 }
Example #3
0
 public Box2Rotated(Box2 box, Angle rotation, Vector2 origin)
 {
     Box      = box;
     Rotation = rotation;
     Origin   = origin;
 }
Example #4
0
 public Box2Rotated(Box2 box)
     : this(box, 0)
 {
 }