Model rotation vector computation

Hi, I was playing with rotating models within code, and I would like to clarify whether I understand it correctly. E.g. I do have model that has to be rotated by Y and Z. As Rotation is Vector4, is it correct way to set averages of elements for Y as Y / (Y + Z) and for Z as Z / (Y + Z) and angle as Y + Z, for example like,
model.Rotation := Vector4(0, RotY / (RotY + RotZ), rotZ / (rotY + rotZ), rotY + rotZ);
or should there by any other calculation to rotate by Y and Z at once? Thanks.

Rotations in CGE are expressed as 4D vectors “axis+angle”, i.e. the first 3 floats are the axis around which to rotate, the last float is the angle in radians.

To combine 2 rotations, the general way is to use quaternions. Like this:

  Q, Q1, Q2: TQuaternion;
  Q1 := QuatFromAxisAngle(Vector3(0, 1, 0), 0.123);
  Q2 := QuatFromAxisAngle(Vector3(0, 0, 1), 0.456);
  Q := Q1 * Q2;
  MyTransform.Rotation := Q.ToAxisAngle;

This is a general solution that I would advise to use, as it is also readable in code. This way it is clear you combine two rotations, you can control the order of the combination, add more rotations etc. easily too. Maybe in some cases it is similar to averaging the axes, maybe not, we don’t need to think about it, let quaternions math do the work :slight_smile:

Alternative, also good, solution is to always make “deeper” hierarchy. It is OK to have one TCastleTransform inside another TCastleTransform inside another TCastleTransform just to be able to control them in more straightforward ways. The transformations using TCastleTransform are by themselves quite lightweight.

1 Like

Oh, all right, I did not think of quaternions, maybe just a suggestion to add it as an example to API documentation. Thanks for explanation.

Good point, I added a note to TCastleTransform.Rotation API docs about the quaternions, using my example code above.

1 Like