C# 将对象从点(X, Y)移动到点(X, Y)的路径算法

发布于 2024-10-24 03:26:23 字数 708 浏览 2 评论 0原文

给定一个物体,它可以在给定的 X,Y 点向前、向后、向左、向右移动。如何使用给定的运动机制以最有效和最人性化的方式有效地将物体引导到 X,Y 点。

该对象可以实时移动,您可以告诉他们“startMoving”和“stopMoving”。尽管作为一个额外的扭曲和我遇到麻烦的部分是,物体的朝向永远不知道,只知道它的当前位置,所以算法必须“检测”方向。对象的位置在单独的线程中以 500-1 秒的间隔更新。可以随时发出更新算法中的位置的“请求”,但它不会立即可用,并且算法必须考虑到这一点。执行像 requestAndWaitForCoordUpdate() 这样的操作是完全可以接受的,但可能不需要。

这是一些示例代码:

public int[] currentCoords;
public void movement() {
  currentCoords[0] = 1005; // starting y coord
  currentCoords[1] = 1007; // starting x coord
  moveTo(1050, 1025);
}

public void moveTo(int x, int y) {
  ... how?
}

public void threadUpdatingCoords() {
   ... periodically check for source coord updates
   ... between 200ms and 1000ms apart.
}

Given a object which may move forward, backward, left and right at a given X,Y point. How to efficiently direct the object to a X,Y point using the given movement mechanics in the most efficient and human natural way.

The object is available for movement in real time, you may tell them to "startMoving" and "stopMoving". Though as a additional twist and the part I am having trouble with, is the facing of the object is never known, only its current location is known, so the algorithm must "detect" direction. The location of the object is updated in a separate thread in 500-1second intervals. A "Request" to update the location within the algorithm made be made at any point, but it is not immediately available and the algorithm must keep that in consideration. Doing something like requestAndWaitForCoordUpdate() is perfectly acceptable, however likely not needed.

Here is some example code:

public int[] currentCoords;
public void movement() {
  currentCoords[0] = 1005; // starting y coord
  currentCoords[1] = 1007; // starting x coord
  moveTo(1050, 1025);
}

public void moveTo(int x, int y) {
  ... how?
}

public void threadUpdatingCoords() {
   ... periodically check for source coord updates
   ... between 200ms and 1000ms apart.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

太阳公公是暖光 2024-10-31 03:26:23

如果我理解正确的话,你需要确定你的方向。

另外,您还可以查询您当前的位置。

您可以在前往新位置之前存储最后的坐标吗?

如果是这样,请使用基本三角学根据最后一个坐标和新坐标来提取对象移动的角度。

If I understand this correctly, you need to determine your direction.

Also, you can query your current position.

Can you store your last coordinates before going to a new position?

If so, use basic Trigonometry to extract the angle your object is moving in based on the last coordinate and the new coordinate.

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