C# 将对象从点(X, Y)移动到点(X, Y)的路径算法
给定一个物体,它可以在给定的 X,Y 点向前、向后、向左、向右移动。如何使用给定的运动机制以最有效和最人性化的方式有效地将物体引导到 X,Y 点。
该对象可以实时移动,您可以告诉它们“startMoving|Direction|()”和“stopMoving|Direction|()”。尽管作为一个额外的扭曲和我遇到麻烦的部分是,物体的朝向永远不知道,只知道它的当前位置,所以算法必须“检测”方向。对象的位置在单独的线程中以 500-1 秒的间隔更新。可以随时发出更新算法中的位置的“请求”,但它不会立即可用,并且算法必须考虑到这一点。执行像 requestAndWaitForCoordUpdate() 这样的操作是完全可以接受的,但可能不需要。
此外,没有出现任何障碍物,可以假设您处于一个大部分开放的平面上,远离路径之间的直接直线,您可能会遇到障碍物。可以安全地假设,在给定的直接路径上,目标和源之间的距离的 1/4 的宽度应该可用。
我还想提一下,我不确定 A* 是否适用于这种情况,如果适用,我不确定在给定的限制下如何实现它。这里唯一真正的变量是物体的朝向。
这是一些示例代码:
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|Direction|()" and "stopMoving|Direction|()". 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.
In addition, no obstacles appear, it can be assumed you are on a MOSTLY open plane, stray to far from the direct straight line between the paths and you may run into obstacles. It is safe to assume that 1/4th the distance between a target and source, should be available in width on a given direct path.
I would also mention I am not sure A* applys in this scenario, if it does I am unsure how to implement it given the constraints. The only real variable here is the facing of the object.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要计算最佳路线,您应该使用 A* 算法。然而,要以最人性化的方式做到这一点,你只需让它行走并采取随机方向即可。除非是聪明人,否则他只会把右手贴在墙上,继续走,不失去联系:最终你会到达目的地。
人类不是高效的,而是随机的。
A* 不是随机的,而是高效的。
To calculate the optimal route you should use A* algorithm. However to do it in the most human way, you just let it walk and take random directions. Unless it's a smart human, he'll just stick his right hand to the wall and keep walking without losing touch: eventually you will reach your destination.
Human isn't efficient, it's random.
A* isn't random, it's efficient.