Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:15:05+00:00 2026-06-12T19:15:05+00:00

I’m working with a program in which I am simulating a aircraft on a

  • 0

I’m working with a program in which I am simulating a aircraft on a 2D map. I’m having some difficulty finding the direction to turn in order to hit a target bearing.

  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;

You can see what what I’m attempting to do is get the direction to turn, and then if the turn amount is greater than the max turn amount, turn by the max turn amount.

The issue is, that when the object is at a heading of ~10 degrees and the target is, say, 354 degrees, it sees that it should turn clockwise to hit that target, as that would be subtracting heading to hit a smaller target heading. The problem is, it’s reversing the turn to clockwise as soon as the target heading passes 0 and goes to the higher heading ranges. This is causing the object to turn towards its heading (which is rotating counterclockwise, or decreasing in degrees), and suddenly reverse turn direction as soon as it passes 0.

I need to find a more efficient way of getting the turn amount in degrees (I can’t simply set heading to targetHeading as there is some simulation in the maximum turn amount) so that it detects that to hit a heading of 350* from a current heading of 10*, to continue to subtract heading.

Sorry for the long explanation.

[Edit]: POTENTIAL SOLUTION 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:15:06+00:00Added an answer on June 12, 2026 at 7:15 pm
    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.