How do I know when to increase or decrease angle to get a specific angle?

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-06-11T08:01:55Z Indexed on 2010/06/11 8:02 UTC
Read the original article Hit count: 181

Filed under:
|
|

Hi. I am programming a game and I have come to a very hard spot. Basically I have a circle and I have 2 angles on this circle. Angle 1 (A) is a point I want angle 2 (B) to go to. During my game every frame I need to check weither or not to increase or decrease my angle value by a certain amount (speed) to eventually reach the first angle. My question is how do I do this?

I tried doing this but I don't seem to be doing it right.

bool increase = false;

float B = [self radiansToDegrees:tankAngle]; float A = [self radiansToDegrees:tankDestinationAngle]; float newAngle = B;

if(B < A) {

float C = B - (360 - A); float D = A - B;

if(C < D) increase = false; else increase = true;

} else if(B > A) {

float C = B - A; float D = A - (360 - B);

if(C < D) increase = false; else increase = true;

}

if(increase) { newAngle += 1.0; } else { newAngle -= 1.0; }

if(newAngle > 360.0) { newAngle = 0 + (newAngle - 360.0); } else if(newAngle < 0.0) { newAngle = 360 + newAngle; }

if(newAngle == 0) newAngle = 360;

newAngle = [self degreesToRadians:newAngle];

[self setTanksProperPositionRotation:newAngle];

The basic effect I am trying to achieve is when the user makes a new point, which would be angle 1, angle 2 would move towards angle 1 choosing the fastest direction. I think I have spent around 4 hours trying to figure this out.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about math