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

Author Topic: Trouble using iterator for collision  (Read 2349 times)

0 Members and 1 Guest are viewing this topic.

averagejoeprogrammer

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble using iterator for collision
« on: April 12, 2016, 11:22:35 am »
Hi,

I am having some troubles with the collision detection within SFML I have a Vector of skeletons
std::vector<Skeleton> *skeletons = new std::vector<Skeleton>();

I am then trying to test whether or not the main character intersects with the any of the enemies I am trying to do this using a iterator.

for (std::vector<Skeleton>::iterator it = skeletons->begin(); it != skeletons->end(); ++it)
       {
              if (mainCharacter->mSprite.getGlobalBounds().intersects(it->mSprite.getGlobalBounds()))
              {
                     /*skeletons->erase(skeletons->begin() + *it);*/
                     skeletons->erase(*it);
              }
       }

Do you have any idea what I am doing wrong here?

Kind Regards

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trouble using iterator for collision
« Reply #1 on: April 12, 2016, 11:37:48 am »
The right way to erase elements of a vector while iterating over them can easily be found with Google, it's a very common "problem".
Laurent Gomila - SFML developer

averagejoeprogrammer

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Trouble using iterator for collision
« Reply #2 on: April 12, 2016, 03:16:49 pm »
I tried changing this too
skeletons->erase(skeletons->begin() + it);

but the error I get is
   51   IntelliSense: no operator "+" matches these operands
            operand types are: std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Skeleton>>> + std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Skeleton>>>


so I then tried it this way
skeletons->erase(it);
Error   7   error C2582: 'operator =' function is unavailable in 'Skeleton'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility   

Any ideas what is going wrong here? I appreciate this is probably very simple problem but I tried various solutions from google to no avail.

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Trouble using iterator for collision
« Reply #3 on: April 12, 2016, 06:13:26 pm »
with a vector instead of using an iterator i'd use a normal for loop. at which point it would just be erase(i).

 
for (int i = 0; i < skeletons.size(); i++)
       {
              if (mainCharacter->mSprite.getGlobalBounds().intersects(skeletons[i]->mSprite.getGlobalBounds()))
              {
                     /*skeletons->erase(skeletons->begin() + *it);*/
                     skeletons->erase(i);
              }
       }

I'm also not sure why you're using new to create the vector. look up RAII or just avoid using a pointer at all.
« Last Edit: April 12, 2016, 10:43:07 pm by Laurent »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Trouble using iterator for collision
« Reply #4 on: April 12, 2016, 06:55:04 pm »
erasing an element from std::vector is widely covered in net. the most elegant way to achieve it by using STL approach, just  try to search for erase–remove idiom.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trouble using iterator for collision
« Reply #5 on: April 12, 2016, 10:42:22 pm »
Since people seem to prefer trying random stuff than clicking the first answer on Google... here it is ;)

(generic version, adapt to your code)
for (auto it = v.begin(); it != v.end(); )
{
    if (some_condition(*it))
        it = v.erase(it);
    else
        ++it;
}

same thing, hidden behind STL functions:
v.erase(std::remove_if(v.begin(), v.end(), [](T item){return some_condition(item);}), v.end());
Laurent Gomila - SFML developer