1 Reply Latest reply on Jan 18, 2007 4:29 AM by the real POTMO

# rotate a vector

Hi id like to rotate a vector by x degrees

right now I use
on rotateVector tVector, tAngle
trn = transform()
trn.position = duplicate(tVector)

if tAngle.ilk = #vector then -- for vectors
trn.rotate(-tAngle)
else
trn.rotate(vector(0,-tAngle,0)) -- single angle
end if

return trn.position
end

but i dont think thats the best way of rotating a vector.
Is it possible to rotate it with a rotation matrix or something.
• ###### 1. Re: rotate a vector
If you want anything done youll have to do it yourself :)

--!! args: vector Vector, float AngleDegree, #x/#y/#z aboutAxis
--!! returns: a new vector rotated
--!! note: aboutAxis is optional, #y is default

tNewVector = vector(0,0,0)
#x:
tNewVector.x = tVector.x
tNewVector.y = cos(tAngle) * tVector.y - sin(tAngle) * tVector.z
tNewVector.z = sin(tAngle) * tVector.y + cos(tAngle) * tVector.z
#y:
tNewVector.x = cos(tAngle) * tVector.x + sin(tAngle) * tVector.z
tNewVector.y = tVector.y
tNewVector.z = -sin(tAngle) * tVector.x + cos(tAngle) * tVector.z
#z:
tNewVector.x = cos(tAngle) * tVector.x - sin(tAngle) * tVector.y
tNewVector.y = sin(tAngle) * tVector.x + cos(tAngle) * tVector.y
tNewVector.z = tVector.z
end case

return tNewVector

end