/// <summary>
        ///		Returns whether or not this box intersects another.
        /// </summary>
        /// <param name="box2"></param>
        /// <returns>True if the 2 boxes intersect, false otherwise.</returns>
        public bool Intersects(AxisAlignedBox2 box2)
        {
            // Early-fail for nulls
            if (this.IsNull || box2.IsNull)
            {
                return(false);
            }

            if (this.IsInfinite || box2.IsInfinite)
            {
                return(true);
            }

            // Use up to 6 separating planes
            if (this.maxVector.X <= box2.minVector.X)
            {
                return(false);
            }
            if (this.maxVector.Y <= box2.minVector.Y)
            {
                return(false);
            }

            if (this.minVector.X >= box2.maxVector.X)
            {
                return(false);
            }
            if (this.minVector.Y >= box2.maxVector.Y)
            {
                return(false);
            }

            // otherwise, must be intersecting
            return(true);
        }
Example #2
0
 public Shape(AxisAlignedBox2 box)
 {
     this._axesDirty       = true;
     this._vertices        = (Vector2[])box.Corners.Clone();
     this._derivedVertices = (Vector2[])box.Corners.Clone();
     this._axes            = null;
 }
        public MinimumTranslationVector collideY(AxisAlignedBox2 box2)
        {
            var overlap = Intersection(box2);

            if (!overlap.IsNull)
            {
                var axis       = new Axis();
                var minOverlap = 0.0;
                var adjust     = overlap.Size;

                minOverlap = adjust.Y;
                if (overlap.Center.Y > box2.Center.Y)
                {
                    axis.edge = new Vector2(overlap.X0, overlap.Y0) - new Vector2(overlap.X1, overlap.Y0);
                }
                else
                {
                    axis.edge = new Vector2(overlap.X1, overlap.Y1) - new Vector2(overlap.X0, overlap.Y1);
                }

                axis.unit   = axis.edge.Perpendicular;
                axis.normal = axis.unit.ToNormalized();
                return(new MinimumTranslationVector(axis, minOverlap));
            }
            return(MinimumTranslationVector.Zero);
        }
 public AxisAlignedBox2(AxisAlignedBox2 box)
 {
     corners    = new Vector2[4];
     minVector  = box.minVector;
     maxVector  = box.maxVector;
     isNull     = box.IsNull;
     isInfinite = box.IsInfinite;
     UpdateCorners();
 }
Example #5
0
        public void update(AxisAlignedBox2 box)
        {
            if (_vertices.Length != 4)
            {
                throw new Exception("can't update with an aabb unless the shape has 4 verts");
            }

            _axesDirty = true;
            for (var i = 0; i < _vertices.Length; i++)
            {
                _vertices[i]        = box.Corners[i];
                _derivedVertices[i] = box.Corners[i];
            }
        }
        /// <summary>
        ///		Calculate the area of intersection of this box and another
        /// </summary>
        public AxisAlignedBox2 Intersection(AxisAlignedBox2 b2)
        {
            if (!Intersects(b2))
            {
                return(AxisAlignedBox2.Null);
            }

            Vector2 intMin = Vector2.Zero;
            Vector2 intMax = Vector2.Zero;

            Vector2 b2max = b2.maxVector;
            Vector2 b2min = b2.minVector;

            if (b2max.X > maxVector.X && maxVector.X > b2min.X)
            {
                intMax.X = maxVector.X;
            }
            else
            {
                intMax.X = b2max.X;
            }
            if (b2max.Y > maxVector.Y && maxVector.Y > b2min.Y)
            {
                intMax.Y = maxVector.Y;
            }
            else
            {
                intMax.Y = b2max.Y;
            }

            if (b2min.X < minVector.X && minVector.X < b2max.X)
            {
                intMin.X = minVector.X;
            }
            else
            {
                intMin.X = b2min.X;
            }
            if (b2min.Y < minVector.Y && minVector.Y < b2max.Y)
            {
                intMin.Y = minVector.Y;
            }
            else
            {
                intMin.Y = b2min.Y;
            }

            return(new AxisAlignedBox2(intMin, intMax));
        }
        public AxisAlignedBox2[] fromRectOffset(RectOffset offset)
        {
            var inner = new AxisAlignedBox2(minVector + offset.min, maxVector - offset.max);

            var rects = new AxisAlignedBox2[9];

            rects[0] = new AxisAlignedBox2(corners[0], inner.corners[0]);
            rects[2] = new AxisAlignedBox2(Utility.Min(corners[1], inner.corners[1]), Utility.Max(corners[1], inner.corners[1]));
            rects[6] = new AxisAlignedBox2(Utility.Min(corners[3], inner.corners[3]), Utility.Max(corners[3], inner.corners[3]));
            rects[8] = new AxisAlignedBox2(inner.corners[2], corners[2]);

            rects[1] = new AxisAlignedBox2(rects[0].corners[1], inner.corners[1]);
            rects[3] = new AxisAlignedBox2(rects[0].corners[3], inner.corners[3]);
            rects[4] = inner;
            rects[5] = new AxisAlignedBox2(rects[2].corners[3], rects[8].corners[1]);
            rects[7] = new AxisAlignedBox2(rects[6].corners[1], rects[8].corners[3]);
            return(rects);
        }
        /// <summary>
        ///		Allows for merging two boxes together (combining).
        /// </summary>
        /// <param name="box">Source box.</param>
        public void Merge(AxisAlignedBox2 box)
        {
            if (box.IsNull)
            {
                // nothing to merge with in this case, just return
                return;
            }
            else if (box.IsInfinite)
            {
                this.IsInfinite = true;
            }
            else if (this.IsNull)
            {
                SetExtents(box.Minimum, box.Maximum);
            }
            else if (!this.IsInfinite)
            {
                if (box.minVector.X < minVector.X)
                {
                    minVector.X = box.minVector.X;
                }
                if (box.maxVector.X > maxVector.X)
                {
                    maxVector.X = box.maxVector.X;
                }

                if (box.minVector.Y < minVector.Y)
                {
                    minVector.Y = box.minVector.Y;
                }
                if (box.maxVector.Y > maxVector.Y)
                {
                    maxVector.Y = box.maxVector.Y;
                }

                UpdateCorners();
            }
        }
Example #9
0
 /// <summary>
 ///    Tests whether this ray intersects the given box.
 /// </summary>
 /// <param name="box"></param>
 /// <returns>
 ///		Struct containing info on whether there was a hit, and the distance from the
 ///		origin of this ray where the intersect happened.
 ///	</returns>
 public IntersectResult Intersects(AxisAlignedBox2 box)
 {
     return(Utility.Intersects(this, box));
 }
Example #10
0
 /// <summary>
 ///		Returns whether or not this Circle interects a box.
 /// </summary>
 /// <param name="box"></param>
 /// <returns>True if the box intersects, false otherwise.</returns>
 public bool Intersects(AxisAlignedBox2 box)
 {
     return(Utility.Intersects(this, box));
 }
 public bool Contains(AxisAlignedBox2 box)
 {
     return(Contains(box.minVector) && Contains(box.maxVector));
 }