You have to take into account the following:
- In your case,
CMRotationMatrix
needs to be transposed (http://en.wikipedia.org/wiki/Transpose) which means swapping columns and rows. - You don't need to set the transform as
CATransform3DIdentity
, because you're overwriting each value, so you can start with an empty matrix. If you want to useCATransform3DIdentity
you can omit setting 0s and 1s since they've already been defined. (CATransform3DIdentity
is an identity matrix, see http://en.wikipedia.org/wiki/Identity_matrix) - To also correct the rotation around the Y axis, you need to multiply your vector with [1 0 0 0; 0 -1 0 0; 0 0 1 0; 0 0 0 1].
Make the following changes to your code:
CMRotationMatrix r = attitude.rotationMatrix;
CATransform3D t;
t.m11=r.m11; t.m12=r.m21; t.m13=r.m31; t.m14=0;
t.m21=r.m12; t.m22=r.m22; t.m23=r.m32; t.m24=0;
t.m31=r.m13; t.m32=r.m23; t.m33=r.m33; t.m34=0;
t.m41=0; t.m42=0; t.m43=0; t.m44=1;
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 1.0 / -650;
t = CATransform3DConcat(t, perspectiveTransform);
t = CATransform3DConcat(t, CATransform3DMakeScale(1.0, -1.0, 1.0));
Or, with setting t to CATransform3DIdentity
just leave the 0s and 1s out:
...
CATransform3D t = CATransform3DIdentity;
t.m11=r.m11; t.m12=r.m21; t.m13=r.m31;
t.m21=r.m12; t.m22=r.m22; t.m23=r.m32;
t.m31=r.m13; t.m32=r.m23; t.m33=r.m33;
....