Search code examples
iosmacoscatransform3d

Understanding CATransform3D rotations around all three axis


I want to rotate a view around the three axis, with 3 different angles : rx, ry, rz.

Here's what I done :

_tx = CATransform3DMakeRotation(rx*M_PI/360, 1, 0, 0);
_ty = CATransform3DMakeRotation(ry*M_PI/360, 0, 1, 0);
_tz = CATransform3DMakeRotation(rz*M_PI/360, 0, 0, 1);
view.layer.transform = CATransform3DConcat(_tx, CATransform3DConcat(_ty, _tz));

I though it was working when I tried to invert the concatenation order. For example,

view.layer.transform = CATransform3DConcat(_tx, CATransform3DConcat(_ty, _tz));

and

view.layer.transform = CATransform3DConcat(_tz, CATransform3DConcat(_tx, _ty));

Do not give the same result !!

  1. Is there a way to rotate around the 3 axis with one CATransform3DMakeRotation?
  2. Or a better way to do this?

Solution

  • Matrix operations are not commutative, changing the order in which you multiply them together will give you different resulting transform matrices.

    In which order do you want to apply your 3 different rotations? That is the order that you should concatenate your transform matrices.