CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2012
    Posts
    49

    compass angle prediction question

    Hello,

    In a cartesian coordinate system, I want to be able to predict a compass angle of an object. So I have a base position of (0,0) and then a distance and compass angle to an object. This object also has a heading and a speed. How can I predict the new compass angle of the object with that information?

    my coordinate system is like this:

    Code:
                                           0 y
                                           |
                                           |
                                  270-------------- 90 x
                                           |
                                           |
                                          180

    I think the first step would be to compute the cartesian coordinates of the object:

    float degs_to_rads = 3.141592653589793 / 180.0;
    x = distance * sin(angle*degs_to_rads);
    y = distance * cos(angle*degs_to_rads);

    then the next step would be to compute the predicted x and y from the speed and heading of the object:

    predictedx = ??
    predictedy = ??

    then finally convert back to an angle, and distance:

    newDistance = sqrt(predictedx^2 + predictedy^2);
    newAngle = atan2(predictedy, predictedx);

    Can anyone help me fill in the blanks?? Are my other things correct?

    Thanks!
    Alex
    Last edited by aseminov; April 16th, 2012 at 02:56 PM.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: compass angle prediction question

    Your coordinate system is incorrect C++, like most programming languages use radians for trig functions.

    Code:
                                           0 y
                                           |
                                           |
                               3pi/2-------------- pi/2 x
                                           |
                                           |
                                          pi

  3. #3
    Join Date
    Jan 2012
    Posts
    49

    Re: compass angle prediction question

    ok so say I convert angle to radians.. then what?

  4. #4
    Join Date
    Apr 2012
    Posts
    2

    Re: compass angle prediction question

    Hi, IRC guy here. So what you want to do is compute the displacement vector v = (x',y') that you will add to your old coordinates to get the new point. Now, given your distance d (say your speed for 1 second) and your heading t, you will have v = (d*cos(t),d*sin(t)). Just add this to your old vector to get your predicted vector.

    In other words,

    predictedx = x + d*cos(t)
    predictedy = y + d*sin(t)

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: compass angle prediction question

    The displacement vector needs to depend on delta-t. It won't always be 1 unit of time.

  6. #6
    Join Date
    Jan 2012
    Posts
    49

    Re: compass angle prediction question

    Can you elaborate Lindley?

    Thanks,
    Alex

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: compass angle prediction question

    Just look at the units. I don't know what your units are, but for argument let's say meters and seconds.

    You have a position (m) and a velocity (m/s). You also have a delta_time (s). You know that your displacement vector has to have units of m, therefore clearly it has to be m/s*s = m, or velocity*delta_time.

  8. #8
    Join Date
    Apr 2012
    Posts
    2

    Re: compass angle prediction question

    I said 1 second because that's what we talked about in IRC, but yeah d = seconds*speed.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: compass angle prediction question

    Quote Originally Posted by aseminov View Post
    Are my other things correct?
    You're in trouble because you've chosen a non-standard angle definition. There a two so called principal angle definitions for a 2D Cartesian coordinate system but this is the one most in use I think,

    Code:
                                           90 y
                                           |
                                           |
                                +-180-------------- 0 x
                                           |
                                           |
                                          -90
    By adopting this standard everything becomes so much easier. It's used in textbooks, handbooks, standard libraries and generally assumed in discussions.

    The relation between angle, distance and position coordinates will be,

    x = distance * cos(angle)
    y = distance * sin(angle)

    The inverse will be,

    distance = sqrt(x*x + y*y)
    angle = atan2(y,x)

    And the prediction formula suggested by sifgfisdogjjpgij is now correct,

    predictedx = x + d*cos(t)
    predictedy = y + d*sin(t)

    But of course you can still express standard angles in other definitions, and back again. It's a simple (piecewise) linear relationship as I explaned in one of your previous threads. So use standard angles for calculation (in radians throughout) and your own angle definition for presentation.
    Last edited by nuzzle; April 17th, 2012 at 04:37 AM.

  10. #10
    Join Date
    Jan 2012
    Posts
    49

    Re: compass angle prediction question

    Hello Nuzzle,

    I think if I just reverse the sin's and cos's and reverse x and y in the atan2() the code all works. Correct?


    Thanks!
    Alex
    Last edited by aseminov; April 17th, 2012 at 07:40 AM.

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: compass angle prediction question

    Quote Originally Posted by aseminov View Post
    I think if I just reverse the sin's and cos's and reverse x and y in the atan2() the code all works. Correct?
    That should work in theory. Your angle definition means you're shifting the trigonometric functions PI/2 degrees to the right and then negating the angle. If you do that cos becomes sin and sin becomes cos indeed.

    But you're still using standard functions that are designed to work with a standard coordinate system. So in practice you still need to be very careful making sure results become correctly mapped to your angle definition.

    I just don't see why you have to introduce the additional complication of mixing standard coordinates with non-standard angles that are rotated and reversed? But it's your call. Please go ahead and shoot yourself in the foot.
    Last edited by nuzzle; April 18th, 2012 at 01:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured