1
Graphics / Collision detection problem
« on: July 15, 2011, 12:36:55 pm »
My frame rate is at 60fps
I wrote this code and sometimes it detects a collision and sometimes it does not.
My ship is meant to collide with mines and crates, but sometimes it will just pass through them, or the mine or crate will just explode before it should.
Any help would be appreciated.
I wrote this code and sometimes it detects a collision and sometimes it does not.
My ship is meant to collide with mines and crates, but sometimes it will just pass through them, or the mine or crate will just explode before it should.
Any help would be appreciated.
Code: [Select]
bool Collision::HasCollided(sf::Sprite& sprite1, sf::Sprite& sprite2)
{
// this function checks if the two inputted sprites have
// collided using circle collision detection
if(sprite1.GetSize().x > sprite1.GetSize().y)
{
r1 = sprite1.GetSize().x/2;
}
else
{
r1 = sprite1.GetSize().y/2;
}
if(sprite2.GetSize().x > sprite2.GetSize().y)
{
r2 = sprite2.GetSize().x/2;
}
else
{
r2 = sprite2.GetSize().y/2;
}
circle1 = sf::Shape::Circle(0, 0, r1, sf::Color(255,255,255,200));
circle1.SetPosition(sprite1.GetPosition().x, sprite1.GetPosition().y);
circle2 = sf::Shape::Circle(0,0,r2,sf::Color(255,255,255,200));
circle2.SetPosition(sprite2.GetPosition().x, sprite2.GetPosition().y);
distX = circle2.GetPosition().x - circle1.GetPosition().x;
distY = circle2.GetPosition().y - circle1.GetPosition().y;
distance = sqrt((distX*distX)+(distY*distY));
sumOfRadii = r1 + r2;
if(distance <= sumOfRadii)
{
return true;
}
else
{
return false;
}
}