function to rotate a vector

Blitz3D Forums/Blitz3D Programming/function to rotate a vector

Craig H. Nisbet(Posted 2008) [#1]
Anyone got a function to rotate a vector?

Here's the function I used and it's returning totally screwy results.

x = (x * Cos(angle)) - (y * Sin(angle))
y = (y * Cos(angle)) + (x * Sin(angle))


nawi(Posted 2008) [#2]
r = sqrt(x*x+y*y)
newangle = acos(x/r)+angle
x = r*cos(newangle)
y = r*sin(newangle)

No idea if this works, just something I came up with


Floyd(Posted 2008) [#3]
The reason the first code fails is that x changes before it is used to calculate y.
You would need something like this:

tempx = x * Cos(angle) - y * Sin(angle)
y = y * Cos(angle) + x * Sin(angle)
x = tempx