从距离获取点的坐标

Get Coordinates of a Point from Distance

所以,我正在做一个机器人算法游戏,我有 2 个玩家和 7 个球。

我设法计算出每个球员和每个球之间的距离,但现在我需要我的球员移动到球的坐标,球与球员的距离最短。

球员和球是 2 类2 个整数,x 和 y 是它们的坐标。

我用 Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

现在我需要的是以某种方式获得最短距离的 x 和 y 坐标。我考虑过 反转公式 以获得 x 值y 值,但我不确定它会给我带来想要的结果。

你有什么想法或不同的方法想法吗?我只需要一些想法和解决方法,剩下的由我来做。

不能post代码导致许多其他方法,类等没有存储在本地,所以我无法访问完整的源代码。如果你仍然认为你可以更好地帮助代码,我会上传到这里。

我假设你的项目有代表球员和包含 X-Y 坐标的球的对象,并且你想 最近的球移动(而不是传送到它)。

如果假设是正确的,那么您需要的是指向最近球的方向向量。每个球都应该 知道 它的 X-Y 坐标,你不需要 计算 它们 - 或者我错过了重点?

即像这样的伪代码:

class Player { float x, y };
class Ball { float x, y };

// Determine nearest ball
function distance(Player, Ball) = ... // the code you already posted
Ball nearest = for each ball { calculate distance } and find lowest

// Calculate direction to nearest ball
Vector dir = normalize(nearest.xy - player.xy);

// Move player
Vector move = dir * speed;
player.xy += move;

nearest.xy - player.xy计算方向到最近的球,normalize把它变成一个单位向量。

有很多关于简单矢量数学的教程。