转向目标的最短路线?
我已经创建了一个小型坦克射击游戏,目前正在研究其他坦克的 CPU 控制。
cpu 坦克知道以下信息:
- 它自己的面向,它面向的方向
- 它自己的位置(X 和 Y)
- 它的目标位置(X 和 Y)
在此基础上,我使得正 y 向下而不是向上。 x 正方向是从左到右。
我如何计算CPU坦克需要旋转到指向其目标的最短路径?
假设坦克位于 (3,3),面朝 90 度(正向左侧),而我位于 (4,7)。计算结果是怎样的?
Ive created a small tank-shooter-minigame, and is currently working on cpu control of other tanks.
A cpu tank knows the following:
- Its own facing, the direction its facing
- Its own position (X and Y)
- Its targets position (X and Y)
On top of that ive made it so that positive y is downwards instead of upwards. positive x direction is left to right.
How do i calculate the shortest way the cpu tank need to rotate to point on its target?
Lets say tank is at (3,3), facing 90 degrees (straight left) and Im at (4,7). What would the calucations be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否有更简单的方法来做到这一点,我倾向于经常恢复到 3d 向量数学,即使 2d 三角解更适合。你的问题意味着你正在 2d 中工作,但 3d 数学仍然有效。希望有人能为你想出一个更简单的算法,但如果没有,这里有一些东西。
我假设你的意思是角度测量中的最短距离。在此代码中,如果最短路径为 CCW,则结果将为正角度值;如果为 CW,则结果将为负角度值。
如果结果相反(正/负),则反转 Cross 函数中参数的顺序。
I'm not sure there isn't an easier way to do this, I tend to revert to 3d vector math often, even if in 2d trigonometric solutions are better suited. Your question implies you are working in 2d, but the 3d math will still work. Hopefully, someone will come up with an easier algorithm for you, but if not, here is something.
I assume you mean the shortest way in an angular measurement. In this code, the result will be a positive angular value if the shortest way is CCW, negative if CW.
If this turns out backwards (positive/negative wise) then reverse the order of the params in the Cross function.