Exemplo n.º 1
0
        public void Circle_IntersectionTest()
        {
            var circle = new CircleF(new Vector2(200.0f, 300.0f), 100.0f);

            var circ1 = new CircleF(new Vector2(350.0f, 300.0f), 100.0f);
            var circ2 = new CircleF(new Vector2(400.0f, 300.0f), 100.0f);

            var rect1 = new Rectangle(250, 300, 100, 100);
            var rect2 = new Rectangle(400, 300, 100, 100);

            bool result;

            circle.Intersects(ref circ1, out result);
            Assert.AreEqual(true, result);
            circle.Intersects(ref circ2, out result);
            Assert.AreEqual(false, result);

            circle.Intersects(ref rect1, out result);
            Assert.AreEqual(true, result);
            circle.Intersects(ref rect2, out result);
            Assert.AreEqual(false, result);

            Assert.AreEqual(true, circle.Intersects(circ1));
            Assert.AreEqual(false, circle.Intersects(circ2));

            Assert.AreEqual(true, circle.Intersects(rect1));
            Assert.AreEqual(false, circle.Intersects(rect2));
        }
Exemplo n.º 2
0
 public override void Update(double totalSeconds)
 {
     base.Update(totalSeconds);
     #region Collision update
     if (TranslationVector != Vector2.Zero)
     {
         _boundingCircle.Center = new Point2(WorldPosition.X, WorldPosition.Y);
     }
     foreach (AEntity entity in GameDatas.AllEntities.Values)
     {
         if (entity is CollidableEntity && this != entity)
         {
             if (_boundingCircle.Intersects(((CollidableEntity)entity)._boundingCircle))
             {
                 if (!_collideWith.ContainsKey(entity.Id))
                 {
                     ((CollidableEntity)entity).OnCollisionEnter?.Invoke(null, this);
                     OnCollisionEnter?.Invoke(null, (CollidableEntity)entity);
                 }
                 else
                 {
                     ((CollidableEntity)entity).OnCollisionStay?.Invoke(null, this);
                     OnCollisionStay?.Invoke(null, (CollidableEntity)entity);
                 }
             }
             else
             {
                 ((CollidableEntity)entity).OnCollisionExit?.Invoke(null, this);
                 OnCollisionExit?.Invoke(null, (CollidableEntity)entity);
             }
         }
     }
     #endregion Collision update
 }
Exemplo n.º 3
0
        public void CircCircIntersectionSameCircleTest()
        {
            IShapeF shape1 = new CircleF(Point2.Zero, 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.True(shape1.Intersects(shape2));
        }
Exemplo n.º 4
0
        public void CircleCircleNotIntersectingTest()
        {
            IShapeF shape1 = new CircleF(new Point2(5, 5), 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.False(shape1.Intersects(shape2));
        }
Exemplo n.º 5
0
        public void CircCircIntersectionOverlappingTest()
        {
            IShapeF shape1 = new CircleF(new Point2(1, 2), 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.True(shape1.Intersects(shape2));
        }
Exemplo n.º 6
0
        private static Vector2 PenetrationVector(CircleF circ1, CircleF circ2)
        {
            if (!circ1.Intersects(circ2))
            {
                return(Vector2.Zero);
            }

            var displacement = Point2.Displacement(circ1.Center, circ2.Center);

            Vector2 desiredDisplacement;

            if (displacement != Vector2.Zero)
            {
                desiredDisplacement = displacement.NormalizedCopy() * (circ1.Radius + circ2.Radius);
            }
            else
            {
                desiredDisplacement = -Vector2.UnitY * (circ1.Radius + circ2.Radius);
            }


            var penetration = displacement - desiredDisplacement;

            return(penetration);
        }
Exemplo n.º 7
0
        public void PenetrationVectorSlightlyOverlappingOffAxisTest()
        {
            Point2 pos1 = new Point2(2, 2.5f);
            Point2 pos2 = new Point2(2, 1);

            IShapeF shape1 = new CircleF(pos1, 2.0f);
            IShapeF shape2 = new CircleF(pos2, 2.0f);

            var actor1 = new BasicActor()
            {
                Position = pos1,
                Bounds   = shape1
            };
            var actor2 = new BasicWall()
            {
                Position = pos2,
                Bounds   = shape2
            };

            Assert.True(shape1.Intersects(shape2));
            collisionComponent.Insert(actor1);
            collisionComponent.Insert(actor2);
            collisionComponent.Update(_gameTime);
            // Actor should have moved up because the distance is shorter.
            Assert.True(actor1.Position.Y > actor2.Position.Y);
            // The circle centers should be about 4 units away after moving
            Assert.True(Math.Abs(actor1.Position.Y - 5.0f) < float.Epsilon);
            Assert.True(Math.Abs(actor1.Position.X - 2.0f) < float.Epsilon);
        }
Exemplo n.º 8
0
        public void PenetrationVectorSameCircleTest()
        {
            Point2 pos1 = Point2.Zero;
            Point2 pos2 = Point2.Zero;

            IShapeF shape1 = new CircleF(pos1, 2.0f);
            IShapeF shape2 = new CircleF(pos2, 2.0f);

            var actor1 = new BasicActor()
            {
                Position = pos1,
                Bounds   = shape1
            };
            var actor2 = new BasicWall()
            {
                Position = pos2,
                Bounds   = shape2
            };

            Assert.True(shape1.Intersects(shape2));
            collisionComponent.Insert(actor1);
            collisionComponent.Insert(actor2);
            collisionComponent.Update(_gameTime);
            Assert.True(Math.Abs(actor1.Position.Y - -4f) < float.Epsilon);
        }
Exemplo n.º 9
0
        public void PenetrationVectorCircleOffAxisRectangleTest()
        {
            Point2 pos1 = new Point2(2, 1);
            Point2 pos2 = new Point2(-2, -1);

            IShapeF shape1 = new CircleF(pos1, 2.0f);
            IShapeF shape2 = new RectangleF(pos2, new Size2(4, 2));


            var actor1 = new BasicActor()
            {
                Position = pos1,
                Bounds   = shape1
            };
            var actor2 = new BasicWall()
            {
                Position = pos2,
                Bounds   = shape2
            };

            Assert.True(shape1.Intersects(shape2));
            collisionComponent.Insert(actor1);
            collisionComponent.Insert(actor2);
            collisionComponent.Update(_gameTime);
            Assert.True(Math.Abs(actor1.Position.X - 2.0f) < float.Epsilon);
            Assert.True(Math.Abs(actor1.Position.Y - 3.0f) < float.Epsilon);
        }
Exemplo n.º 10
0
        public void RectCircOnEdgeTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new CircleF(new Point2(5, 0), 4);

            Assert.True(shape1.Intersects(shape2));
            Assert.True(shape2.Intersects(shape1));
        }
Exemplo n.º 11
0
        public void RectCircContainedTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new CircleF(Point2.Zero, 4);

            Assert.True(shape1.Intersects(shape2));
            Assert.True(shape2.Intersects(shape1));
        }
Exemplo n.º 12
0
 public void Intersects(CircleF circle, RectangleF rectangle, bool expectedToIntersect)
 {
     Assert.AreEqual(expectedToIntersect, circle.Intersects((BoundingRectangle)rectangle));
     Assert.AreEqual(expectedToIntersect, CircleF.Intersects(circle, (BoundingRectangle)rectangle));
 }
Exemplo n.º 13
0
 public void Intersects(CircleF circle, CircleF circle2, bool expectedToIntersect)
 {
     Assert.AreEqual(expectedToIntersect, circle.Intersects(circle2));
     Assert.AreEqual(expectedToIntersect, CircleF.Intersects(circle, circle2));
 }