I use this to check distances.
/**
* Finds the distance IN TILES between a pair of x,y coordinates
*/
inline int path_length(int x1, int y1, int x2, int y2)
{
return std::abs(x1 - x2) + std::abs(y1 - y2);
}
int enemy_distance = path_length(enemy->x, enemy->y, this->x, this->y);
You can use the points and bounds of the Circleshape in your collision code.
Why go to the trouble when something as simple as below works just as effectively and is much more efficient for circular collision detection?
private static float Distance(Vector2f A, Vector2f B) // returns distance between two points
{
return (float)Trigonometry.SqrRoot((B.X - A.X) * (B.X - A.X) + (B.Y - A.Y) * (B.Y - A.Y));
}
private static bool Collides(Circle A, Circle B) // simple collision detection between two circles
{
float dist = Distance(A.Position, B.Position); // gets distance between circles
return dist < A.Radius + B.Radius; // if dist < combined radius we have a collision
}