For the game I am working on I needed to do some 2D vector math, so I wrote a small script/library and figured I would share it with anyone that wants it. Please report any bugs. For anyone that has worked with vectors before it should be self explanatory.
type Vector2
X as float
Y as float
endtype
function Vector2_Add(v1 as Vector2, v2 as Vector2)
r as Vector2
r.X = v1.X + v2.Y
r.Y = v1.Y + v2.Y
endfunction r
function Vector2_Subtract(v1 as Vector2, v2 as Vector2)
r as Vector2
r.X = v1.X - v2.X
r.Y = v1.Y - v2.Y
endfunction r
function Vector2_Scale(v1 as Vector2, s as float)
r as Vector2
r.X = v1.X * s
r.Y = v1.Y * s
endfunction r
function Vector2_Dot(v1 as Vector2, v2 as Vector2)
r as float = 0.0
r = (v1.X * v2.X) + (v1.Y * v2.Y)
endfunction r
function Vector2_Distance(v1 as Vector2, v2 as Vector2)
r as float = 0.0
r = Sqrt(Pow(v1.X - v2.X, 2) + Pow(v1.Y - v2.Y, 2))
endfunction r
function Vector2_Angle(v1 as Vector2, v2 as Vector2)
r as float = 0.0
r = ATan2(v2.Y - v1.Y, v2.X - v2.X)
endfunction r
function Vector2_Magnitude(v1 as Vector2)
r as float = 0.0
r = Sqrt(v1.X * v1.X + v1.Y * v1.Y)
endfunction r
function Vector2_Normalized(v1 as Vector2)
r as Vector2
r.X = v1.X / Vector2_Magnitude(v1)
r.Y = v1.Y / Vector2_Magnitude(v1)
endfunction
If you have any questions pleas ask!