Atan2 problem

BlitzMax Forums/BlitzMax Programming/Atan2 problem

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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