Пример #1
0
        public void CollidesWith(RectangleF boundingBox, Action<CollisionInfo> onCollision)
        {
            var sx = (int)(boundingBox.Left / CellWidth);
            var sy = (int)(boundingBox.Top / CellHeight);
            var ex = (int)((boundingBox.Right / CellWidth) + 1);
            var ey = (int)((boundingBox.Bottom / CellHeight) + 1);

            for (var y = sy; y < ey; y++)
            {
                for (var x = sx; x < ex; x++)
                {
                    if (GetDataAt(x, y) == 0)
                        continue;

                    var cellRectangle = GetCellRectangle(x, y);
                    var intersectingRectangle = RectangleF.Intersect(cellRectangle.ToRectangleF(), boundingBox);

                    if (intersectingRectangle.IsEmpty)
                        continue;

                    var collisionInfo = new CollisionInfo
                    {
                        Row = y,
                        Column = x,
                        IntersectingRectangle = intersectingRectangle,
                        CellRectangle = cellRectangle
                    };

                    onCollision(collisionInfo);
                }
            }
        }
Пример #2
0
        private CollisionInfo GetCollisionInfo(ICollidable first, ICollidable second, RectangleF intersectingRectangle)
        {
            var info = new CollisionInfo
            {
                Other = second
            };

            if (intersectingRectangle.Width < intersectingRectangle.Height)
            {
                var d = first.BoundingBox.Center.X < second.BoundingBox.Center.X ? intersectingRectangle.Width : -intersectingRectangle.Width;
                info.PenetrationVector = new Vector2(d, 0);
            }
            else
            {
               var d = first.BoundingBox.Center.Y < second.BoundingBox.Center.Y ? intersectingRectangle.Height : -intersectingRectangle.Height;
                info.PenetrationVector = new Vector2(0, d);
            }

            return info;
        }
Пример #3
0
        private CollisionInfo GetCollisionInfo(ICollidable first, ICollidable second, RectangleF intersectingRectangle)
        {
            var info = new CollisionInfo
            {
                Other = second
            };

            if (intersectingRectangle.Width < intersectingRectangle.Height)
            {
                var d = first.BoundingBox.Center.X < second.BoundingBox.Center.X
                    ? intersectingRectangle.Width
                    : -intersectingRectangle.Width;
                info.PenetrationVector = new Vector2(d, 0);
            }
            else
            {
                var d = first.BoundingBox.Center.Y < second.BoundingBox.Center.Y
                    ? intersectingRectangle.Height
                    : -intersectingRectangle.Height;
                info.PenetrationVector = new Vector2(0, d);
            }

            return(info);
        }
Пример #4
0
        public void OnCollision(CollisionInfo c)
        {
            Position -= c.PenetrationVector;
            Velocity = Vector2.Zero;
            //if (c.PenetrationVector.X != 0)
            //{
            //    Position -= c.PenetrationVector;
            //    Velocity = new Vector2(0, Velocity.Y);
            //}
            //else
            //{
            //    if (Velocity.Y > 0)
            //        IsOnGround = true;

            //    var d = Position.Y < c.Other.BoundingBox.Center.Y
            //        ? c.IntersectingRectangle.Height
            //        : -c.IntersectingRectangle.Height;
            //    Position = new Vector2(Position.X, Position.Y - d);
            //    Velocity = new Vector2(Velocity.X, 0);
            //}
        }
 public void OnCollision(CollisionInfo collisionInfo)
 {
     _target.OnCollision(collisionInfo);
 }
Пример #6
0
 public void OnCollision(CollisionInfo collisionInfo)
 {
     _target.OnCollision(collisionInfo);
 }
Пример #7
0
        public void OnCollision(CollisionInfo c)
        {
            if (c.IntersectingRectangle.Width < c.IntersectingRectangle.Height)
            {
                var d = Position.X < c.CellRectangle.Center.X
                    ? c.IntersectingRectangle.Width
                    : -c.IntersectingRectangle.Width;
                Position = new Vector2(Position.X - d, Position.Y);
                Velocity = new Vector2(0, Velocity.Y);
            }
            else
            {
                if (Velocity.Y > 0)
                    IsOnGround = true;

                var d = Position.Y < c.CellRectangle.Center.Y
                    ? c.IntersectingRectangle.Height
                    : -c.IntersectingRectangle.Height;
                Position = new Vector2(Position.X, Position.Y - d);
                Velocity = new Vector2(Velocity.X, 0);
            }
        }