Whats up with this ?

Monkey Forums/Monkey Programming/Whats up with this ?

Paul - Taiphoz(Posted 2016) [#1]
left and right mouse to switch between targets, so the issue is that when right clicking( do it a few times) you will notice something fishy with the direction of travel, the angles it's getting are off, but working as intended when going to the left.

Import mojo

Class Test Extends App

		
	Field velx:Float
	Field vely:Float
	Field x:Float
	Field y:Float
	Field angle:Float
	Field speed:Float
	Field moving:Bool
	Field target:Int
	
	Method OnCreate()
		SetUpdateRate 60
		Self.moving=False
		
	End
	
	
	
	Method OnUpdate()
		If MouseDown(0)
			Self.x=320
			Self.y=220
			Self.angle=Rnd(-180,180)
			Self.SetVelocities()
			Self.speed=5
			Self.moving=True
			Self.target=1
		Endif
		
		If MouseDown(1)
			Self.x=320
			Self.y=220
			Self.angle=Rnd(-180,180)
			Self.SetVelocities()
			Self.speed=5
			Self.moving=True
			Self.target=2
		Endif		
		
		If Self.moving
			Self.x+=Self.velx*Self.speed
			Self.y+=Self.vely*Self.speed

			Select Self.target
				Case 1
					Self.TurnTo(20,220,5)
				Case 2
					Self.TurnTo(620,220,5)
			End Select
		Endif
	End

	Method OnRender()
		SetColor(0,255,0)
		DrawOval (320,220,10,10)
		
		SetColor (0,0,255)
		DrawOval(Self.x,Self.y,10,10)
		
		SetColor(255,0,0)
		DrawOval (620,220,10,10)
		DrawOval (20,220,10,10)
	End


	Method TurnTo:Void(_x:Float, _y:Float, _step:Float)
		Local min:Float = -180;
		Local max:Float = 180;
		Local half:Float = 0
		Local retval:Float = 0.0;
		Local _start:Float = Self.angle
		Local _End:Float = ATan2(Self.x - _x, Self.y - _y)
		'Print "end = " + _End
		_End -= 180
		'Print "end = " + _End
		
	 	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
	
	   	Self.angle = retval
		Self.SetVelocities()
	End	

	Method SetVelocities:Void()
		Self.velx = Sin(Self.angle)
		Self.vely = Cos(Self.angle)
	End Method	

End

Function Main()

	New Test

End


thanks in advance, just not seeing the problem at the moment and its starting to really annoy me.


Phil7(Posted 2016) [#2]
In what direction is your angle value? Is it down for 0 degres and then turning counterclockwise? It's hard if you are thinking in other directions ;-)

I tried to change the x and y values for atan2 so you can uncomment _End -= 180 in the TurnTo Method:
		Local _End:Float = ATan2(_x - Self.x, _y - Self.y)
		'Print "end = " + _End
'		_End -= 180
		'Print "end = " + _End



Nobuyuki(Posted 2016) [#3]
2 lazy, I just rewrote it




Paul - Taiphoz(Posted 2016) [#4]
Nobuyuki that's deff better than mine but still results in a few dodgy results, so the perfect solution still eludes us.


Jesse(Posted 2016) [#5]
replace with this line:
		Local _End:Float = (ATan2(Self.x - _x, Self.y - _y)+360.0) Mod 360.0



Paul - Taiphoz(Posted 2016) [#6]
Solution right there.. thanks dude.. have a star, if the forum had a Solved button I would be smacking it right now .. thanks. lol been pulling my hair out over this all day.,


Phil7(Posted 2016) [#7]
Did you try my solution above? Just switching each argument in ATan2 and then deleting the following line ( _End -=180)


Paul - Taiphoz(Posted 2016) [#8]
oh mate I think I might have missed it , been binge watching chuck while i code so no clue if I did or didnt try it sorry. it very well may have solved the issue.