# Rotation difference [SOLVED]

6 posts

 I feel like a dumb dumb having problems with this, and probably just need a pair of fresh eyes. My problem is simple: Rotation in AS3 goes as -180 to +180, and I want to find the difference between two rotations. So finding it – only having one direction in mind is simple, but if I want to take the other direction in regards, it get slightly more tricky. Could be wrong but my guess would be: difference = rotation2 – rotation1; while (difference<-180) difference += 360; while (difference>180) difference -= 360; Cheers! That seems to have done the trick – my previous attempt was close, but not close enough :) You could imagine a situation in which you have a pool of objects (call them rotators), half of which always rotate clockwise and the other half always rotate counter-clockwise so that, over time, the rotation value of these objects tends to either +infinity or -infinity. Further imagine that, every frame, you want to know the rotation difference between every pair of rotators. The magnitude of the difference as suggested, when taken between a clockwise rotator and a counter-clockwise rotator can be, after enough time has elapsed, arbitrarily large, leading to one of the while loops needing an arbitrarily large number of passes to compute a difference in the ]-180; 180] interval. This would in turn yield lag. Instead of using while loops, you might want to simply compute the number of loops and subtract it from the value of the rotation. ```difference = rotation2 - rotation1; var loops:int = (difference + 180)/ 360; difference -= loops * 360; ``` Bam! Done. No while, no lag. The final value is in the [-180; 180[ range because that’s what you wanted. If you simply wanted a value in the [0; 360[ range you’d take out the +180 in the loops formula. Also, for the record, rotation in AS3 does not go as -180 to 180. Edit: \/ Yea, if you’re using the .rotation property the value you set for the rotation is already converted to the [-180, 180] range, so indeed all values you would obtain for the difference should be limited to the [-360; 360] range and neither while loop can run more than once. But then there’s no reason to have two while loops where two if statements would do the job, and still no reason to have two if statements where one operation will work as well. If I understand you correctly… I am only going to look into what direction my objects are currently pointing towards, rather than looking at how much they have rotated in total. -180 to 180 is coming from the rotation property You’d expect rotationDifferent%360 would work, but I’ve had some trouble with it in the past. It should be the quickest solution (what Ace said, but it does it automatically). Then, subtract 180 at the end to get in the desired range.