public CircleCollider(Vector2 position, float radius)
     : base(ColliderTypes.Circle)
 {
     _position = position;
     _radius = radius;
     _localBounds = new AABB(new Vector2(-radius, -radius), new Vector2(radius, radius));
 }
 public CircleCollider(Entity e, float radius)
     : base(ColliderTypes.Circle)
 {
     _entity = e;
     _radius = radius;
     _localBounds = new AABB(new Vector2(-radius, -radius), new Vector2(radius, radius));
 }
Example #3
0
        public static Shape CreateRectangle(AABB aabb)
        {
            Shape s = new Shape();

            s._vertices.Add(aabb.lowerBound);
            s._vertices.Add(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y));

            s._vertices.Add(aabb.upperBound);
            s._vertices.Add(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y));

            s._vertices.Add(aabb.lowerBound);

            return s;
        }
 public RectangleCollider(Vector2 position, int width, int height)
     : base(ColliderTypes.Rectangle)
 {
     _position = position;
     _localBounds = new AABB(0, 0, width, height);
 }
 public RectangleCollider(Entity e, int width, int height)
     : base(ColliderTypes.Rectangle)
 {
     _entity = e;
     _localBounds = new AABB(0, 0, width, height);
 }
Example #6
0
        public static AABB operator +(Vector2 offset, AABB bounds)
        {
            AABB result = new AABB();
            result.lowerBound = bounds.lowerBound + offset;
            result.upperBound = bounds.upperBound + offset;

            return result;
        }
Example #7
0
        public bool Intersects(AABB aabb)
        {
            Vector2 d1, d2;
            d1 = aabb.upperBound - lowerBound;
            d2 = upperBound - aabb.lowerBound;

            if (d1.X > 0.0f || d1.Y > 0.0f)
                return false;

            if (d2.X > 0.0f || d2.Y > 0.0f)
                return false;

            return true;
        }
Example #8
0
        // Does this contain the other within it
        public bool Contains(AABB aabb)
        {
            if (upperBound.X <= aabb.upperBound.X) return false;
            if (upperBound.Y <= aabb.upperBound.Y) return false;
            if (aabb.lowerBound.X <= lowerBound.X) return false;
            if (aabb.lowerBound.Y <= lowerBound.Y) return false;

            return true;
        }
Example #9
0
 // Add the other to this bounds
 public void Combine(ref AABB aabb)
 {
     upperBound = Vector2.Min(upperBound, aabb.upperBound);
     lowerBound = Vector2.Max(lowerBound, aabb.lowerBound);
 }
Example #10
0
 public static bool Overlap(AABB a, AABB b)
 {
     return a.Intersects(b);
 }
Example #11
0
 // Does the Point Intersect with the AABB a
 public static bool IntersectsPoint(AABB a, Vector2 point)
 {
     return (
         point.X < a.Right  && point.X > a.Left &&
         point.Y < a.Bottom && point.Y > a.Top);
 }
Example #12
0
        public static bool IntersectsLine_Fast(AABB a, Vector2 lineStart, Vector2 lineEnd)
        {
            if (a.Intersects(lineStart) || a.Intersects(lineEnd)) { return true; }

            LineSegment te, re, be, le;
            te = new LineSegment(new Vector2(a.Left, a.Top), new Vector2(a.Right, a.Top));
            re = new LineSegment(new Vector2(a.Right, a.Top), new Vector2(a.Right, a.Bottom));
            be = new LineSegment(new Vector2(a.Right, a.Bottom), new Vector2(a.Left, a.Bottom));
            le = new LineSegment(new Vector2(a.Left, a.Bottom), new Vector2(a.Left, a.Top));

            return (Collider.LinesIntersect_Fast(lineStart, lineEnd, te.start, te.end) ||
                Collider.LinesIntersect_Fast(lineStart, lineEnd, re.start, re.end) ||
                Collider.LinesIntersect_Fast(lineStart, lineEnd, be.start, be.end) ||
                Collider.LinesIntersect_Fast(lineStart, lineEnd, le.start, le.end));
        }
Example #13
0
        // TODO: return ALL hit points (including ends if they are within the bounds of the AABB)
        public static bool IntersectsLine(AABB a, Vector2 lineStart, Vector2 lineEnd, out Vector2 hitPoint)
        {
            LineSegment te, re, be, le;
            te = new LineSegment(new Vector2(a.Left, a.Top), new Vector2(a.Right, a.Top));
            re = new LineSegment(new Vector2(a.Right, a.Top), new Vector2(a.Right, a.Bottom));
            be = new LineSegment(new Vector2(a.Right, a.Bottom), new Vector2(a.Left, a.Bottom));
            le = new LineSegment(new Vector2(a.Left, a.Bottom), new Vector2(a.Left, a.Top));

            return (Collider.LinesIntersect(lineStart, lineEnd, te.start, te.end, out hitPoint) ||
                Collider.LinesIntersect(lineStart, lineEnd, re.start, re.end, out hitPoint) ||
                Collider.LinesIntersect(lineStart, lineEnd, be.start, be.end, out hitPoint) ||
                Collider.LinesIntersect(lineStart, lineEnd, le.start, le.end, out hitPoint));
        }
Example #14
0
        public static Vector2 GetIntersectionDepth(AABB a, AABB b)
        {
            // Calculate half sizes.
            Vector2 extendsA = a.Extents;
            Vector2 extendsB = b.Extents;

            // Calculate centers.
            Vector2 centerA = a.Center;
            Vector2 centerB = b.Center;

            // Calculate current and minimum-non-intersecting distances between centers.
            float distanceX = centerA.X - centerB.X;
            float distanceY = centerA.Y - centerB.Y;
            float minDistanceX = extendsA.X + extendsB.X;
            float minDistanceY = extendsA.Y + extendsB.Y;

            // If we are not intersecting at all, return (0, 0).
            if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
                return Vector2.Zero;

            // Calculate and return intersection depths.
            float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
            float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
            return new Vector2(depthX, depthY);
        }
 public EditorPallet(EditorScene scene, float width, float height)
     : this(scene)
 {
     _localBounds = new AABB(0, 0, width, height);
 }