1
在關閉它之前,這實際上與編程有關。打開目標標題的方向
我正在使用一個程序來模擬2D地圖上的飛機。爲了達到目標方位,我很難找到轉向。
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時突然反向旋轉方向。
我需要找到一個更有效的方法來獲得轉彎以度爲單位的數量(我不能簡單地將標題設置爲targetHeading,因爲在最大轉動量中有一些模擬),以便它檢測到從當前的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;
}
/// <summary>
/// Left is false
/// </summary>
/// <param name="hdg"></param>
/// <param name="newHdg"></param>
/// <returns></returns>
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;
}
〜基督教
測試,由於到目前爲止 –
我有這樣的問題: 目標標題:146.630473812382標題:321.152852773836 它只是坐在卡在146.6,無法轉向。 –
兩個標題: 目標標題:146.249466480246標題:331.152852773836 目標標題:146.240074739079標題:321.152852773836 它去這些 –