Converts an Angle (in degrees) to a 2Dvector and a 2Dvector to and angle. If the Angle is in radians then you need to use the rad functions like sinrad() and cosrad(). The angle must be between 0 and 360
I think you might be looking for a 3D vector though.
Thanks to Phaelax for the vector routines
type vector2D
x as float
y as float
endtype
#constant PI=3.141592653589793238
/****************************************************************
Returns the length (or magnitude) of a 2D vector
****************************************************************/
function getVector2DLength(v1 as Vector2D)
l as float
l = v1.x*v1.x + v1.y*v1.y
l = sqrt(l)
endfunction l
/****************************************************************
Returns a normalized 2D vector (or unit vector) of v1
****************************************************************/
function normalizeVector2D(v1 as Vector2D)
d as float
v as Vector2D
d = getVector2DLength(v1)
v.x = v1.x / d
v.y = v1.y / d
endfunction v
/****************************************************************
Converts an angle to a 2D vector
****************************************************************/
function AngleToVector2D(angle as float)
vector as vector2D
vector.x = cos(angle)
vector.y = sin(angle)
vector = normalizeVector2D(vector)
endfunction vector
/****************************************************************
Converts a 2D vector to an angle
****************************************************************/
function Vector2DToAngle(vector as vector2D)
degrees as float
radians as float
radians = Atan2(vector.X, -vector.Y)
degrees = radians * 180 / PI
endfunction degrees
Sign up for NaGaCreMo!