转向目标的最短路线?

发布于 2024-10-06 11:49:21 字数 280 浏览 0 评论 0原文

我已经创建了一个小型坦克射击游戏,目前正在研究其他坦克的 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 技术交流群。

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

发布评论

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

评论(1

吃兔兔 2024-10-13 11:49:21

我不确定是否有更简单的方法来做到这一点,我倾向于经常恢复到 3d 向量数学,即使 2d 三角解更适合。你的问题意味着你正在 2d 中工作,但 3d 数学仍然有效。希望有人能为你想出一个更简单的算法,但如果没有,这里有一些东西。

我假设你的意思是角度测量中的最短距离。在此代码中,如果最短路径为 CCW,则结果将为正角度值;如果为 CW,则结果将为负角度值。

Vector2 targetPosition = new Vector2(3, 3);
Vector2 myPosition = new Vector2(4, 7);
Vector2 myFacingDirection2D = new Vector2(-1, 0);//facing straight left   
Vector3 directionToTarget3D = Vector3.Normalize(new Vector3(targetPosition - myPosition, 0));
Vector3 myFacingDirection3D = Vector3.Normalize(new Vector3(myFacingDir2D, 0);
Vector3 crossResult = Vector3.Cross(myFacingDirection3D, directionToTarget3d);
float dotFactor = Vector3.Dot(myFacingDirection3D, directionToTarget3D) < 0 ? MathHelper.Pi : 0f;

float angleToTarget = (dotFactor - (float)Math.Sin(crossResult.Length()) ) * Math.Sign(crossResult.Z);

如果结果相反(正/负),则反转 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.

Vector2 targetPosition = new Vector2(3, 3);
Vector2 myPosition = new Vector2(4, 7);
Vector2 myFacingDirection2D = new Vector2(-1, 0);//facing straight left   
Vector3 directionToTarget3D = Vector3.Normalize(new Vector3(targetPosition - myPosition, 0));
Vector3 myFacingDirection3D = Vector3.Normalize(new Vector3(myFacingDir2D, 0);
Vector3 crossResult = Vector3.Cross(myFacingDirection3D, directionToTarget3d);
float dotFactor = Vector3.Dot(myFacingDirection3D, directionToTarget3D) < 0 ? MathHelper.Pi : 0f;

float angleToTarget = (dotFactor - (float)Math.Sin(crossResult.Length()) ) * Math.Sign(crossResult.Z);

If this turns out backwards (positive/negative wise) then reverse the order of the params in the Cross function.

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