Improve my homing missiles

BlitzMax Forums/BlitzMax Programming/Improve my homing missiles

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.

Got the missiles working but having some problems getting then to point in the correct direction. heres some code (no media needed)

If you click on Target 1 the missiles come out pointing backwards, click on Target 2 and thay come out sideways.

The problem seems to be that thay point to the target regarless of what direction they are moving it.. hard to explain, but running the code should show you what i mean.


Graphics 640,480,0,70

Global LMissile:TList=New TList
Type TMissile
	Field x#,y# Field Velx#,Vely# Field Angle#,Drag#=0.980,Speed#=0.1 Field TargetX,TargetY
	Field Life
End Type

'=====================================

Repeat ; Cls
	
	UpdateMissile()
	
	If MouseHit(1)=True Then  MakeMissile()
	
	DrawOval 320-8,240-8,16,16
	DrawText "Target 1",32,32
	DrawText "Target 2",32,440
	
FlushMem() ; Flip ; Until KeyDown(KEY_ESCAPE)=True ; End

'=====================================

Function UpdateMissile()

	For m:TMissile = EachIn LMissile
		
		m.angle = ATan2(m.TargetY - m.Y , m.TargetX - m.X)
		
		m.Velx=m.Velx * m.Drag + m.Speed * Cos(m.angle)
		m.Vely=m.Vely * m.Drag + m.Speed * Sin(m.angle)

		m.X:+m.VelX
		m.Y:+m.VelY
		
		SetRotation(m.angle)
			SetColor 0,0,255 ; DrawRect m.X,m.Y,24,12
			SetHandle(-16,-2)
			SetColor 255,0,0 ; DrawOval m.X,m.Y,8,8
			SetHandle(0,0)
		SetRotation(0)
		
		If MilliSecs()>m.Life Then ListRemove(LMissile,m)
	Next

End Function

'=====================================

Function MakeMissile()

	m:TMissile = New TMissile
	ListAddLast(LMissile,m)
		m.x		= 320
		m.y		= 240
		m.Velx		= 4
		m.Vely		= 4
		m.Life		= MilliSecs()+3000
		m.TargetY	= MouseY()
		m.TargetX	= MouseX()

End Function