Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision detection not working?  (Read 750 times)

0 Members and 1 Guest are viewing this topic.

BlazeCrate

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Collision detection not working?
« on: May 21, 2014, 12:56:09 am »
Hello,

I have been working on a 2d space shooter game for the past 2 weeks. I have gotten to the part where I want my player's bullets to collide with the enemy ships. I have setup this function and it checks the boundaries of both the bullets and the enemy ship.

bool Game::enemyBulletCollision()
{
    if (newEnemy->right <= player->newBullet->left || newEnemy->left >= player->newBullet->right ||
        newEnemy->bottom <= player->newBullet->top || newEnemy->top >= player->newBullet->bottom)
    {
        return false;
    }
    else
    {
        return true;
    }
}

All that I want it to do currently is print out "COLLISION!" in the console. (Will add explosions later). With this code if calls the boolean function above and prints out collision if true.

void Game::handleCollision()
{
    if (enemyBulletCollision())
    {
        printf("Collision!\n");
    }
}

This handleCollison function is called in the main game loop.

I have the bullets in a std::vector in the player class and it allows me to handle the player bullets in that player class. I have the enemies also in a std::vector but in the main game class. What I do is I create the newEnemy and newBullet and then I change their position and then push_back that newBullet or newEnemy into the std::vector of each.

I have tried access the bullets and enemies through a loop instead of accessing the top, left, bottom and right from the newBullet and newEnemy objects but the game seems to crash. (Weird)?

Please help!
Thanks