how to code 'Scramble' missiles

BlitzMax Forums/BlitzMax Beginners Area/how to code 'Scramble' missiles

'ere try this, always remember its atan2(Y,X) not X,Y and the sign of x & y is important!

you also need an extra test to allow for "cones" that span the zero angle...


Strict

Graphics 800,600

Global List_cones : TList = CreateList()

Type TCone

	Field x
	Field y
	Field Angle
	Field Rotation
	Field Length 
	
	Function Create:TCone (x:Int, y:Int, Angle : Int, Rotation : Int, Length : Int= 200)
	
		Local c:TCone = New TCone
		
		c.x = x
		c.y = y
		c.Angle = (Angle + 360) Mod 360
		c.Rotation = (Rotation + 360) Mod 360
		c.Length = Length

		
		ListAddLast List_cones, c
		
		Return c
		
	End Function
	
	Function Show_all ()
	
		For Local c:TCone = EachIn List_cones
		
			SetColor 255,0,0
			DrawOval c.x-2,c.y-2,4,4
			
			SetColor 255,0,0
			SetRotation (c.Rotation)
			DrawLine c.x, c.y, c.x + c.Length, c.y
			
			SetColor 0,255,0
			SetRotation (c.Rotation + c.Angle)
			DrawLine c.x, c.y, c.x + c.Length, c.y

			SetRotation (c.Rotation - c.Angle)
			DrawLine c.x, c.y, c.x + c.Length, c.y
						
			SetRotation 0
			c.rotation:+1
			c.rotation = (c.rotation + 360) Mod 360			

		Next
	
	End Function
	
	Function Check_in_view ()
	
		Local d, dx, dy, angle, angleinf, anglesup
	
		For Local c:TCone = EachIn List_cones
		
			' ... need help to write code here ;-)
			
			dx = (px - c.x)
			dy = (py - c.y)
			angle = ATan2(dy,dx) 
			'angle = (angle + 360) Mod 360
			If angle<0 Then angle:+360
			
			
			angleinf = c.Rotation - c.angle  
			anglesup = c.Rotation + c.angle  
			
			'angleinf = (angleinf + 360) Mod 360
			'anglesup = (anglesup + 360) Mod 360
			If angleinf<0 Then angleinf:+360
			

			
			
			d = Int (Sqr ((px - c.x) * (px - c.x) + (py - c.y) * (py - c.y)))			
			
			SetColor 255,255,255
			DrawText "anglesup = " + String (anglesup), c.x, c.y - 10
			DrawText "angleinf = " + String (angleinf), c.x, c.y + 10
			DrawText "playerangle = " + String (angle), c.x, c.y + 30
			DrawText "dist= " + String (d), c.x, c.y + 50
						
			If (angle)> angleinf And (angle) < anglesup And d <= c.Length Then
			
				SetColor 255,0,0
				DrawText "inside", c.x , c.y
			End If
			
			If angleinf>anglesup Then
				If angle<anglesup Or angle>angleinf Then
					SetColor 255,0,0
					DrawText "inside2", c.x , c.y	
				EndIf			
			EndIf
		
		Next
	
	End Function
	
End Type

Global px = MouseX()
Global py = MouseY()

TCone.Create (100,200,25, -45)

TCone.Create (400,300,25, 0)
TCone.Create (200,300,25, 180)

TCone.Create (100,400,25, 45)



While Not KeyDown (KEY_ESCAPE)

	Cls
	
	px = MouseX()
	py = MouseY()
	
	TCone.Show_all()
	TCone.Check_in_view()
	
	SetColor 0,255,0
	DrawOval px-2, py-2,4,4
	Flip
	
Wend