Apparent-gravity camera frame (source code)

= Apparent-gravity camera frame

If $g$ is gravitational <acceleration> and $a$ is the camera's <acceleration>, the locally felt upward vector is $U=a-g$. Choose the travel <unit vector> $X$, set $W=U-(U\cdot X)X$, and, when $W\ne0$, take $Z=W/\|W\|$ and $Y=Z\times X$. These form an <orthonormal basis> with $U$ in the $XZ$ plane. If $U$ is parallel to $X$, the condition leaves the roll angle free even when $U\ne0$; transport a previous transverse axis or choose a well-conditioned coordinate axis and project it into $X^\perp$.