Help me optimize this code

BlitzPlus Forums/BlitzPlus Programming/Help me optimize this code

siread(Posted 2005) [#1]
The following is my adaptation of the ATan2 demo in the blitz+ docs. Basically it represents a computer controlled player chasing a ball, always following the shortest route. I think i could use a lookup table for the Cos and Sin calculations but what about the SquareRoot and ATan2 calculations. Is there a better/quicker way to do this?

; ATan2 example.
; Move mouse. Escape quits.

Const width = 640, height = 480
Const KEY_ESC = 1

Graphics width, height
SetBuffer BackBuffer( )
HidePointer
MoveMouse .75 * width, height / 2

playerx# = 320
playery# = 240
speed# = 2.0

While Not KeyDown( KEY_ESC )

ballx# = MouseX()
bally# = MouseY()

Cls
Color 0, 0, 255		Oval playerx - 3, playery - 3, 7, 7, True		; Draw Player
Color 255, 255, 255	Oval ballx  - 3, bally - 3, 7, 7, True			; Draw Ball
Color 255,255,0		Line playerx, playery, ballx, bally			; I Draw The Line Here

a# = ballx-playerx
b# = bally-playery
distance# = Sqr(a*a + b*b)
angle# = ATan2( bally - playery, ballx - playerx )

Color 255, 255, 255		Text 10,10, "Ball x = " + ballx
Color 255, 255, 255		Text 10,20, "Ball y = " + bally
Color 255,255,0			Text 10,30, "ATan2( y, x ) = " + angle
Color 255,255,0			Text 10,40, "Distance = " + distance

xplus# = Cos(angle) * speed
yplus# = Sin(angle) * speed

Text 10,50, "Move x = "+xplus
Text 10,60, "Move y = "+yplus

playerx = playerx + xplus
playery = playery + yplus

Flip
Wend

End



Ace Grace(Posted 2005) [#2]
You are definitely on the right track.

Lookup tables where possible will save you loads of time.


Regular K(Posted 2005) [#3]
do some tests, apparently sqr is very optimized:
[CODE]
start=MilliSecs()
a=323
b=73
For i%=0 To 1000000
Sqr(a*a + b*b)
Next
Print MilliSecs()-start
Repeat
Forever
[/CODE]

and also atan2 is pretty good

(at least for me, using BP)


Beaker(Posted 2005) [#4]
You can avoid Sin, Cos and Atan2 completely:
Const width = 640, height = 480
Const KEY_ESC = 1

Graphics width, height
SetBuffer BackBuffer( )
HidePointer
MoveMouse .75 * width, height / 2

playerx# = 320
playery# = 240
speed# = 2.0

While Not KeyDown( KEY_ESC )

ballx# = MouseX()
bally# = MouseY()

Cls
Color 0, 0, 255		Oval playerx - 3, playery - 3, 7, 7, True		; Draw Player
Color 255, 255, 255	Oval ballx  - 3, bally - 3, 7, 7, True			; Draw Ball
Color 255,255,0		Line playerx, playery, ballx, bally			; I Draw The Line Here

a# = ballx-playerx
b# = bally-playery
distance# = Sqr(a*a + b*b)

Color 255, 255, 255		Text 10,10, "Ball x = " + ballx
Color 255, 255, 255		Text 10,20, "Ball y = " + bally
Color 255,255,0			Text 10,40, "Distance = " + distance

xplus# = a * (speed / distance)
yplus# = b * (speed / distance)

Text 10,50, "Move x = "+xplus
Text 10,60, "Move y = "+yplus

playerx = playerx + xplus
playery = playery + yplus

Flip
Wend

End



siread(Posted 2005) [#5]
Thanks guys, that's very helpful.

BTW Beaker. FONText is superb. :)


Grey Alien(Posted 2005) [#6]
I made lookup tables for sin (with an Array) it it really was not noticebly faster. Can this be right?


WolRon(Posted 2005) [#7]
CPU's are so fast now-a-days that look-up tables aren't as advantageous as they used to be.


Grey Alien(Posted 2005) [#8]
Lookup tables used to be vital, but chips must be pretty well optimised for floating point and trigonomety now I guess. btw CPUs has no apostrophe, nor does PCs or CDs or DVDs or 1980s not a lot of people know that ;-)