Let's remove quaternions from every 3D engine (2018)(marctenbosch.com)
marctenbosch.com
Let's remove quaternions from every 3D engine (2018)
https://marctenbosch.com/quaternions/
326 comments
I'm a pure math dude at heart, even if I don't get to do it much any more.
Two years ago, my wife asked me, "If you had to get a math equation tattooed on your body, what would it be?" I answered, "i^2 = j^2 = k^2 = ijk = -1".
I felt a brief flush of anger when I saw this headline.
This is an extraordinarily good article that should be read by pretty much anyone doing graphics programming.
Two years ago, my wife asked me, "If you had to get a math equation tattooed on your body, what would it be?" I answered, "i^2 = j^2 = k^2 = ijk = -1".
I felt a brief flush of anger when I saw this headline.
This is an extraordinarily good article that should be read by pretty much anyone doing graphics programming.
I've only skimmed the article, but it seems to be saying this:
> Instead of using this thing you don't understand and investing the time to understand it, why don't you use this other thing you don't understand, and invest the time to learn that instead.
Indeed, later in the article the author says:
> We can notice that 3D Rotors look a lot like Quaternions ... In fact the code/math is basically the same! The main difference is that i, j, and k get replaced by y∧z, x∧z and x∧y, but they work mostly the same way.
Have I got that right?
> Instead of using this thing you don't understand and investing the time to understand it, why don't you use this other thing you don't understand, and invest the time to learn that instead.
Indeed, later in the article the author says:
> We can notice that 3D Rotors look a lot like Quaternions ... In fact the code/math is basically the same! The main difference is that i, j, and k get replaced by y∧z, x∧z and x∧y, but they work mostly the same way.
Have I got that right?
Amusingly the discovery of Quaternions by Hamilton was also followed by arguments within the mathematical community as to whether mathematics should be rewritten in the language of quaternions, as Hamilton proposed to do and subsequently spent the rest of his life pursuing. Initially many mathematicians found them confusing, Hamilton's book is unusually difficult to read and there were no others. The work of Grassmann was somewhat neglected at the time, and eventually the Gibbs/Heaviside notion of vectors (essentially what we use today) emerged as a competitor to the quaternions. It seems it was a particularly bitter mathematical divide, up there with Newton vs Leibniz. Here is a quote by Tait, one of the "quaternionists":
"Even Prof. Willard Gibbs must be ranked as one of the retarders of quaternion progress, in virtue of his pamphlet on Vector Analysis, a sort of hermaphrodite monster, compounded of the notations of Hamilton and of Grassman"
See "History of Vector Analysis" by Crowe or "Hamilton, Rodrigues, and the Quaternion Scandal" by Altmann. Nice to see the author cites these!
The Geometric Algebra comes from Clifford Algebras, which where an attempt to combine Hamilton's Quaternions and Grassmann's forms, and in fact contains both as sub-algebras. In the case of 3D rotations calling them Rotors or Quaternions seems mostly like a different way of thinking about the same thing.
I think this would be more kindly put as "reimagining" quaternions and not "removing" them. The additional geometric intuition from GA does seem useful, and even as someone who has used quaternions extensively (though in a very different context), I would also choose to work with Geometric Algebra as a framework for geometry over Quaternions.
The visualizations are quite good here, it is a good way to understand bi-vectors, you can wiggle them about a bit in 3D instead of just staring at parallelograms on a page. The only criticism I have is that they say quaternions and the cross product come "out of nowhere", but then the way they present the geometric product is equally "out of nowhere".
"Even Prof. Willard Gibbs must be ranked as one of the retarders of quaternion progress, in virtue of his pamphlet on Vector Analysis, a sort of hermaphrodite monster, compounded of the notations of Hamilton and of Grassman"
See "History of Vector Analysis" by Crowe or "Hamilton, Rodrigues, and the Quaternion Scandal" by Altmann. Nice to see the author cites these!
The Geometric Algebra comes from Clifford Algebras, which where an attempt to combine Hamilton's Quaternions and Grassmann's forms, and in fact contains both as sub-algebras. In the case of 3D rotations calling them Rotors or Quaternions seems mostly like a different way of thinking about the same thing.
I think this would be more kindly put as "reimagining" quaternions and not "removing" them. The additional geometric intuition from GA does seem useful, and even as someone who has used quaternions extensively (though in a very different context), I would also choose to work with Geometric Algebra as a framework for geometry over Quaternions.
The visualizations are quite good here, it is a good way to understand bi-vectors, you can wiggle them about a bit in 3D instead of just staring at parallelograms on a page. The only criticism I have is that they say quaternions and the cross product come "out of nowhere", but then the way they present the geometric product is equally "out of nowhere".
While we're at it, can we please move the homogeneous coordinate to be the first value in memory? That way, a vector or a point, if they're represented sparsely in the data structure, just work.
[1], a point at the origin, is the same as [1 0] - a point at the origin in 1 dimension, or as [1 0 0], a point at the origin in 2 dimensions, or [1 0 0 0], a point at the origin in 3 dimensions.
Similarly, [0] is a zero vector. [0 0 0 0] is a zero vector in 3 dimensions.
Having to know [0 0 1] is a point at the origin in 2 dimensions (with a homogeneous coordinate), while [0 0 1] is a z vector in 3 dimensions (without the homogeneous coordinate), is just silly.
Why did we do this to ourselves?
[1], a point at the origin, is the same as [1 0] - a point at the origin in 1 dimension, or as [1 0 0], a point at the origin in 2 dimensions, or [1 0 0 0], a point at the origin in 3 dimensions.
Similarly, [0] is a zero vector. [0 0 0 0] is a zero vector in 3 dimensions.
Having to know [0 0 1] is a point at the origin in 2 dimensions (with a homogeneous coordinate), while [0 0 1] is a z vector in 3 dimensions (without the homogeneous coordinate), is just silly.
Why did we do this to ourselves?
When I was learning graphics, the reasoning around quaternions was to avoid "gimbal lock". It was just taken as orthodoxy without question.
I think going forward next gen 3D engines will have to account for the improvements in GPU hardware realized by advances in machine learning. Mixed precision matrix multiply at massively parallel scale. As well as the demands of next-gen games. Things like real time ray tracing of deformable meshes ;)
I think going forward next gen 3D engines will have to account for the improvements in GPU hardware realized by advances in machine learning. Mixed precision matrix multiply at massively parallel scale. As well as the demands of next-gen games. Things like real time ray tracing of deformable meshes ;)
Quaternions are 3D rotations (plus, possibly, rescaling). The author notes that they have the same API (I like this term used in mathematical context).
So, the actual point is that the word quaternions (and "these strange i, j, k") is confusing. Rightfully (at least for anyone without a background in maths or physics).
"Let's remove Quaternions from every 3D Engine" -> "Let's remove the word 'quaternion' from every 3D Engine"
(Nice explanations and visualization, anyway!)
So, the actual point is that the word quaternions (and "these strange i, j, k") is confusing. Rightfully (at least for anyone without a background in maths or physics).
"Let's remove Quaternions from every 3D Engine" -> "Let's remove the word 'quaternion' from every 3D Engine"
(Nice explanations and visualization, anyway!)
Previous discussion: https://news.ycombinator.com/item?id=18365433
Check out http://bivector.net, a new community on geometric algebra.
Check out the demo https://observablehq.com/@enkimute/animated-orbits
Join the discord https://discord.gg/vGY6pPk
Check out the demo https://observablehq.com/@enkimute/animated-orbits
Join the discord https://discord.gg/vGY6pPk
The reason many people have trouble understanding quaternions is that they reason about them incorrectly.
Quaternions are actually a separate 'thing' when compared to vectors (which is why their multiplication seems off and the square is a negative number).
Quaternions should be considered a versor (a rotation around great circles), that is a change in direction which is different from a vector.
See this work: https://archive.org/details/cu31924001506769/
Quaternions are actually a separate 'thing' when compared to vectors (which is why their multiplication seems off and the square is a negative number).
Quaternions should be considered a versor (a rotation around great circles), that is a change in direction which is different from a vector.
See this work: https://archive.org/details/cu31924001506769/
I am impressed at the number of people reacting with « Euler angles are fine » or « quaternions are fine, we always did this way ». Having worked extensively with them it is obvious that Euler angles are broken beyound repair (singularities, 12 competing variations...) and quaternion are too (they only work in 3D). This article is extremely interesting and absolutely true. If geometric algebra had been discovered earlier we would never have needed a lot of these unnatural constructs.
In order to make geometric algebra really nice, you have to go 5D, see versor.mat.ucsb.edu. This imposes a significant computational overhead. Nice things are expensive.
You don't need to understand the root construction of Quaternions to use them in game code anymore than you need to understand the root construction of the Reals to be able to do arithmetic in game code. Just treat them as opaque values that have certain operations you can perform to get certain results and be done with it.
From the title, I was expecting to read an article by a crazy old retro mathematician shaking his fist and yelling at clouds that we should all go back to using Euler angles. But this was a much better article than the title suggested!
This seems to be an excellent introduction to geometric algebra, a topic I have had difficulty picking up so far.
Let's remove them from one 3d engine first, and see how it goes.
Putting content aside (it's great actually), I love the format used for the article. Each header is also a link to a time point in the accompanying video. There are interactive canvases that are also used in the video. The only missing part is a teacher AI which I can ask questions to (just kidding).
They say Geometric Algebra is "the new language of physics" and whatever else; my problem with this is that, unlike, say the calculus of differential forms (or tensors), it does not make sense in more general manifolds which are at the heart of the modern theoretical physics. Geometric Algebra, therefore, is more like something that, sure, could be taught in high school in an attempt, for example, to streamline elementary vector algebra and thus eliminate some nasty questions (should they arise); yet, there is some mental load to it that may make it not worth the effort...
His criticism of the cross product seems more poignant than his criticism of quaternions in computer programming. One might ask -- why not teach bivectors in introductory math instead of cross products?
Historically it seems like 3D geometry and particularly cross products in the context of electromagnetism fomented the primary demand in mathematics education for students to be taught vectors. Unfortunately the mathematics curriculum (in Western countries) has not really been updated in decades to better prepare students for the jobs of today; we still enroll all students in a sequence that culminates in differential equations and particularly in second-order linear differential equations, which just so happen to be crucial to control problems in electrical and mechanical engineering. While many people's jobs involve some kind of mathematics somehow, only a few jobs involve the mathematics of electrical engineering, and I suspect that is part of the reason why so many students are bored in math class.
Historically it seems like 3D geometry and particularly cross products in the context of electromagnetism fomented the primary demand in mathematics education for students to be taught vectors. Unfortunately the mathematics curriculum (in Western countries) has not really been updated in decades to better prepare students for the jobs of today; we still enroll all students in a sequence that culminates in differential equations and particularly in second-order linear differential equations, which just so happen to be crucial to control problems in electrical and mechanical engineering. While many people's jobs involve some kind of mathematics somehow, only a few jobs involve the mathematics of electrical engineering, and I suspect that is part of the reason why so many students are bored in math class.
A point that I don't think has been mentioned here: quaternions as a representation of attitude are really just a funky parameterization of an Euler axis/angle representation. Quaternions happen to be parameterized in a way that lets you avoid the evaluation of trigonometric functions when expressing their dynamics. This used to be important, e.g. in aircraft control systems, because computing power was extremely limited and you didn't want to have to evaluate sines and cosines in real time.
The biggest disadvantage of using quaternions over Euler axis/angle representations is that quaternions are basically impossible for humans to visualize, whereas Euler axis/angle representations are easier than transformation matrices, Euler angles, or any other representation.
So why not just use Euler axis/angle representations instead? Nobody cares any more about evaluating cosines at 1 kilohertz, and there would be none of this complicated geometric algebra stuff that nobody understands.
The biggest disadvantage of using quaternions over Euler axis/angle representations is that quaternions are basically impossible for humans to visualize, whereas Euler axis/angle representations are easier than transformation matrices, Euler angles, or any other representation.
So why not just use Euler axis/angle representations instead? Nobody cares any more about evaluating cosines at 1 kilohertz, and there would be none of this complicated geometric algebra stuff that nobody understands.
Worth noting that the author is the guy behind this extremely interesting project to create a 4D puzzle game: https://miegakure.com/
He's given a lot of talks on subjects like this. I imagine that what he's doing mathematically is a lot more intricate than most games need though.
He's given a lot of talks on subjects like this. I imagine that what he's doing mathematically is a lot more intricate than most games need though.
It is somewhat strange that all these articles/blogs claiming that Geometric Algebra is inherently superior to Quaternions spend so much time on how Quaternions are isomorphic to the even-subalgebra of GA3 (hence for all intents and purposes they're the same) and so little time on the odd-subalgebra (exterior algebra IIRC), which as far as I can tell is the main (only?) advantage of GA over Quaternions.
On the other hand, almost nobody mentions the nice geometric perspective that unit quaternions offer that are somehow "lost in translation" in GA: as a compact Lie group, unit quaternions come endowed with a bi-invariant Riemannian metric which means you can do interpolation, clustering, blending, statistics with them in a metric-consistent manner. And since the metric is compatible with the group structure, the geodesics are cheap to compute.
On the other hand, almost nobody mentions the nice geometric perspective that unit quaternions offer that are somehow "lost in translation" in GA: as a compact Lie group, unit quaternions come endowed with a bi-invariant Riemannian metric which means you can do interpolation, clustering, blending, statistics with them in a metric-consistent manner. And since the metric is compatible with the group structure, the geodesics are cheap to compute.
Having written engines using both GA and matrices+quats, as well as writing articles on GA (one intro one in an old Games Gems book), I'd say GA is a terrible idea for 3D engines. GA is inherently slower to manipulate, as items require more storage and more memory touches. They add almost zero benefit.
If the idea is use a higher, more pure math structure, then one can go to even more abstract math formalisms, such as coord-free calculus and bigger algebras, but these, like GA, add more computational overhead to solve problems that don't exist.
Hestenes et. al., the main popularizes of GA in the math/programming intersection, have papers on writing raytracers in both, and they too clearly demonstrate loss of performance using GA.
If the idea is use a higher, more pure math structure, then one can go to even more abstract math formalisms, such as coord-free calculus and bigger algebras, but these, like GA, add more computational overhead to solve problems that don't exist.
Hestenes et. al., the main popularizes of GA in the math/programming intersection, have papers on writing raytracers in both, and they too clearly demonstrate loss of performance using GA.
Hm. I don't claim to fully understand quaternions, but I simply think of them as an obfuscated version of axis-angle representation, which seems to serve me well in reasoning about operations on them.
[deleted]
Nice thought, perhaps, but I doubt anything would come of it. Quaternions came into vogue because the problems of Euler angles were very apparent in practice, but we still think, at best, in yaw/pitch/roll, and for a lot of people, roll is a little on the sketchy side.
Now we have twenty, twenty-five years of code and resources that make use of quaternions. In some ways, game development is incredibly hide-bound and conservative, and for the most part eschews rigid correctness for performance, convenience, and a loosey-goosey good 'nuff feel.
Now we have twenty, twenty-five years of code and resources that make use of quaternions. In some ways, game development is incredibly hide-bound and conservative, and for the most part eschews rigid correctness for performance, convenience, and a loosey-goosey good 'nuff feel.
Isn't the only difference between rotors, as defined in this article, and quaternions the sign of the second 3d component?
And this only happens because the planes are defined as "xy", "xz" and "yz", rather than the more consistent "xy", "yz" and "zx"?
If you just changed the definition of the planes at the start of the derivation, it seems you would end up deriving the exact same operations as you would use with quaternions? I'm not sure if there is a good argument for not doing that.
And this only happens because the planes are defined as "xy", "xz" and "yz", rather than the more consistent "xy", "yz" and "zx"?
If you just changed the definition of the planes at the start of the derivation, it seems you would end up deriving the exact same operations as you would use with quaternions? I'm not sure if there is a good argument for not doing that.
Let's also remove bivectors and instead use skew-symmetric matrices ( https://en.wikipedia.org/wiki/Skew-symmetric_matrix ) . The modern matrix notation is so much more powerful and flexible than all those 19th century inventions like complex numbers, quaternions, bivectors, dual numbers, ... that we should really stop teaching all those old confusing stuff.
This is a very cool article which has left me wanting more. It seems to stop just short of the end though, there is no section to actually explain the component values in a rotor, or an interactive diagram deconstructing the component parts of a rotor. Or did I miss it?
I'd also be really interested to see some more applications. I wonder what rotor interpolation looks like for example? I know I've had problems with quaternion interpolation in the past.
I'd also be really interested to see some more applications. I wonder what rotor interpolation looks like for example? I know I've had problems with quaternion interpolation in the past.
Yes, the even Clifford algebra is the quaternions. So what?
I worked in a game engine where a pair of mathy programmers fell in love with Geometric Algebra, and use this same argument that quarternions ought to be replaced to overhaul all of the math code in the engine using Geometric Algebra. The character rigging system removed matrices and used GA instead.
This caused several large problems in the code base:
For one, it slowed the code down a little because the GPU interface is all matrices, so there were conversions to matrices all over the place, rigging in particular.
And only two guys in the studio knew Geometric Algebra, and they didn’t invest time in teaching it or helping people understand it, they just hoisted it on everyone. All the rest of the programmers knew matrix math but not Geometric Algebra, so they would end up avoiding touching any of the GA code, i.e., any code that dealt in transformations. The two guys ended up with a lot of support of their own creation, but they were short with their answers, in part because they got so many questions, so the problem never went away.
The third problem is this whole rewrite was unnecessary. Fixing gimbal lock with quaternions is a tiny corner of the game engine, whereas using GA throughout is a massive rewrite. Matrices work really well for 98% of the code, and it’s not really a huge problem to have one or two routines that convert to quaternions and back while they do a rotation. It is a problem when any transform at all involves bivectors and rotors and you have no idea what the hell those are nor do you have time in your schedule to take a math class at work.
Personally, I’m intrigued by GA and have wanted to learn it for a while, but having used it in production, I’m mildly against replacing quaternions with GA, and very wildly against replacing matrices with GA.