Angle Rotation
Blitz3D Forums/Blitz3D Beginners Area/Angle Rotation
| ||
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? :) |
| ||
There are probably cleverer ways, but this ought to workIf a-b>180 b=b+360 End If If a-b<-180 a=a+360 End If |
| ||
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 |
| ||
Do you realise just how useful that little snippet really is??? Thanks :) (Im happy) |
| ||
This might help...Function AngleDifference#(angle1#,angle2#) Return ((angle2 - angle1) Mod 360 + 540) Mod 360 - 180 End Function |
| ||
> 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! |
| ||
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... |
| ||
I thought Mod was a slower command. Anyway I'll give it a whirl and see how it goes. BAPS! ;) |
| ||
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 |
| ||
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 :) |
| ||
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. |