how to code 'Scramble' missiles

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

Hi !
i want add to my game some missiles like the old classic game Scramble. Missile is launched when player is near it.

What's the better method and what is the formulae to determine that player (px, py) is inside the view zone (see picture) ?

1) linepick ?
2) cone ?
3) 'oriented' rectangle
4) classic rectangle



for info and your pseudo code have this :

type tMissile
field x
field y
field Angle
end type

Many thanks for your help

If theres minimal obstacles, you could use an image drawn as a cone, and check for collision with the player?

The cone image is a good idea, but i've already a lot of images collisions tests into my game.



Is there a math formulae to check if a point is inside a cone ?

A 2D cone?

You need to find the angle one point is at (the player) relative to another point (the small end of the cone) and then detrmine if it is between angles (lookdirection-conefov/2) and (lookdirection+conefov/2).

I'm sure there's code in the code archives to find the angle of one point relative to the other. I think it involves Atan or Atan2.



a= Atan2(dx,dy)

yep atan2 and a "cone" (flat 2d code=triangle) has my vote!

atan2 I wonder if this one command has the most uses out of all the functions of just about any language

Thanks, i'm trying to transform your explanations into code. Here a code that draw some cones and try to do the job ! But not work...

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.y - c.Length
			
			SetColor 0,255,0
			SetRotation (c.Rotation + c.Angle)
			DrawLine c.x, c.y, c.x, c.y - c.Length

			SetRotation (c.Rotation - c.Angle)
			DrawLine c.x, c.y, c.x, c.y - c.Length
						
			SetRotation 0
			

		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 = Abs (px - c.x)
			dy = Abs (py - c.y)
			
			angle = ATan2(dx,dy) 
			angle = (angle + 360) Mod 360
			
			angleinf = c.Rotation- c.angle  
			anglesup = c.Rotation + c.angle  
			
			angleinf = (angleinf + 360) Mod 360
			anglesup = (anglesup + 360) Mod 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> c.Rotation And angle < anglesup And d <= c.Length Then
			
				SetColor 255,0,0
				DrawText "inside", c.x , c.y
			End If
			
			If angle < c.Rotation And angle > angleinf And d <= c.Length Then
			
				SetColor 255,0,0
				DrawText "inside2", c.x , c.y
			End If
			
		
		Next
	
	End Function
	
End Type

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

TCone.Create (100,200,25, 0)

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

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



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
	


'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
	
	
	


Many thanks Chris, i've spent two hours to search what was wrong inside my previous code !!!

yeah - an sometimes you're just to close to the forest to see the trees

Wouldn't you have to be far from the forest not to be able to see the individual trees? Or be unable to see the forest because you're so close all you can see is trees?