Advertisement

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

Angle Math rotates wrong way when passing origin

Asked by burningmace in Physics & Artificial Intelligence in Game Programming, Math & Science Questions, Microsoft Visual Basic.Net

Tags: VB.NET

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,
BurningmaceStart Free Trial
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;
            }
 
 
[+][-]07.21.2008 at 07:45AM PDT, ID: 22050946

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 05:39AM PDT, ID: 22068501

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 07:38AM PDT, ID: 22069767

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 11:04AM PDT, ID: 22072051

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 12:24PM PDT, ID: 22072892

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 05:44PM PDT, ID: 22075310

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 01:54PM PDT, ID: 22083437

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 06:11AM PDT, ID: 22088157

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 07:30AM PDT, ID: 22088910

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2008 at 12:56PM PDT, ID: 22096043

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.28.2008 at 07:00AM PDT, ID: 22103171

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.20.2008 at 06:02AM PDT, ID: 22268892

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.20.2008 at 06:57AM PDT, ID: 22269447

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628