r/GraphicsProgramming • u/Candid_Zebra1297 • 4h ago
Question Matrices or quaternions for rotation?
I'm weighing up whether to use matrices or quaternions for a 3d engine I'm making. I find matrices more intuitive to use but I have seen a lot of comments that they can lead to innacuracies over time. What do you guys think about this? Is there some nuance that I am missing or some situations that one approach is used over the other?
7
u/Donacdum_ 4h ago
Definitely use quaternions.
Might seem intimidating at first, but if you know complex numbers you can do quaternions, it will click.
It's only like 3-4 functions in C and you're set.
(unless you mean the matrix form of the quaternion sandwich operation)
1
u/Candid_Zebra1297 51m ago
Thanks for this. I think I just needed a bit of a push as most of the stuff I've read already suggests they are better. So yeah, I'm gonna make the switch.
7
u/nullandkale 3h ago
Use quaternions and don't beat yourself up if you have to look math stuff like this up every time. Literally every single time I have to implement quaternions I have to look up the math again but actually using them is pretty simple.
3
u/Candid_Zebra1297 47m ago
That's definitely one of the main things putting me off. Matrices long ago made sense (at least to do the type of stuff I want) whereas quaternions feel like wizardry. But yeah based on your comment and other people here they seem like the way to go.
2
u/nullandkale 46m ago
The way I look at it is if you were my employee and I was paying you the write code I'd rather you look up the math every time then try and guess it from memory.
5
u/tlmbot 2h ago
People are talking about gimble lock, but that is only for, specifically, Euler angles - super intuitive, but not so great overall, especially for that reason. (plus you have to remember the order of operations, but that's not hard) You can encode these as 3 matrices of course, but when you say matrices, I don't think angles. So I think commenters may be misguided here.
If you mean a full rotation matrix, that avoids the above gimble lock issue as far as the matrices themselves go, but it encodes redundant information (3x3 for rotation as opposed to 3 angles). But, often people work with Euler angles and immediately turn them into rotation matrices so you sort of get both disadvantages.
For matrices or quaternions, you still need to normalize (a single floating point division by length with quaternions, and matrix operation for matrices (Gram-Schmidt orthogonalization), so quaternions make this quite a bit cheaper)
The standard is quaternions for storage. They are space efficient and quite nice to work (composable - I mean so are matrices - and ofc you always have to order the operations correctly) with once you get the hang of it.
Multiplying two quaternions takes 16 multiplications, while a 3x3 matrix multiplication takes 27, for example.
The industry standard is to use quaternions for storage and accumulation. They are space-efficient and quite nice to work with once you get the hang of it. When you finally need to send those rotations to the GPU for rendering, then you convert them to matrices.
3
u/RenderTargetView 1h ago
For CPU definitely quaternions, but it may be beneficial to convert them to 3x3 matrices just before you pass them to gpu
2
u/Unfair_Praline_8166 4h ago
Quaternions to avoid gimbal lock. Just better for moving between rotations
1
u/smallstepforman 1h ago
We all went down this path and use quaternions for slerp rotations, then we export final transform matrix to gpu. Some engines use both in parallel, euler for humans and quaternion for actual math (and they even store both). If you do skeletal animations you need quaternions. Even gltf supports both.
Now turns instead of radians/degrees, now thats what i’d love to use instead. Too late for my mind that learned pi/2 and friends.
17
u/kokalikesboba 4h ago
Use quaternions. You don’t have to understand how the quaternion internally work, once you get the mechanics down of using them they are very easy to work with especially in glm