SFML community forums

Help => Graphics => Topic started by: steven_g on April 06, 2016, 02:57:24 pm

Title: Check collision with vector of shapes
Post by: steven_g 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
}
 
 
Title: Re: Check collision with vector of shapes
Post by: fallahn on April 06, 2016, 03:02:17 pm
Take a look at this (http://trederia.blogspot.co.uk/2016/02/2d-physics-101-pong.html) for beginner collision :)
Title: Re: Check collision with vector of shapes
Post by: siordache94 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
}
}
 
Title: Re: Check collision with vector of shapes
Post by: steven_g 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
Title: Re: Check collision with vector of shapes
Post by: Jesper Juhl on April 07, 2016, 04:27:57 pm
You could also just use something like std::find_if (http://en.cppreference.com/w/cpp/algorithm/find) 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 (http://en.cppreference.com/w/cpp/algorithm/all_any_none_of).