SFML community forums

Help => General => Topic started by: bumblecorn on February 24, 2013, 04:49:43 pm

Title: [SOLVED]Collision detection between objects stored in a vector
Post by: bumblecorn on February 24, 2013, 04:49:43 pm
Feels like kind of a silly question, but here goes. I know how to do collision detection between rectangles and other basic shapes simply created from the sf::RectangleShape class. You define a FloatRect for each object and that's pretty much it i.e.:

int collisionEnemy (sf::RectangleShape &bullet, sf::RectangleShape &enemy)
{
    sf::FloatRect bulletRect = bullet.getGlobalBounds();
    sf::FloatRect enemyRect = enemy.getGlobalBounds();
    return bulletRect.intersects(enemyRect);
}

Unfortunately I have no clue how to do this for objects stored in vectors, I have a vector for my bullets and a vector for my enemies (with currently only one enemy stored in it).

I don't even know where to begin.
Title: AW: Collision detection between objects stored in a vector
Post by: eXpl0it3r on February 24, 2013, 07:15:01 pm
If you don't know how to work with STL containers, then you've either read a bad C++ book or skip those parts.

You access an element of a vector the same way you access it in an array, with the [] operator or with iterators.
To get through the vector, you can use a for loop.

for(unsigned int i=0; i<vec.size():++i)
    vec[i] getGlobalBounds()
 
Title: Re: Collision detection between objects stored in a vector
Post by: bumblecorn on February 24, 2013, 08:11:40 pm
Thanks, but I know how to access elements in a vector (I'd like to think). I wasn't really clear in my first post and didn't explain the problem, my bad.

This is my code for updating the bullets and collision between enemies & bullets (well, the latter not so much since it doesn't work):

for (int i = 0; i < bullets.size(); ++i)
        {
                bullets[i].Rotate();
                bullets[i].Move();
                bullets[i].Draw(window);

                if(enemies[0].getGlobalBounds().intersects(bullets[i].getGlobalBounds()))
                {
                        cout<<"BOOM";
                }
        }

When the bullets and the enemy overlap in the game nothing is displayed in the console. Simply doesn't work, I must be doing something wrong, but no idea what it is.
Title: Re: Collision detection between objects stored in a vector
Post by: Gan on February 24, 2013, 08:18:20 pm
Try printing out the bounds of the enemy compared to the bullets.
If it doesn't completely clog the output, you might be able to examine the data to find potential bugs.
Title: Re: Collision detection between objects stored in a vector
Post by: bumblecorn on February 24, 2013, 09:59:48 pm
I had inherited both classes from sf::RectangleShape and used the methods from that class instead of creating my own. This resulted in the default values being used i.e. the default position of the enemy is (0, 0) and that is what was used for everything, same for global bounds, not sure why though. I simply created my own methods in the Enemy class to return the position and bounds and it works like a charm now.
Thanks Gan, testing that set me on the right track.