我正在研究我的世界查询方法,以从一个点找到最近的实体(用于 AI 定位)。我的实体被边界圆圈覆盖。
我有这个:
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...
}
虽然它没有考虑边界圆的半径,但它给我的结果不准确。我可以只从距离中减去半径的平方以获得到边界圆边缘的距离,还是我需要使用 Math.sqrt 计算更准确的距离?
谢谢!
最佳答案
Can I just subtract the radius squared from distance to get the actual distance to the edge of the bounding circle
是的,这应该没问题。
如果到敌人的距离是Δ,他的包围圈半径是r,那么到他的包围圈的距离就是Δ-r。
关于algorithm - 从一个点找到最近的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7330673/