public bool HandleCollision(GameObject other, bool applyForce = true)
        {
            Vector2 en1Center = Position;
            int en1Radius = (Height + Width) / 4; // Average of height and width divided by 2 => 4

            Vector2 en2Center = other.Position;
            int en2Radius = (other.Height + other.Width) / 4; // Average of height and width divided by 2 => 4

            Vector2 diffVector = en1Center - en2Center; // From en2 to en1
            int distance = (int)diffVector.Length();

            //Collision?
            if (distance >= en1Radius + en2Radius || distance == 0) return false;

            if (applyForce)
            {
                int moveDistance = (en1Radius + en2Radius) - distance;
                diffVector /= distance;
                diffVector *= moveDistance / 2.0f;
                Position += diffVector;
                other.Position -= diffVector;
            }

            //Die on collision code
            if (other.Team != Team && other.DestroyOnCollsion != DestroyOnCollsion && !(other is Particle) && !(this is Particle))
            {
                if (other.DestroyOnCollsion)
                {
                    if (this is Entity && other is Entity) (this as Entity).Score += (int)(other as Entity).MaxHealth*5;
                    other.Destroy();
                }
                if (DestroyOnCollsion)
                {
                    if (this is Entity && other is Entity) (other as Entity).Score += (int) (this as Entity).MaxHealth*5;
                    Destroy();
                }
            }

            return true;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Detects and handles collision between GameObjects and the World.
        /// </summary>
        /// <param name="gameObject"></param>
        /// <returns>true if there occured an collision</returns>
        public bool WorldCollision(GameObject gameObject)
        {
            bool hasCollided = false;

            Point entityInTile = GetSquareAtPixel(gameObject.Position);

            // Collision detect in a 5x5 grid to where you are
            for (int i = 0; i < 5; ++i)
            {
                for (int j = 0; j < 5; ++j)
                {
                    hasCollided |= HandleTileCollision(gameObject, new Vector2(entityInTile.X + (i - 2), entityInTile.Y + (j - 2)));
                }
            }

            return hasCollided;
        }
Exemplo n.º 3
0
        private bool HandleTileCollision(GameObject gameObject, Vector2 tile)
        {
            //Prevent array out of bounds
            if (tile.X < 0 || tile.Y < 0 || tile.X >= MapWidth || tile.Y >= MapHeight) return false;

            //Is the tile collidable?
            if (!_mapSquares[(int)tile.X, (int)tile.Y].BlocksMovement) return false;

            Vector2 collisionTileCenter = GetSquareCenter(tile);
            int collisionTileRadius = TileWidth / 2;

            Vector2 entityCenter = gameObject.Position;
            int entityRadius = (gameObject.Height + gameObject.Width) / 4;// Average of height and width divided by 2 => 4
            entityRadius -= 8; // Make it possible to pass between tiles

            Vector2 diffVector = entityCenter - collisionTileCenter; // Tile to Entity
            int distance = (int)diffVector.Length();

            //Collision?
            if (distance < collisionTileRadius + entityRadius)
            {
                int moveRadius = (collisionTileRadius + entityRadius) - distance; // how long to move to not collide any more
                diffVector /= diffVector.Length();
                Vector2 moveVector = diffVector * moveRadius;

                gameObject.Position += moveVector;
                return true;
            }

            return false;
        }
Exemplo n.º 4
0
        public Point? GetCollidedTile(GameObject gameObject)
        {
            Point entityInTile = GetSquareAtPixel(gameObject.Position);
            // Collision detect in a 5x5 grid to where you are
            for (int i = 0; i < 5; ++i)
            {
                for (int j = 0; j < 5; ++j)
                {
                    if (HandleTileCollision(gameObject, new Vector2(entityInTile.X + (i - 2), entityInTile.Y + (j - 2))))
                    {
                        return new Point(entityInTile.X + (i - 2), entityInTile.Y + (j - 2));
                    }
                }
            }

            return null;
        }