Last active
August 29, 2015 14:06
-
-
Save MichaelEstes/821e39f25761178134e3 to your computer and use it in GitHub Desktop.
AABB Collision detection between a SFML shape and circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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