I’m working on my world query methods for finding the closest entity from a point (for AI targeting). My entities are covered in bounding circles.
I have this:
var distanceX : Number = boundingCircle.position.x - startPosition.x;
var distanceY : Number = boundingCircle.position.y - startPosition.y;
var distance : Number = (distanceX * distanceX + distanceY * distanceY);
if (distance < lastDistance)
{
// set this circle as the closest...
}
It doesn’t take the radius of the bounding circle in to account though and that’s giving me inaccurate results. Can I just subtract the radius squared from distance to get the distance to the edge of the bounding circle or do I need to calculate a more accurate distance with Math.sqrt?
Thanks!
Yes, this should be perfectly fine.
If the distance to the enemy is Δ, and his bounding circle radius is r, then the distance to his bounding circle is Δ-r.