91
General / Determing top/bottom and side collision
« on: April 16, 2012, 10:57:50 am »
Hello I'm currently working on a program in which I have a ball which moves and collides with a square. What I need to determine is whether to flip the Xvelocity or the Yvelocity. The function looks like this
The pTop and the pSide variables points to a bool value which is use to conditionally reverse the Y or Xvelocity at a detected collision.
The problem is that the side collision always flips, no matter on what side the ball collides with the square and the top bool never flips at all.
Any help?
Also sorry for posting this here, wasn't sure if this fitted anywhere at all.
bool ADVBoxCollision(sf::gEntity* sprite1, sf::gEntity* sprite2, bool* pTop, bool* pSide)
{
sf::Rect<float> FirstRect = sf::Rect<float>(sprite1->GetPosition().x - sprite1->GetCenter().x ,sprite1->GetPosition().y - sprite1->GetCenter().y,sprite1->GetPosition().x+sprite1->GetSize().x - sprite1->GetCenter().x,sprite1->GetPosition().y+sprite1->GetSize().y - sprite1->GetCenter().y);
sf::Rect<float> SecondRect = sf::Rect<float>(sprite2->GetPosition().x - sprite2->GetCenter().x ,sprite2->GetPosition().y - sprite2->GetCenter().y,sprite2->GetPosition().x+sprite2->GetSize().x - sprite2->GetCenter().x,sprite2->GetPosition().y+sprite2->GetSize().y - sprite2->GetCenter().y);
//ToS stands for Top or side. It tells whether the sprite collided with the top/bottom part or the side part. 0 = top/bottom, 1 = sides
if(BoxCollision(FirstRect, SecondRect))
{
if((FirstRect.Left <= SecondRect.Right) && (FirstRect.Right >= SecondRect.Left))
{
if(!*pSide) *pSide = 1;
else *pSide = 0; //If RECT1 is to the left of RECT2
}
if((FirstRect.Top <= SecondRect.Bottom) && (FirstRect.Bottom >= SecondRect.Top))
{
if (*pTop) *pTop = 0; //If RECT1 is over RECT2
else *pTop = 1;
}
return BoxCollision(FirstRect, SecondRect);
}
else return false;
}
{
sf::Rect<float> FirstRect = sf::Rect<float>(sprite1->GetPosition().x - sprite1->GetCenter().x ,sprite1->GetPosition().y - sprite1->GetCenter().y,sprite1->GetPosition().x+sprite1->GetSize().x - sprite1->GetCenter().x,sprite1->GetPosition().y+sprite1->GetSize().y - sprite1->GetCenter().y);
sf::Rect<float> SecondRect = sf::Rect<float>(sprite2->GetPosition().x - sprite2->GetCenter().x ,sprite2->GetPosition().y - sprite2->GetCenter().y,sprite2->GetPosition().x+sprite2->GetSize().x - sprite2->GetCenter().x,sprite2->GetPosition().y+sprite2->GetSize().y - sprite2->GetCenter().y);
//ToS stands for Top or side. It tells whether the sprite collided with the top/bottom part or the side part. 0 = top/bottom, 1 = sides
if(BoxCollision(FirstRect, SecondRect))
{
if((FirstRect.Left <= SecondRect.Right) && (FirstRect.Right >= SecondRect.Left))
{
if(!*pSide) *pSide = 1;
else *pSide = 0; //If RECT1 is to the left of RECT2
}
if((FirstRect.Top <= SecondRect.Bottom) && (FirstRect.Bottom >= SecondRect.Top))
{
if (*pTop) *pTop = 0; //If RECT1 is over RECT2
else *pTop = 1;
}
return BoxCollision(FirstRect, SecondRect);
}
else return false;
}
The pTop and the pSide variables points to a bool value which is use to conditionally reverse the Y or Xvelocity at a detected collision.
The problem is that the side collision always flips, no matter on what side the ball collides with the square and the top bool never flips at all.
Any help?
Also sorry for posting this here, wasn't sure if this fitted anywhere at all.