I posted a similar question on the programming board, but I thought it might be more fitting here;

I'm trying to gradually rotate a vector back to some initial angle w.r.t the Z axis after having rotated it 90 degrees around the Y-axis. Here is my current method:

At the start I have a vector that is rotating around the z-axis at an angle of around 24 degrees, example vector would be:[0.2,0.4,1], which is then multiplied by a rotation matrix of 90 degrees around the y-axis:

$ \begin{pmatrix} 0.2 \\ 0.4 \\ 1 \end{pmatrix} % \begin{pmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \\ \end{pmatrix} =% \begin{pmatrix} 1 \\ 0.4 \\ -0.2 \end{pmatrix} $

Now I would like to slowly return that vector (in N amount of steps), while rotating around the z-axis, to the same angle the original vector was in (the 24 degrees w.r.t. to the z-axis). This would create a sort of spiral motion, with the angle towards the z-axis becoming smaller until it hits 24 degrees.

What I tried to do was calculate the rotation matrix for the (-90 / N) angle around the y-axis, and apply it N times (effectively doing a 90 degree rotation back), while also each time rotating around the z-axis. For N=10 would look something like this:

$ \overrightarrow{v} % \begin{pmatrix} 0.989 & 0 & -0.1564 \\ 0 & 1 & 0 \\ 0.1564 & 0 & 0.989 \\ \end{pmatrix} % \begin{pmatrix} 0.866 & -0.5 & 0 \\ 0.5 & 0.866 & 0 \\ 0 & 0 & 1 \\ \end{pmatrix} = % \overrightarrow{v} $

Which is a -9 degree rotation matrix around Y, and a 30 degree rotation around Z. And apply this iteratively 10 times. This approach works if there is no rotation around the z-axis going on, but because of the extra rotation, simply doing this doesn't work.

In the end I would like to have a vector with the same magnitude, rotating at the same angle from Z (24 degrees) as the starting vector. Basically, rotating in the same circumference as the starting vector. In my understanding this would mean that the Z component of the vector has to be 1 since that's what it was for the starting vector. I am not sure how to account for the extra rotation around the Z-axis however, any help would be greatly appreciated.

Edit: some code to illustrate

import math
from scipy.spatial.transform import Rotation
import numpy as np

# Starting vector
v = [0.2,0.4,1.]

for i in range(16):
    # Rotate around Z axis
    axis = np.array([0.,0.,1.])
    theta = 1
    rot = Rotation.from_rotvec(theta * axis)
    v = rot.apply(v)     
    # After 5 time steps, rotate 90 degrees around the y-axis
    if i == 5:
        axis = np.array([0.,1.,0.])
        theta = math.radians(90)
        rot = Rotation.from_rotvec(theta * axis)
        v = rot.apply(v)    
    # Start rotating back 'up'
    elif i > 5:
        axis = np.array([0.,1.,0.])
        # -9 degrees because you rotated 90 degrees, now do -9 10x backwards
        theta = math.radians(-9)
        rot = Rotation.from_rotvec(theta * axis)
        v = rot.apply(v)     

1 Answer 1


I think you are overthinking this.

You know how you can rotate a vector around the z-axis, parameterized by an angle. What you get is a circle in the xy-plane with the radius determined by the components of the initial vector and the center is at (0,0,z) where z is the z-component of the initial vector.

You can further parametrize the radius of the circle by an angle if you want other shapes then circles.

And you can furthermore parameterize the height of the shape by letting the z-component vary with the angle.

With these 3 transformations you get pretty much any spiral shape around the z-axis you would want

Does this answer your question or do you really want the transformation you are describing because then you need to clear up your question, I at least cannot make heads and tails of it

EDIT: after the rotation around y-axis there is a new z-component, let's call the old value $z$ and the new one $z'$

Since the z-component won't change anymore via the rotation around the z-axis, you can simply solve for the rotation back around the y-axis, let's call it $Y$. Since $Y$ is only dependent on 2 values $c$ and $s$, you only have to solve this system of equations

$$c^2+s^2=1\\ z= -sx'+ cz'$$

where the last equation results from simply comparing the components before and after the multiplication with $Y$

with $c$ and $s$, you can solve for the angle (the letters stand for cos and sin) and then use your angle subdivision method

you have to keep in mind though that you need to keep track then of how the y-axis gets rotated along with the vector and rotate around the rotated y-axis, not the regular y-axis

  • $\begingroup$ I'll try to clear up the question; The vector is rotating around the z-axis, and then flipped 90 degrees along the y-axis while continually rotating around z, like so: link. After this I want to rotate the vector back 'up' to the starting circle in the xy-plane as you described. I don't have an animation for this because I don't know how to do this :P, but it would create a spiral motion of the vector. So my question is, how do I combine both the Z rotation and the XY-rotation to go back to my original circumference? $\endgroup$
    – Tr33hugger
    Jan 29, 2023 at 13:20
  • $\begingroup$ Ok, so if I understood correctly, you just need to multiply by the inverse of the rotation around the y-axis $\endgroup$ Jan 29, 2023 at 13:41
  • $\begingroup$ But that wouldn't account for the rotation around the z-axis taking place. The vector is constantly rotating around the z-axis. While that is happening, after the 90 degree rotation around Y, I would like to gradually rotate it back into the same circumference as it was before the 90 degree rotation. If there was no rotation around Z this would be easy, simply rotate -90 degrees around the Y-axis, but that doesn't work because of the rotation around Z. I edited my post to include some (python) code that could maybe clear it up. $\endgroup$
    – Tr33hugger
    Jan 29, 2023 at 14:11
  • $\begingroup$ yeah I only want to end up along the same circle, but simple rotating back AFTER also rotating in another direction doesn't work; numerically: [0.2,0.4,1.] starting vector, rotate it 90 degrees around Y -> [1,0.4,-0.2], then after some random rotation around Z -> [-0.779, 0.742, -0.2], if you now do the inverse 90 degree rotation across the y-axis, you'll end up with [0.528, 0.743, -0.607], which is not in the same circle as the original vector. I think the problem is I have to rotate around both X and Y, but not sure how much around each. $\endgroup$
    – Tr33hugger
    Jan 29, 2023 at 15:06
  • $\begingroup$ First of all, I want to apologize, for some reason I thought tbose rotations commute, but you are right, they don't . I have updated my answer, I think that should be the solution $\endgroup$ Jan 29, 2023 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .