How do I rotate a vector around the Z axis?

MAXScript FAQ > Working With Vectors > How do I rotate a vector around the Z axis?

A user asked:

I am trying to rotate a unit vector around 360 degrees in steps of 2 degrees. It only needs to rotate around the Z axis, X and Y stay the same.

Answer:

The Matrix3 Values topic provides convenient ways to transform vectors (Point3 values) including rotation about the world Z axis! You can simply construct a rotate Z Matrix value and transform the original vector using it:

SCRIPT

resetMaxFile #noPrompt --reset the file
theV = normalize [5,0,10] --the vector to rotate, normalized to unit vector
theStep = 10 --the step by which you want to rotate --rotate from 0 to 360-theStep with step TheStep:
for a = 0 to 360-theStep by theStep do
(
  rm = rotateZMatrix a --create a rotation matrix from the value
  theRotV = theV * rm --transform the original vector using the matrix
  format "%: %\n" a theRotV --print the result to Listener
  c = cylinder() --create a cylinder
  c.dir = theRotV --orient along the vector to see what it looks like
)

The results looks like a muffin!

See Also