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

Author Topic: Check collision with vector of shapes  (Read 2636 times)

0 Members and 1 Guest are viewing this topic.

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Check collision with vector of shapes
« on: April 06, 2016, 02:57:24 pm »
Hi, what I want to do is check for collision of an sf::FloatRect between any other sf::FloatRect stored in a vector. I don't know how one would do this really. I would of thought you could use a for loop but I'm not sure   :-\
std::vector<sf::FloatRect>myFloatRects(100);                     //  100 floatRects of sf::Shape
sf::FloatRect myPosition = myShape.getGlobalBounds();
if (myPosition.intersects(myFloatRects)){                               // if myPosition intersects any of myFloatRects
//do something
}
 
 

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Check collision with vector of shapes
« Reply #1 on: April 06, 2016, 03:02:17 pm »
Take a look at this for beginner collision :)

siordache94

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Check collision with vector of shapes
« Reply #2 on: April 06, 2016, 03:03:03 pm »
hi, when you need to check any kind of container like a vector or an array you need to loop it , for example :
std::vector<sf::FloatRect>myFloatRects(100);                     //  100 floatRects of sf::Shape
sf::FloatRect myPosition = myShape.getGlobalBounds();
for(int i = 0; i = myFloatRects.size();i++)
if (myPosition.intersects(myFloatRects[i])){                               // if myPosition intersects any of myFloatRects
//do something
}
}
 

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Check collision with vector of shapes
« Reply #3 on: April 07, 2016, 12:02:05 am »
Thanks for both responses, I thought the way to do it was with a for loop, I was just a bit disbelieving :P
« Last Edit: April 07, 2016, 01:14:06 am by steven_g »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Check collision with vector of shapes
« Reply #4 on: April 07, 2016, 04:27:57 pm »
You could also just use something like std::find_if with a lambda.
Or if you just need to know if there is a collision but don't care about the colliding element, then there is std::any_of.

 

anything