Question

I'm programming a kind of Bullet Hell game, and I've made code to detect the collisions, and it actually works, but I think there's an easier way to do this. Let me explain.

In the code, there's the GameScreen, that create instances of a Player Class and an Enemy Class.

That Enemy Class create a instance of a Bullet Pattern Class, and the Bullet Pattern is the class that instantiate the bullets. The reason of not spawning them with the enemy class is that one enemy can have more than one pattern.

So, the Bullet Pattern class is the one that contain a List of bullets on screen. So, to check the collision, the GameScreen has a method, that gets the player position(and the collider radius) and calls for a collision check with the enemy, which, if not colliding with the enemy, calls for a collision check of the pattern, which checks for collisions with the bullets.

GameScreen CheckCollision:

private void CheckCollision()
{
    Vector2 playerPos = player.GetPosition();
    float colSize = player.GetColliderSize();
    bool isColliding = testEnemy.CheckCollision(playerPos, colSize);
    if (isColliding) //If collides, the player do what it should do.
    {
        player.Collide();
    }
}

Enemy CheckCollision, which checks it if is colliding with the enemy, if not, calls the pattern method:

    public bool CheckCollision(Vector2 playerPos, float colSize)
    {
        float distance = Game1.DistanceBetween(playerPos, position);
        float minDistance = colSize + colliderSize;
        bool collision = distance <= minDistance;
        if (collision)
        {
            return collision;
        }
        collision = patterns[actualPattern].CheckCollision(playerPos, colSize);
        return collision;
    }

The Pattern CheckCollision:

    public bool CheckCollision(Vector2 playerPos, float colSize)
    {
        //The pattern can have Sub Patterns, wich is the same pattern class...
        for (byte i = 0; i < subPatterns.Count; i++)
        {
            subPatterns[i].CheckCollision(playerPos, colSize);
        }
        for (int i = 0; i < bullets.Count; i++)
        {
            Vector2 bulletPos = bullets[i].GetPosition();
            float bulletSize = bullets[i].GetColliderSize();
            float distance = Game1.DistanceBetween(playerPos, bullets[i].position);
            float minDistance = colSize + bulletSize;
            if (distance <= minDistance)
            {
                return true;
                break;
            }
        }
        return false;
    }

As I said, it actually works, but I know that there's an easier way to do that... So the question is actually two:

  1. Is there an easier way to do that collision check?
  2. Is there a way to the GameScreen access the Bullet List from the pattern directly?
Était-ce utile?

La solution

There are a lot of ways to detect collisions without looping through each game object.

  1. Quad-trees (http://en.wikipedia.org/wiki/Quadtree)
  2. Grid based collision checking where the game is divided into grids, and the whole collision detection is done by iterating through the different grids.

There are other ways that I am not aware of. Apart from that, you also need to determine whether the collision detection is continuous or discrete, specially when working with bullets since those are fast moving objects.

As for the way for the GameScreen to access those bullets, use a GameObjectManager that keeps track of all objects within the game. Keep that class inside your game registry as a singleton instance so that keeping track of the elements you need constant update with will be easy.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top