寻找距离一点最近的圆
我正在研究我的世界查询方法,用于从一个点查找最近的实体(用于人工智能定位)。我的实体被边界圆圈覆盖。
我有这个:
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 计算更准确的距离?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这应该完全没问题。
如果到敌人的距离为Δ,其边界圆半径为r,则到其边界圆的距离为Δ-r。
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.
但是
所以你不会通过减去平方值来得到距离......
but
So you will not get distance by subtraction of squared values...