I’ve been researching quaternions and I’ve been wondering why godot uses transforms with basis to handle rotation. Unity and Unreal both use quaternions to handle rotation (and separate vectors for scale). I know that a basis can also do shear, which is something a quaternion and scale can’t, but as far as I know, shearing is rarely used. Applying multiple rotations with a basis requires matrix multiplication which is expensive and can cause floating point errors requiring a renormalization which removes all scale. With quaternions, applying multiple rotations is slightly cheaper and renormalization doesn’t affect scale because scale is completely separate. Interpolation is done with quaternions exclusively, which godot requires a conversion from and back into a basis.
So my question is what are the reasons godot chose basis for its Transform3D?
1 Like
Basis is just a 3x3 subset of the standard 4x4 transformation matrix, used almost everywhere. Transform3D represents that matrix.
In general, when using quaternions in 3D graphics, a conversion to a matrix will almost always happen in the pipeline, as that’s what shaders use to do transformations. All quaternions that affect rendering will eventually need to end up in the basis part of the 4x4 matrix.
Internally in the engine code, quaternions are likely used for calculations whenever it makes sense.
Godot’s Node3D has the quaternion property, so you can use that directly without interfering with scale.
7 Likes
Quaternions are cheap to combine but expensive to apply, transforms in game engines are used significantly more often for the latter than for the former
Combining two quaternions takes 16 multiplications, and combining basis matrices takes 27, but applying a quaternion to a vector takes 15 multiplications where a rotation matrix takes 9, if we use a quaternion plus scale this gets even worse
Edit: also combining two quaternions plus scale must necessarily be more expensive than combining two matrices, unless there’s some shortcut, naive solution would be 16+15 multiplications, which, as normalized says above, means they almost certainly represent the data as a matrix internally
3 Likes
Can you produce a camera’s perspective matrix with only quaternions? If not that’s already one required mat4 operation for every 3D object in the game. I’d be very surprised if those other engines did not store the matrix representations internally.
3 Likes
They almost certainly store the matrix and convert at every update.
Godot doesn’t include quaternion interface in Transform3D object, probably to keep it lightweight, But it provides it for Node3D. I think that from user perspective as well as from performance perspective, there’s no much difference compared to other mentioned engines.
2 Likes
Basis exposes Quaternion conversions, though you can’t set the quaternion part directly you can extract it and create a Basis from a quaternion
So you can do Transform3D(Basis(my_quaternion).scaled(my_scale), my_translation) for example
1 Like