Homing missiles math

BlitzMax Forums/BlitzMax Beginners Area/Homing missiles math

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

EDIT
Solved it, I found an old B3d file :P

The solution:

Method Homing()
				
	While Dir > 360 Dir:-360 Wend
	While Dir <= 0 Dir:+360 Wend		
						
	Local TE# = RotaryDir( Dir# , iDir)
			
	If TE# < 0 Then Dir:- Turnrate*Delta.Time
	If TE# > 0 Then Dir:+ Turnrate*Delta.Time

EndMethod


This is the function I found that solved the problem. it returns the closest angle to turn to get to target, negative or Positive. Probably taken from the code archives a very long time ago.
Function RotaryDir#(SourceDir#,DestDir#)
Local Diff1#,Diff2#,Dir#
	If SourceDir#>DestDir#
		Diff1#=SourceDir-DestDir
		diff2#=(360.0-SourceDir)+DestDir
		If diff2<diff1
			dir#=diff2
		Else
			dir#=diff1/-1
		EndIf
	Else
		If SourceDir#<DestDir#
			diff1=DestDir-SourceDir
			diff2=(360.0-DestDir)+SourceDir
			If diff2<diff1
				dir#=diff2/-1
			Else
				dir#=diff1
			EndIf
		Else
			dir=0
		EndIf
	EndIf
	Return dir
End Function