Atan2 problem

BlitzMax Forums/BlitzMax Programming/Atan2 problem

Hi,

I have up to 200 enemies who need to rotate to point in the direction they are travelling.

This game is targetted to run on a P2-450 and above, therefore would Atan2 be very expensive?

Are there alternatives to finding the rotation based on 2 coordinates, or failing that, I am able to provide a movement vector vx,vy.

You could just normalize the 2d movement vector then rotate the object accordingly. I believe you can use sin and cosine on the normalized component to find the angle.

Ok here's something that might help. You store the x and y speeds in the vector and normalize to find the direction vector. Hope it helps.

Graphics 800,600,32,2
SetBuffer BackBuffer()

a.Vector2d = Vector2d(400,300)

x=400
y=300

Const tol# = 0.001

While Not KeyHit(1)
Cls

xspeed = 0
yspeed = 0

If KeyDown(200) Then yspeed = -2
If KeyDown(208) Then yspeed = 2
If KeyDown(203) Then xspeed = -2
If KeyDown(205) Then xspeed = 2

x = x + xspeed
y = y + yspeed
Rect x,y,10,10,1

a\x = xspeed
a\y = yspeed

Normalize2D(a.Vector2D)
ShowVector2D(a.Vector2d,10,10)

Oval 400 + (Sin(a\x)*500),300 + (Sin(a\y)*500),10,10,1

Flip
Wend

Type Vector2D
	Field x#
	Field y#
End Type

Function Vector2D.Vector2d(x#=0,y#=0)
	v.Vector2D = New Vector2D
	v\x = x
	v\y = y
	Return v
End Function

Function Normalize2D(v.Vector2D)
	mag#=Sqr(v\x * v\x + v\y * v\y)
	v\x = v\x / mag
	v\y = v\y / mag
	If (Abs(v\x) < tol) v\x = 0.0
	If (Abs(v\y) < tol) v\y = 0.0
End Function

Function ShowVector2D(v.Vector2D,x,y)
	Text x,y,"X= "+v\x
	Text x,y+20,"Y= "+v\y
End Function


Very helpful, thanks :)


And quite clever...

This game is targetted to run on a P2-450 and above, therefore would Atan2 be very expensive?
Nope.