BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Finding the difference between 2 angles."
, by thechange
Included are 5 different ways to find the difference between 2 angles where 0 degrees means up.
Code
; Imagine you want to land a ship on a planet. ; You can calculate the angle from the planet to the ship using ATan2. ; But you only want to allow a maximum of 45 degrees difference ; between the player ship's angle and the landing angle. ; Then you can for example make the ship explode if it's not landing ; with the right angle (and speed too ofcourse :) ; Thanks to various people from BlitzCoder who gave their solution ; for solving this angle-related problem. ; Displayed below the test program and the functions of each method. Const MaxDiff# = 45 ; 45 degrees to the left and to the right Const a2 = 0 ; Second angle (doesn't matter which is which) Graphics 400 , 800 , 0 , 2 Print "Max: " + MaxDiff For a1# = 0 To 360 Step 10.0 ; Test all angles (+1) to see the results Write a1 + " - " + a2 + " =" Write " (E) " + angdif2 ( a1 , a2 , maxdiff ) Write " (F) " + angledifference2 ( a1 , a2 , maxdiff ) Write " (B) " + angdif ( a1 , a2 , maxdiff ) Write " (C) " + diff ( a1 , a2 , maxdiff ) Write " (T) " + AngleDifference ( a1 , a2 , maxdiff ) Print Next WaitKey End ;= By Eelco Function angdif2(angle1,angle2,max) dif=Abs(angle1-angle2) Mod 360 If dif
180) Then Difference = 360 - Difference End If Return(Abs(Difference) <= MaximumDifference) End Function ;= By BasicGamer2 Function angdif(currentangle,newangle,maxdiff) currentangle=newangle-currentangle Return Abs(currentangle-currentangle/180*360)<=maxdiff End Function ;= By CyberSeth Function diff(angle1,angle2,maxdiff) If angle1-angle2>180 angle1=angle1-360 If angle2-angle1>180 angle2=angle2-360 Return Abs(angle2-angle1)<=maxdiff End Function ;= By TheChange Function AngleDifference ( Angle1 , Angle2 , MaximumDifference ) ; Angles must be 'near' each other (exception) Difference = Angle1 - Angle2 If Abs ( Difference ) >= 360 - MaximumDifference If Angle1 > Angle2 Angle2 = Angle2 + 360 Else Angle1 = Angle1 + 360 End If End If ; Real difference Difference = Angle1 - Angle2 If Abs ( Difference ) <= MaximumDifference Return True Else Return False End If End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations