-
-
Save markjlorenz/8199773 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CMRotationMatrix rotationMatrixFromGravity(float x, float y, float z) | |
{ | |
// The Z axis of our rotated frame is opposite gravity | |
vec3f_t zAxis = vec3f_normalize(vec3f_init(-x, -y, -z)); | |
// The Y axis of our rotated frame is an arbitrary vector perpendicular to gravity | |
// Note that this convention will have problems as zAxis.x approaches +/-1 since the magnitude of | |
// [0, zAxis.z, -zAxis.y] will approach 0 | |
vec3f_t yAxis = vec3f_normalize(vec3f_init(0, zAxis.z, -zAxis.y)); | |
// The X axis is just the cross product of Y and Z | |
vec3f_t xAxis = vec3f_crossProduct(yAxis, zAxis); | |
// each array is a row | |
CMRotationMatrix mat = { | |
[ xAxis.x, xAxis.y, xAxis.z ], | |
[ yAxis.x, yAxis.y, yAxis.z ], | |
[ zAxis.x, zAxis.y, zAxis.z ] }; | |
return mat; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Applying a rotation. Useful if the sensor rotates in place without translating. Requires gyros and accelerometers:
Yay! it works!