问题
I have a quaternion that contains the rotation of the three axes (x, y, z) at the same time.
I want to convert this quaternion to a rotation matrix but only the rotation on the Y axis of the quaternion or of any of the other axes, without all three at the same time.
回答1:
A possible route:
- Transform unit vectors X=(1,0,0) and Z=(0,0,1) by the quaternion
- Call these rotated vectors (x0,x1,x2) and (z0,z1,z2)
- If the rotation would have been purely around Y, we would have:
- (x0,x1,x2) = (cos(theta), 0, sin(theta))
- (z0,z1,z2) = (-sin(theta), 0, cos(theta))
- not used is (y0,y1,y2) = (0, 1, 0)
- so, calculate
- c = (x0+z2) / 2
- and s = (x2-z0) / 2
- then normalize to get c2 + s2 equal to 1
- norm = sqrt(c * c + s * s)
- if norm != 0:
- c = c / norm
- s = s / norm
- (if the norm would be zero, there is not much we can do)
- the angle would be atan2(c, s)
- the rotation matrix would be [[c,0,-s],[0,1,0],[s,0,c]]
来源:https://stackoverflow.com/questions/58946202/quaternion-to-matrix-rotation-only-one-axis