Created
April 17, 2012 21:16
-
-
Save sinc/2409107 to your computer and use it in GitHub Desktop.
get rotation matrix from accelerometer
This file contains hidden or 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); | |
CMRotationMatrix mat = { | |
xAxis.x, yAxis.x, zAxis.x, | |
xAxis.y, yAxis.y, zAxis.y, | |
xAxis.z, yAxis.z, zAxis.z}; | |
return mat; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Here's some octave code that I used to prove to myself that it works:
Conclusion: It works!