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

Author Topic: [SOLVED]Collision detection between objects stored in a vector  (Read 3461 times)

0 Members and 1 Guest are viewing this topic.

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
[SOLVED]Collision detection between objects stored in a vector
« 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.
« Last Edit: February 24, 2013, 10:11:25 pm by bumblecorn »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10834
    • View Profile
    • development blog
    • Email
AW: Collision detection between objects stored in a vector
« Reply #1 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()
 
« Last Edit: February 24, 2013, 09:35:22 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Collision detection between objects stored in a vector
« Reply #2 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.

Gan

  • Jr. Member
  • **
  • Posts: 98
    • View Profile
Re: Collision detection between objects stored in a vector
« Reply #3 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.

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Collision detection between objects stored in a vector
« Reply #4 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.