寻找距离一点最近的圆

发布于 2024-12-03 02:33:40 字数 485 浏览 3 评论 0原文

我正在研究我的世界查询方法,用于从一个点查找最近的实体(用于人工智能定位)。我的实体被边界圆圈覆盖。

我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-12-10 02:33:40

我可以用距离减去半径的平方来得到到边界圆边缘的实际距离

是的,这应该完全没问题。

如果到敌人的距离为Δ,其边界圆半径为r,则到其边界圆的距离为Δ-r

Can I just subtract the radius squared from distance to get the actual distance to the edge of the bounding circle

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.

素衣风尘叹 2024-12-10 02:33:40
DistanceToCenter - radius == DistanceToCircle

但是

DistanceToCenter^2 - radius^2 != DistanceToCircle^2

所以你不会通过减去平方值来得到距离......

DistanceToCenter - radius == DistanceToCircle

but

DistanceToCenter^2 - radius^2 != DistanceToCircle^2

So you will not get distance by subtraction of squared values...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文