Advertisement

07.21.2008 at 06:45AM PDT, ID: 23581758
[x]
Attachment Details

Angle Math rotates wrong way when passing origin

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.0
I am developing a Missile class in VB.NET that tracks its target. It is given a Target (as a Vector2, aka a point), a velocity and a rotation rate (radians per second). Every time its Update() method is called, it calculates its next position and the angle it must now face in the following way:

1) Move forward by Velocity * Elapsed in direction CurrentAngle (elapsed is the time elapsed since the last update call)
2) Calculate the Target Angle (which way the missile wants to face) using Atan2
3) Check if the difference between the two angles is more than RotationRate * Elapsed. If not, just set CurrentAngle = TargetAngle and skip the rest.
4) If TargetAngle > CurrentAngle, then add RotationRate * Elapsed to angle, else subtract RotationRate * Elapsed from it.

The problem is that when the correct rotation path goes through the origin (angle goes from Pi*2 to 0) it rotates back the wrong way. I've tried lots of ways to fix this and spent hours debugging but I just can't get it to work. Could someone sort this bug out for me?

Thanks in advance,
Burningmace
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
position += new Vector2((float)Math.Sin(CurrentAngle) * Velocity * Elapsed, (float)Math.Cos(CurrentAngle) * Velocity * Elapsed);
 
            float TargetAngle = (float)(Math.PI) - (float)Math.Atan2(target.Y - position.Y, target.X - position.X);
            if (target.X == position.X)
            {
                TargetAngle = (float)(Math.PI * 0.75f);
            }
            if (target.Y == position.Y)
            {
                TargetAngle = 0;
            }
 
            float angleDelta = Math.Abs(CurrentAngle - TargetAngle);
 
            if (angleDelta >= RotationSpeed * Elapsed)
            {
                // We need to cap to RotationSpeed
                if (AngleDirection(CurrentAngle,TargetAngle))
                {
                    CurrentAngle += RotationSpeed * Elapsed;
                }
                else
                {
                    CurrentAngle -= RotationSpeed * Elapsed;
                }
            }
            else
            {
                // We can rotate without limitation!
                CurrentAngle = TargetAngle;
            }
Answered By: burningmace
Expert Since: 05/08/2003
Accepted Solutions: 28
burningmace has been an Expert for 5 years 8 months, during which he has posted 373 comments and answered 28 questions. burningmace is just one of 64 experts in the Physics & Artificial Intelligence in Game Programming Zone. 1 expert collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-48 / EE_QW_2_20070628