I'm currently working on a program that requires specifying a quaternion rotation to point a cylinder in the direction of its velocity vector, i.e. projecting the $z$-axis onto that velocity vector. Following this answer, I attempted the following, where $v$ is the velocity vector:
- Take $\mathbf{v} = (0,0,1)$ and $\mathbf{w}=\frac{v}{\|v\|}$.
- Determine the axis $\mathbf{a} = \mathbf{v} \times \mathbf{w}$ and $\theta = \arccos(\mathbf{v} \cdot \mathbf{w})$
- Convert to a quaternion in the usual way, i.e. $\mathbf{q} = (\cos(\theta/2),\mathbf{a}\sin(\theta/2))$
This, however, produced rotated cylinders which are slightly off from the lines they follow (except if the velocity vector is the x or y-axis, in which case it works perfectly). I know it is an issue with the method and not my code, as I've been able to reproduce the issue in Mathematica:
vel = {0.00113606,0.01807943,0.02356922};
z ={0,0,1};
axis = Cross[Normalize@z,Normalize@vel];
theta = ArcCos[(Normalize@z).(Normalize@vel)];
c = Cos[theta/2];
s = Sin[theta/2];
q = Normalize@{c, axis[[1]]*s, axis[[2]]*s, axis[[3]]*s};
rm = RotationMatrix[2 ArcCos[First@q], Rest@q];
Show[{ParametricPlot3D[vel* u, {u, -200, 200},PlotRange->{{-5,5},{-5,5},{-5,5}}],Graphics3D[{
GeometricTransformation[Cylinder[], rm]}]},Axes->True]
This is the exact same misalignment I see in my program. Am I missing some concept for how to align these two vectors? Is it not as simple as making sure the up-axis is projected onto the velocity vector? Any help is appreciated.
