angles...

Monkey Forums/Monkey Code/angles...

Paul - Taiphoz(Posted 2013) [#1]
Source [ Download ]
Test Play

Just some code to make a sprite turn to target/face something, probably a cleaner way of doing this, in fact if there is please share, either way, hope this helps.

Strict
Import mojo

Global RedArrow:Image
Global BlueArrow:Image
Global targetx:Float
Global targety:Float
Global movetime:Int


Class MyApp Extends App

	Method OnCreate:Int()
		SetUpdateRate 60

		RedArrow = LoadImage("red_arrow.png",,Image.MidHandle)
		BlueArrow = LoadImage("blue_arrow.png",,Image.MidHandle)
		
		targetx=Rnd(640)
		targety=Rnd(480)
		movetime=Millisecs()
		
		For Local l:Int = 1 To 100
			Local ta:cArrow = New cArrow()
		Next

		Return 0
	End
	
	Method OnUpdate:Int()

		If Millisecs()-movetime > 1500
			movetime=Millisecs()
			targetx=Rnd(640)
			targety=Rnd(480)
		End If


		If TouchHit()
			Local ta:cArrow = New cArrow(BlueArrow)
		End If

		For Local ta:cArrow = Eachin ArrowList
			ta.Update()
		Next
				
		Return 0		
 	End
	
	Method OnRender:Int()
		Cls

		For Local ta:cArrow = Eachin ArrowList
			DrawImage(ta.sprite,ta.x,ta.y,ta.angle,1,1,0)
		Next
			
		Return 0	
	End
	
End

Global ArrowList:List<cArrow> = New List<cArrow>

Class cArrow
	Field sprite:Image
	Field x:Float
	Field y:Float
	Field velx:Float
	Field vely:Float
	Field speed:Float
	Field angle:Float
	Field turnrate:Float
	
	
	Method New(_image:Image=RedArrow)
		Self.sprite = _image
		Self.x=Rnd(640)
		Self.y=Rnd(480)
		Self.angle=ATan2(targetx-Self.x,targety-Self.y)
		Self.velx=Sin(Self.angle)
		Self.vely=Sin(Self.angle)
		Self.speed=Rnd(2,6)
		Self.turnrate=Rnd(0,5)
		ArrowList.AddLast(Self)
	End Method
	
	Method Update:Void()
		If TouchDown()
			Self.angle=TurnTo(Self.angle,ATan2(MouseX()-Self.x,MouseY()-Self.y),Self.turnrate)
		Else
			Self.angle=TurnTo(Self.angle,ATan2(targetx-Self.x,targety-Self.y),Self.turnrate )
		End If
		Self.velx=Sin(Self.angle)
		Self.vely=Cos(Self.angle)	
		Self.x+=Self.velx*Self.speed
		Self.y+=Self.vely*Self.speed
	End Method
	
	Method Render:Void()
		DrawImage(Self.sprite,Self.x,Self.y,Self.angle)
	End Method	
	
End Class


Function Main:Int()
	New MyApp	
	Return 0
End


Function Clerp:Float(_start : Float, _End : Float, _value : Float)
	Local min:Float = 0;
	Local max:Float = 360;
	Local half:Float = Abs((max - min)/2.0)
	Local retval:Float = 0.0;
	Local diff:Float = 0.0;
 
	If ((_End - _start) < -half) Then
		diff = ((max - _start)+_End)*_value
		retval =  _start+diff		
	Else If((_End - _start) > half)
		diff = -((max - _End)+_start)*_value
		retval =  _start+diff
	Else
		retval =  _start+(_End-_start)*_value
	Endif
	
   	Return retval
End

Function TurnTo:Float(_start : Float, _End : Float, _step : Float)
	Local min:Float = -180;
	Local max:Float = 180;
	Local half:Float = 0
	Local retval:Float = 0.0;
	
 	If (_End - _start) < -half And (_End - _start)>min And (_End - _start)<max Then
			retval = (_start-_step)
			
		Elseif (_End - _start) > half And (_End - _start)>min And (_End - _start)<max Then
			retval = (_start+_step)	
					
		Elseif (_End - _start) < min Then
			
			retval = (_start+_step)
			Repeat
				If retval<-180 Then retval+=360
				If retval>180 Then retval -=360
			Until retval>=-180 And retval<=180
			
		Elseif (_End - _start) > max Then

			retval = (_start-_step)
			Repeat
				If retval<-180 Then retval+=360
				If retval>180 Then retval -=360
			Until retval>=-180 And retval<=180
						
		Else
			retval = _start
	Endif
 	
   	Return retval
End



Raul(Posted 2013) [#2]
usefull code man. thank you for sharing it.


Paul - Taiphoz(Posted 2013) [#3]
I'm tempted to make them shoot at each other lol.. and no problem, it's not often I can share code so when I can I do.

as long as it helps at least 1 person I am happy.