I have added my sprites into a vector and am passing this vector and a shape object into a collision function. I need the function to check every element of the vector to see if the shape has collided with it, yet it only returns true if it collides with the first element of the vector. How can I get this function to return true if the shape collides with ANY element of the vector, not just the first:
bool CollisionCheck(const sf::CircleShape& shape, vector<sf::Sprite>& sprite){
// return true if a shape collides with an element of the vector
for (vector<sf::Sprite>::iterator it = sprite.begin(); it != sprite.end(); it++)
{
// get the current position of the sprite
if (shape.getGlobalBounds().intersects(it->getGlobalBounds()))
{
return true;
}
}
return false;
}
Any help will be greatly appreciated :)
What do you mean by missing Consts?
I mean this:
bool CollisionCheck(const sf::CircleShape& shape, const vector<sf::Sprite>& sprite){
// return true if a shape collides with an element of the vector
for (vector<sf::Sprite>::const_iterator it = sprite.begin(); it != sprite.end(); it++)
{
// get the current position of the sprite
if (shape.getGlobalBounds().intersects(it->getGlobalBounds()))
{
return true;
}
}
return false;
}
(it's not a member function so there's one less const than I though)
I have also stepped through my code. The function only returns true when the shape collides with the first element of my vector. And whilst it is colliding it rapidly iterates through the vector. So when it collides with sprite 1 in my vector it collides with them all.
When it's the second sprite that is supposed to collide, what happens on the second iteration? Have you checked both rectangles, and found out why the test returns false?
Only you and your debugger can find out what really happens, from looking at these few lines there's nothing more we can do to help you.