1
General / Re: I'm confused about collision detection.
« on: April 05, 2013, 03:53:57 pm »I want to detect collision between player and object in map. I keep all objects in vector.
Here this is the codebool Collision::IsCollided(Player *player, std::vector<Platform*> platformList)
{
for(int i = 0; i < platformList.size(); i++)
{
if(player->GetPosition().x + player->GetWidth() > platformList[i]->GetPosition().x &&
player->GetPosition().x < platformList[i]->GetPosition().x + platformList[i]->GetWidth() &&
player->GetPosition().y + player->GetHeight() > platformList[i]->GetPosition().y &&
player->GetPosition().y < platformList[i]->GetPosition().y + platformList[i]->GetWidth())
return true;
}
return false;
}
I don't really understand the problem, but maybe intersects method can help you, would be something like:
bool Collision::IsCollided(Player *player, std::vector<Platform*> platformList)
{
for(int i = 0; i < platformList.size(); i++)
{
if(player->GetSprite().GetGlobalBounds().intersects(platformList[i]->GetSprite().GetGlobalBounds())
return true;
}
return false;
}
{
for(int i = 0; i < platformList.size(); i++)
{
if(player->GetSprite().GetGlobalBounds().intersects(platformList[i]->GetSprite().GetGlobalBounds())
return true;
}
return false;
}