gpt4 book ai didi

c# - 目标航向的转向

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:06 24 4
gpt4 key购买 nike

我正在使用一个程序在二维 map 上模拟飞机。我很难找到转向的方向以击中目标方位。

  double maxHeadingChange = 10; //Maximum heading change per 100 ms
double targetHeading = 0;
double differenceHeading = Math.Abs(targetHeading - heading);
//if we need to turn clockwise

if (targetHeading > 340 || targetHeading < 30)
{
if (heading < 180)
{
if (differenceHeading > maxHeadingChange)
heading -= maxHeadingChange;
else
heading -= differenceHeading;
}
else
{
if (differenceHeading > maxHeadingChange)
heading -= maxHeadingChange;
else
heading -= differenceHeading;
}
}
else if (targetHeading > heading)
{
if (targetHeading - heading < maxHeadingChange)
heading = targetHeading;
else
heading += maxHeadingChange;
}
else
{
if (heading - targetHeading < maxHeadingChange)
heading = targetHeading;
else
heading -= maxHeadingChange;
}

//MessageBox.Show(headingDifference + "");
//just for now
//heading = targetHeading;

if(heading > 359)
heading = 0;
if(heading < 0)
heading += 360;

你可以看到我正在尝试做的是获得转向的方向,然后如果转向量大于最大转向量,则转向最大转向量。

问题是,当物体的航向约为 10 度并且目标为 354 度时,它会发现它应该顺时针转动以击中该目标,因为这将减去航向以击中较小的目标航向。问题是,一旦目标航向超过 0 并进入更高的航向范围,它就会顺时针反转。这导致对象转向其航向(逆时针旋转,或减少度数),并在它通过 0 时突然反转转向。

我需要找到一种更有效的方法来获得以度数为单位的转弯量(我不能简单地将 heading 设置为 targetHeading 因为最大的模拟turn amount) 以便它检测到从当前航向 10* 打到 350* 航向,继续减去航向。

抱歉,解释得太长了。

[编辑]: 可能的解决方案 1

  double maxHeadingChange = 10; //Maximum heading change per 100 ms
double targetHeading = 0;
double differenceHeading = Math.Abs(targetHeading - heading);
//if we need to turn clockwise
if(getTurnDir(heading, targetHeading))
{
//Turn right
if(differenceHeading > maxHeadingChange)
heading -= maxHeadingChange;
else
heading -= differenceHeading;
}
else
{
if (differenceHeading > maxHeadingChange)
heading += maxHeadingChange;
else
heading += differenceHeading;
}

//MessageBox.Show(headingDifference + "");
//just for now
//heading = targetHeading;

if(heading > 359)
heading = 0;
if(heading < 0)
heading += 360;

double hdgDiff(double h1, double h2)
{ // angle between two headings
double diff = h1 - h2 + 3600 % 360;
return diff <= 180 ? diff : 360 - diff;
}
bool getTurnDir(double hdg, double newHdg)
{ // should a new heading turn left or right?
if (newHdg > hdg)
return newHdg - hdg > 180;
return hdg - newHdg > 180;
}

最佳答案

double hdgDiff (double h1, double h2) { // angle between two headings
const double diff = fmod(h1 - h2 + 3600, 360);
return diff <= 180 ? diff : 360 - diff;
}

bool isTurnCCW(double hdg, double newHdg) { // should a new heading turn left ie. CCW?
const double diff = newHdg - hdg; // CCW = counter-clockwise ie. left
return diff > 0 ? diff > 180 : diff >= -180;
}

关于c# - 目标航向的转向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12869557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com