简体   繁体   English

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

[英]C# Pathing algorithm for moving a object from point(X, Y) to point(X, Y)

Given a object which may move forward, backward, left and right at a given X,Y point. 给定一个对象,它可以在给定的X,Y点处向前,向后,向左和向右移动。 How to efficiently direct the object to a X,Y point using the given movement mechanics in the most efficient and human natural way. 如何使用给定的运动机制以最有效,最自然的方式将对象有效地指向X,Y点。

The object is available for movement in real time, you may tell them to "startMoving" and "stopMoving". 该对象可实时移动,您可以告诉他们“ startMoving”和“ 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. 对象的位置以500-1秒的间隔在单独的线程中更新。 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. 像requestAndWaitForCoordUpdate()这样的事情是完全可以接受的,但是可能不需要。

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.
}

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. 如果是这样,请使用基本三角函数根据最后一个坐标和新坐标来提取对象移动的角度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM