edit: added SOLVED
I just started coding in SFML so I am not sure if there is already some class for the following thing:
I have RectangleShapes in my game. I need to check if they are colliding between each other so I can adjust their position based on that. So if I hit into another RectangleShape it will stop etc...
Person is a class which holds the rectangle shape and other info, like speed etc.
I made a global vector:
std::vector<Person*>& fuckingPerson;
And I am pushing newly created items into it:
fuckingPerson.push_back(* new Someone(sf::Vector2f(300, 211), sf::Vector2f(0, 0), fuckingPerson));
fuckingPerson.push_back(*new Someone(sf::Vector2f(300, 311), sf::Vector2f(0, 0), fuckingPerson));
MainPlayer ply(sf::Vector2f(250, 100), sf::Vector2f(0, 0), window, mainView, fuckingPerson);
fuckingPerson.push_back(&ply);
I have an update function in the Person class (MainPlayer and Someone are inherited from Person)
void Person::update()
{
curPos = m_recShShape->getPosition();
m_recShShape->move(m_vfSpeed);
if (m_vfSpeed.x > 0) m_vfSpeed.x -= m_fFriction;
if (m_vfSpeed.x < 0) m_vfSpeed.x += m_fFriction;
if (m_vfSpeed.y > 0) m_vfSpeed.y -= m_fFriction;
if (m_vfSpeed.y < 0) m_vfSpeed.y += m_fFriction;
if (m_vfSpeed.x < m_fFriction && m_vfSpeed.x > 0) m_vfSpeed.x = 0;
if (m_vfSpeed.y < m_fFriction && m_vfSpeed.y > 0) m_vfSpeed.y = 0;
if (collidesWithWall() || collidesWithSomeone())
{
m_recShShape->setPosition(curPos);
m_vfSpeed.x = 0;
m_vfSpeed.y = 0;
}
}
My main problem lies probably in the collidesWithSomeone function:
bool Person::collidesWithSomeone()
{
sf::FloatRect boundingBox = m_recShShape->getGlobalBounds();
std::vector<Person*>::iterator j;
for (j = fuckingPerson.begin(); j != fuckingPerson.end(); j++)
{
if (this != (*j))
{
if (boundingBox.intersects((*j)->getPersonShape().getGlobalBounds()))
{
return true;
}
else
{
return false;
}
}
}
}
My problem is that the collision detection works between the first inserted Person and MainPlayer. Other inserted Persons are ignored. Examples in images below. I have been trying to solve this problem for about 4 hours now and I just can't find the damn bug.
Blue ones are "Someone".
Green one is "MainPlayer". Global bounds box is drawn for each shape.Detects collision properly on the first inserted:
Another doesn't: