Skip to content

Instantly share code, notes, and snippets.

@MichaelEstes
Last active August 29, 2015 14:06
Show Gist options
  • Save MichaelEstes/821e39f25761178134e3 to your computer and use it in GitHub Desktop.
Save MichaelEstes/821e39f25761178134e3 to your computer and use it in GitHub Desktop.
AABB Collision detection between a SFML shape and circle
bool CheckCollisionShapeCircle(sf::Shape & shape, sf::CircleShape circle){
sf::Vector2f shapePoint, shapePosition, circleCenter;
float radius;
sf::FloatRect shapeBounds = shape.getLocalBounds();
int numPoints = shape.getPointCount();
shapePosition = shape.getPosition();
circleCenter = circle.getPosition();
radius = circle.getRadius();
for (int i = 0; i < numPoints; ++i){
shapePoint = shape.getPoint(i);
shapePoint.x += shapePosition.x - shapeBounds.width;
shapePoint.y += shapePosition.y - shapeBounds.height;
shapePoint.x = shapePoint.x - circleCenter.x;
shapePoint.x *= shapePoint.x;
shapePoint.y = shapePoint.y - circleCenter.y;
shapePoint.y *= shapePoint.y;
if (shapePoint.x + shapePoint.y < radius * radius){
return true;
}
} return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment