Collision System in SFML / C#

The collision is the most important thing in this game like most others. In this post, we will try to implement a collision system for all objects. 

We don't have to use the collision system of SFML, but I want these tutorials of the series would be basic. So I'm going to choose the basic operation of collision. But, If you have experience about how can you that or curious you can find your way or use another better implementation of this. 

So, what should happen when objects collided with each other? Let's look at scenarios:

  • If the player's spacecraft collided with any invaders it should explode and the game is over.
  • If the bullet collided with any invaders it should be gone and also invader should be exploded.
According to this list, the player should control these enemies object with own method. And also bullets should be taken as a parameter in the update method of the enemy and are should be controlled by EnemyManager. Let's do it.
Okay firstly, we need to property to get sprite reference of objects:
//Player.cs
public Sprite PlayerSprite { get { return sprite; } }

// Enemy.cs
public Sprite EnemySprite { get { return sprite; } }

public Vector2f Position { get { return position; } }

// Bullet.cs

public RectangleShape RectangleBullet { get { return this.rectangle; } }
We need them because we can get the global bounding rectangle of the game object in this way. I added one parameter which it will take Player object to the update method of EnemyManager:
        public void update(Player player) 
        {
            if (enemies.Count < 20){
                for (int i = 0; i < 19 - enemies.Count; i++)
                {
                    enemies.Add(new Enemy());
                }
            }
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].update();
                if (enemies[i].Position.Y > 480 || this.collisionOfBullets(enemies[i], player)) 
                {
                    enemies.Remove(enemies[i]);
                }
            }
        }
I just added one more condition to control collision. If an enemy object collides with any objects that are player and bullets it's gonna be removed from the list of enemies. Now, let's write this method:
        public bool collisionOfBullets(Enemy enemy, Player player)
        {
            if (player.PlayerSprite.GetGlobalBounds().Intersects(enemy.EnemySprite.GetGlobalBounds()))
            {
                // game over                
                return true;
            }
            for (int i = 0; i < player.bullets.Count; i++)
            {
                if (enemy.EnemySprite.GetGlobalBounds().Intersects(player.bullets[i].RectangleBullet.GetGlobalBounds())) 
                {
                    player.bullets.Remove(player.bullets[i]);
                    return true;
                }
            } 
            return false;
        }
The player object has already its own bullets so we can use them to reach bullets at the same time. If there is a collision the method will return true. The bullets in collision state will be removed from the bullets list. Finally, let's add this player as an argument to the update method of the enemies object in Game class:
        private void update() 
        {
            this.player.update();
            this.enemies.update(this.player);
        }
Okay, let's check out the game:
 
Collision System of SFML Game with C#


No comments:

Post a Comment