homing and math

BlitzMax Forums/BlitzMax Beginners Area/homing and math

hub(Posted 2007) [#1]
Hi

i use the following code (from another forum topic) in my game, but it seems there is a bug. Sometimes, anglem > 360. No pb with this version, but i my game i use a pre-computed sin/cos into a table (0 to 360 degree) so it produce a bug when anglem > 360... (array out of bound).
so...

1) is there a way to correct the code to have 0 <= anglem <= 360 ?

2) why cos() and sin() accept a value greater than 360 ? Is there a conversion before computed the sin or cos ? How to do this conversion myself ?

i'm crap with maths ! Thanks !

(initial topic
http://www.blitzbasic.com/Community/posts.php?topic=57317#637433)

Global missile_x#=100.0,missile_y#=100.0, targetx#=100.0, targety#=100.0
Global anglem#=0 , anglet#, dist#
Global missilespeed#=4.0, turnrate#=2.0, missile_img

Graphics 640,480

SetMaskColor 255,0,255

While Not KeyHit(KEY_ESCAPE)
  Cls
  SetRotation anglem#
  DrawRect missile_x,missile_y,20,5 
  SetRotation 0
  movemissile()  
  Flip
Wend

Function movemissile()	
	Local dx, dy

    targetx# = Float(MouseX())
    targety# = Float(MouseY())
	dx=targetx-missile_x
	dy=targety-missile_y
	
	anglet# = ATan2(dy,dx) ' dir between player and target
	dist=Sqr((dx*dx)+(dy*dy))

	' homing
	While anglem > 360 anglem:-360 Wend
	While anglem <= 0 anglem:+360 Wend		
						
	Local TE# = RotaryDir( anglem# , anglet)
			
	If TE# < 0 Then anglem:- Turnrate
	If TE# > 0 Then anglem:+ Turnrate
	'
	
	If anglem > 360 Then DrawText "anglem > 360 !!!!",0,20
		
	xs# = Cos(anglem) * missilespeed
	ys# = Sin(anglem) * missilespeed

	missile_x# = missile_x + xs
	missile_y# = missile_y + ys 
	
	If dist < 10.0 
	   missile_x=targetx
	   missile_y=targety
	
	 	DrawText "GOTCHA!",missile_x, missile_y+30
	EndIf 
	
	DrawText "anglem=" + anglem, 0,0
	
End Function 


Function RotaryDir#(SourceDir#,DestDir#)

	Local Diff1#,Diff2#,Dir#

	If SourceDir# > DestDir#
		Diff1#=SourceDir-DestDir
		diff2#=(360.0-SourceDir)+DestDir
		If diff2<diff1
			dir#=diff2
		Else
			dir#=diff1/-1
		EndIf
	Else
		If SourceDir#<DestDir#
			diff1=DestDir-SourceDir
			diff2=(360.0-DestDir)+SourceDir
			If diff2<diff1
				dir#=diff2/-1
			Else
				dir#=diff1
			EndIf
		Else
			dir=0
		EndIf
	EndIf
	Return dir
End Function



Tom Darby(Posted 2007) [#2]
Put your bound-checking after you've performed your rotation tweak:

	Local TE# = RotaryDir( anglem# , anglet)
			
	If TE# < 0 Then anglem:- Turnrate
	If TE# > 0 Then anglem:+ Turnrate

	While anglem > 360 anglem:-360 Wend
	While anglem <= 0 anglem:+360 Wend		



H&K(Posted 2007) [#3]
Angle =(Angle Mod 360)

Edit DO we have "Angle:Mod 360"


Brucey(Posted 2007) [#4]
What H&K said.

It guarantees that the angle is always between 0 and 360.


hub(Posted 2007) [#5]
Many Thanks !!!


Jesse(Posted 2007) [#6]
@brucey
no. It guarantees that the angle is between 0 and 359 inclusive.

but it works because 360 and 0 degrees is the same.


Brendane(Posted 2007) [#7]
if 0 and 360 are the same then he was correct ;)