Homing missiles math
BlitzMax Forums/BlitzMax Beginners Area/Homing missiles math| 
 | ||
| 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. | 
| 
 | ||
| <shameless plug>and if you want to do it in 3d :D wander over to my site and check out my quat pointing tutorial</shameless plug> | 
| 
 | ||
| http://www.blitzbasic.com/Community/posts.php?topic=55665&hl=guided | 
| 
 | ||
| The example linked by hub is a bit limited in that it doesn't actually include any turn rate limiting, i.e the missile will always point immediately at the target. I've hacked the two together for a slightly nicer example. Play with missilespeed and turnrate for some fun. The higher the turnrate the smarter the missile Grab this image:  | 
| 
 | ||
| my homing missile code. and hub, you code is useless for a missile, don't have head and tail. 
Strict
Graphics 640,480
SetMaskColor 255,0,255
AutoMidHandle True
Global Image:TImage=LoadImage("Missile.png",MASKEDIMAGE)
Global Angle:Int
Global Acel:Int=3
Global TurnSpeed:Int=5   '0 fast , 5 slow
Global X:Int
Global Y:Int
Global TargetX:Int
Global TargetY:Int
While Not KeyHit(KEY_ESCAPE)
	TargetX=MouseX()
	TargetY=MouseY()
	movemissile()
	Cls
	SetRotation Angle
	DrawImage Image, X,Y
	SetRotation 0
	Flip
Wend
Function movemissile()	
	If Sqr((X-TargetX)^2+(Y-TargetY)^2)>20 Then
		Local GotoAngle:Int = ATan2(Y-TargetY,X-TargetX)
		Local tmpAngle:Int=GotoAngle-Sgn(GotoAngle-Angle)*360
		If Abs(GotoAngle-Angle)>Abs(tmpAngle-Angle) Then GotoAngle=tmpAngle
		If Angle<>GotoAngle Then Angle:-Sgn (GotoAngle-Angle)*(180-Abs(GotoAngle-Angle))/(1+Acel*TurnSpeed)
		If Angle=>360 Then Angle:-360 Else If Angle<0 Then Angle:+360
		X:+Cos(Angle)*acel
		Y:+Sin(Angle)*acel
	EndIf
EndFunction
 | 
| 
 | ||
| BlackSp1der's is a far more elegant solution :) |