Angle Rotation

Blitz3D Forums/Blitz3D Beginners Area/Angle Rotation

(tu) ENAY(Posted 2004) [#1]
I've got this really simple problem, yet I just can't seem to figure it out.

I have two integer angle variables that are used to represent 0-360 degrees.
The first tries to be the same as the second variable by adjusting per game loop

So I have something like:-

if a > b then
a=a+1
else
a-a+1
endif


Except when a is close to b and b goes past 0 to 359 angle a thinks that angle b has suddenly become massive so it goes the long way round to 359 instead of realising that it can move below 0 as well.

I came close by saying that when they're over 180 degrees do something like 360-a.
But it didn't work fully and I just can't seem to figure out how I can overcome this.

Can anyone help? :)


Gabriel(Posted 2004) [#2]
There are probably cleverer ways, but this ought to work

If a-b>180
   b=b+360
End If
If a-b<-180
   a=a+360
End If



(tu) ENAY(Posted 2004) [#3]
Cheers Sybixus. With your help (and someone elses ) I came up with this.

temp_angle = b - a;

while a < 0 
temp_angle = temp_angle + 360;
end while
				
if a > 180
a=a+1
else
a=a-1
endif



_PJ_(Posted 2004) [#4]
Do you realise just how useful that little snippet really is???

Thanks :)

(Im happy)


Rob Farley(Posted 2004) [#5]
This might help...
Function AngleDifference#(angle1#,angle2#) 
Return ((angle2 - angle1) Mod 360 + 540) Mod 360 - 180 
End Function



(tu) ENAY(Posted 2004) [#6]
> Do you realise just how useful that
> little snippet really is???

Yes ;)

> This might help...

Is that faster Rob baby? It looks like a hell of a lot more cpu power with there being two mod's in there.
BAPS!


Rob Farley(Posted 2004) [#7]
No idea! I just noticed you've got a loop in yours so that could potentially use up many cycles. And it actually tells you want the angle difference is between 2 angles. Which is pretty handy.

Also, I'd like to point out this is Halo's code not mine, I wouldn't want to take the credit for someone elses work... Unless of course it made me millions...


(tu) ENAY(Posted 2004) [#8]
I thought Mod was a slower command. Anyway I'll give it a whirl and see how it goes. BAPS! ;)


RifRaf(Posted 2009) [#9]
lol, 5 years old ! well the search engine here is working well , this snippet will be useful for interpolating entity rotation angles over a network game. Curious though is there a faster equation for this ?


Function AngleDifference#(angle1#,angle2#)
Return ((angle2 - angle1) Mod 360 + 540) Mod 360 - 180
End Function




RifRaf(Posted 2009) [#10]
ok well.. this "slightly" faster
it takes a loop of about 500k to 1mil to see a big difference.. so its not much faster.

Function AngleDifference2#( CurrentAngle# , TargetAngle# )
    If TargetAngle - CurrentAngle > 180 Then TargetAngle = TargetAngle - 360
    If CurrentAngle - TargetAngle > 180 Then CurrentAngle = CurrentAngle - 360
    Return  TargetAngle - CurrentAngle
End Function


sorry for the post ressurection :)


_PJ_(Posted 2009) [#11]
Wow! I appreciate the bump of this.. I had forgotten all about it.

Not sure, but maybe sticking a "Mod 360" in there somewhere may help too? It might not be necessary, but as a precaution, perhaps.