Improve my homing missiles

BlitzMax Forums/BlitzMax Programming/Improve my homing missiles

coffeedotbean(Posted 2005) [#1]
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




Robert(Posted 2005) [#2]
If you set the initial velocity to 0 in the MakeMissile function that makes them point correctly - otherwise they already have thrust towards the bottom-left corner of the screen (that is, set m.Velx and m.Vely to 0)


Robert(Posted 2005) [#3]
Here you go - I changed MakeMissile set the initial velocity and initial angle correctly. I also changed the angle calculation so that the missile curves towards its target rather than always pointing directly at it.

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
		
		Local newAng:Float=ATan2(m.TargetY - m.Y , m.TargetX - m.X)
		Local angDiff:Float=newAng-m.angle
		
		If (angDiff >= 180) angDiff=angDiff+180
		
		m.angle :+ angDiff/5.0
		
		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.TargetY	= MouseY()
		m.TargetX	= MouseX()
			
		m.angle     = ATan2(m.TargetY - m.Y , m.TargetX - m.X)
		
		
		m.Velx		= 4*Cos(m.angle)
		m.Vely		= 4*Sin(m.angle)
		
		m.Life		= MilliSecs()+3000
		

End Function



coffeedotbean(Posted 2005) [#4]
thanks robert, but not exactly what i needed, but i solved it this moring, a good night sleep always helps.

heres the code as i wanted it.

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 Field angle2#
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.angle2 = ATan2((m.Y+m.VelY) - m.Y , (m.X+m.VelX) - 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.angle2)
			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



Ziltch(Posted 2005) [#5]
isn't
m.angle2 = ATan2((m.Y+m.VelY) - m.Y , (m.X+m.VelX) - m.X)

the same as
m.angle2 = ATan2(m.VelY , m.VelX)

?