Move from point A to B: best practice

Monkey Forums/Monkey Programming/Move from point A to B: best practice

mteo77(Posted 2013) [#1]
Hello all, it's still me.
I am writing code to make objects move in different ways, and one of them is from A to B.
I have this code:


Is it the best way to do it?
Problem is since i am going to use this code to move little bullets, and i know sin and cos and atan2 are cpu intensive,is there another way to do it?
I can't use arrays because the position is changed at update time based on where i am.


GfK(Posted 2013) [#2]
If the bullet has a constant direction and speed, you can calculate an xStep and yStep from sin/cos and use that to move the bullet. That way you only need to use sin and cos once.


Jesse(Posted 2013) [#3]
this should be more efficiently(same as what GFK said):
Strict

Class AtoB
	Field x:Float
	Field y:Float
	Field x1:Float
	Field y1:Float
	Field x2:Float
	Field y2:Float
	Field speed:Float
	Field angle:Float
	Field vx:Float
	Field vy:Float
	Method init()
		x1 = 100.0			'A position X
		y1 = 100.0			'A position Y
		x2 = 200.0				'B position X
		y2 = 400.0				'B position Y
		x = 0.0				'bullet position X
		y = 0.0				'bullet position Y
		speed = 6.0			'bullet speed
		angle = 0.0			'angle
		angle = ATan2( (y2 - y1), (x2 - x1))	'do it only one time in the setup
		vx = Cos(angle) * speed 'do it only one time in the setup  ******************************************
		vy = Sin(angle) * speed 'do it only one time in the setup  ******************************************
	End Method
	Method update()
		x = x + vx '**********************************************
		y = y + vy '**********************************************
	End Method
	
	Method render()
		DrawCircle x, y, 40
	End Method
End Class



mteo77(Posted 2013) [#4]
Oh yes haven't thought about that!
Also i am wondering if i can store the results in an array, since i am going to use same direction everytime, so i can do the calculation during setup, and use the array with fixed results, so i can create my classes during run time with the fixed direction thats been done during setup,if it make any sense.
Trying it now.
Thanks for the help!


mteo77(Posted 2013) [#5]
Hello.
I tried to put in an array the results and it worked wonderfully.
But now i have encountered another problem, i tried to port over some of the code i had ages ago in blitz3d over monkey, and while in blitz 3d is very fast (although not optimised) in monkey is really slow!


Does anybody have an idea why the ball is moving in steps and not smooth?
Is it because the functions i am using are cpu intensive or because i am using the wrong ones(tried to copy the code from blitz3d)?


Jesse(Posted 2013) [#6]
I think there is something you have to understand about Monkey. Monkey produces code that is native to the specific platform. That is, if you compile for html5 it will produce javascript and then the code is run by the native platform in this case the browser. now the javascript code will run at about the same speed whether it is produced by Monkey or you code it your self. so I hope you understand that it's not monkey that is slow but the target. try the code in Flash or GLFW and you will know what I mean.


Jesse(Posted 2013) [#7]
.


AdamRedwoods(Posted 2013) [#8]
HTML5 DrawText is slow. remove those lines and it's fast again.


muddy_shoes(Posted 2013) [#9]
It's more to do with the SetColor before the text drawing. DrawText is just a bunch of image renders and colour filtering of images on the HTML5 target is slow.


mteo77(Posted 2013) [#10]
Oh didn't know that SetColor was going to slow everything to a crawl.
Taking it off solved the problem.
Wasn't sure if it was because of my usage of those instructions or if i was missing something (in this case target specific commands).
Thanks for pointing it out.
Hopefully i can use that function to move waves of objects.
Trying it now.


mteo77(Posted 2013) [#11]
Hello. I am trying to modify the move code to make the object stop when it reach destination and return a value.
I can't find a way to do it, every solution i have tried it doesnt work in a way or another.
The move code is:
	Method MoveTowards2:Bool(_x1, _y1, _x2, _y2)
		x1 = _x1				'start X
		y1 = _y1				'start Y
		x2 = _x2				'end X
		y2 = _y2				'end Y
		angle = ATan2( (y2 - y1), (x2 - x1))
		xstep = Cos(angle) * speed
		ystep = Sin(angle) * speed
		posx = posx + xstep
		posy = posy + ystep
		If posx > x2 Then posx = x2'not working!
		If posy > y2 Then posy = y2'not working!
	End Method

As you can see the last 2 statements should stop the movement...but it doesnt work for example if the destination is already lesser than the start.
Also i cant figure out how to return true once the method is finished (when object has reached destination).
Any suggestion?
Thanks.


Jesse(Posted 2013) [#12]
keep track of the starting position, figure out the distance from the starting position to the destination. if the distanceTraveld >= destinationDistance set distanceTraveld = destinationDistance and return true else return false (or visa-versa which ever works for you) .


mteo77(Posted 2013) [#13]
Oh haven't thought about comparing the distances!
That means a couple of sqrt computations extra...
Modified code here:
	Method MoveTowards2:Bool(_x1, _y1, _x2, _y2)
		x1 = _x1				'start X
		y1 = _y1				'start Y
		x2 = _x2				'end X
		y2 = _y2				'end Y
		angle = ATan2( (y2 - y1), (x2 - x1))
		xstep = Cos(angle) * speed
		ystep = Sin(angle) * speed
		posx = posx + xstep
		posy = posy + ystep
		'If posx > x2 Then posx = x2	'not working
		'If posy > y2 Then posy = y2	'not working
		TotDistance = Sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))				'distance between start and end
		ActDistance = Sqrt( (posx - x1) * (posx - x1) + (posy - y1) * (posy - y1))		'distance between start and actual
		If ActDistance >= TotDistance		'if actual distance is equal or greater than total distance then  reset the actual pos to the target pos
			posx = x2
			posy = y2
		EndIf
	End Method

Thank you very much for the help.
Sadly those computations have to be done realtime as part of a proactive kind of movement, so can't really save them in a array.


Jesse(Posted 2013) [#14]
but you don't have to do square root. keep the distance squared.

	Method MoveTowards2:Bool(_x1, _y1, _x2, _y2)
		x1 = _x1				'start X
		y1 = _y1				'start Y
		x2 = _x2				'end X
		y2 = _y2				'end Y
		angle = ATan2( (y2 - y1), (x2 - x1))
		xstep = Cos(angle) * speed
		ystep = Sin(angle) * speed
		posx = posx + xstep
		posy = posy + ystep
		'If posx > x2 Then posx = x2	'not working
		'If posy > y2 Then posy = y2	'not working
		TotDistance = ( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))				'distance between start and end
		ActDistance = ( (posx - x1) * (posx - x1) + (posy - y1) * (posy - y1))		'distance between start and actual
		If ActDistance >= TotDistance		'if actual distance is equal or greater than total distance then  reset the actual pos to the target pos
			posx = x2
			posy = y2
		EndIf
	End Method



mteo77(Posted 2013) [#15]
*thump* (head banging noise)
I think i am overdoing the calculations atm...
Again thanks for pointing it out!