• 3D
  • Quaternion Rotation

I'm trying to do some rotations around a certain axis in Godot 3D and I ran into a problem.

I have the following variables: t is the local frame of the character and rot is rotation of PI * 3 / 8.0 about t.basis.x, so far so good...

var t = self.get_transform() var rot = Quat(t.basis.x, PI * 3 / 8.0)

The problem is that when I print t.basis.x and Basis(rot).x I get different vectors: t.basis.x = (0.578292, 0.775742, -0.252599) Basis(rot).x = (0.589126, 0.04356, -0.806866)

But if I'm rotating about t.basis.x, then shouldn't the x-basis of the rotation quaternion be identical to t.basis.x? Perhaps I'm missing something super simple here.

Maybe that's why you aren't supposed to use 3X3 matrices to do rotations.

Could you elaborate on your answer? I searched the docs and they say that using a 3-d basis for a transform is fine.

I tried using tranform.rotate(axis, angle) and it worked. Now I'm wondering what's the difference between the following 2 approaches:

1) Define rot = Quat(axis, angle) set transform to be Transform(rot, origin) (this doesn't work)

2) Directly do tranform.rotate(axis, angle) (works fine)

I don't think I should be experiencing gimbal lock though since I am using quaternions. As far as I know that's a problem with Euler angles.

Maybe I should check out the source code for these classes.

One thing with Godot rotation to note is that if you get an angle over 180 degrees and you have rotation on more than one axis, Godot will sometimes flip other axes to -180 rather than going past 180 on the axis you are rotating, if that makes sense. It can make calculating angles rather difficult :neutral:

You can also get the current Basis rotation as a quaternion using global_transform.basis.get_rotation_quat, which may be helpful for certain operations as then you can use quaternion math to rotate it. To apply the result back to a Basis, you can use something like global_transform.basis = Basis(quat_here).scaled(global_transform.basis.scale). The reason we need to scale it is because a Basis made from a quaternion doesn't include any scaling data, so if your node is scaled it will suddenly be reset to a scale of 1 if you don't scale the Basis from the quaternion.