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