
Unlearn rotation matrices as rotations - dosshell
https://kodkodgames.gitlab.io/rotation_matrices/
======
MiroF
"Don’t think of them as rotations, think of them as a unit vectors of a new
coordinate systems."

This makes it clear to me that many people have been very poorly served by
their linear algebra courses if that is not obvious.

~~~
criddell
I'm one of those people. I really struggled with my undergrad linear algebra
courses. It was taught in a very abstract way with no suggestion ever that any
of this had useful applications. I passed (barely) and was glad to be free of
it.

Years later when I got into graphics programming I found myself dealing with
linear algebra concepts but this time it made much more sense to me. Being
able to apply the ideas to real problems with visual solutions made all the
difference.

~~~
0xfaded
I went through Stephen Boyd's "Introduction to Linear Dynamical Systems", and
at some point he said something like "Eigen vectors are just the directions
linear systems grow in", and bam!, many years of linear algebra came rushing
back and everything made sense.

~~~
MiroF
I had a very similar moment when I was thinking about why the determinants of
all (non-identity) projections are 0 and the fact that the determinant is the
product of the eigenvalues.

~~~
Balgair
> the determinant is the product of the eigenvalues.

Ohhhhh, _now_ I get determinants. I just thought they were some easy way to
classify a matrix, not that they had any special meaning. Not that they were
also all the eigenvalues multiplied too.

~~~
llamaz
That's one concrete way to view determinants, but it's not _the_ way to think
about them.

Determinants are kind of abstract and even inelegant. This is because it's
defined in terms of coordinates rather than abstractly. A coordinate free
definition exists, but it's very complicated.

Ideally they would not be taught at all, but it's useful for doing exam
problems. Cramer's rule in particular is quite useful when solving
simultaneous equations in engineering under exam conditions, for example. Even
in computer/engineering applications, row reduction/diagonal normal form type
are used instead.

Determinants are a way to get a scalar number out of a matrix. You have linear
forms, bilinear forms, and multilinear forms, the latter of which determinants
fall under.

There are certain properties you would like the determinant to satisfy, such
as det(AB) = det(A) det(B), so that the determinant can give you an idea of
whether a matrix is invertible. Eventually you work out that a determinant
_has to be_ a multilinear form with certain properties (alternating, skew
symmetric).

Again this is all very complex and messy. Initially 'linear algebra' was just
the study of determinants, because people thought of math in terms of solving
equations. Linear algebra, as it's known today, came later.

Bilinear forms turn up all the time, so ideally you would first learn bilinear
forms, then learn the properties a bilinear form can have (e.g. alternating,
skew symmetry, etc), and then learn determinants in that context.

~~~
rumanator
> Determinants are kind of abstract and even inelegant.

No they really aren't. Determinants represent the change in volume in a
transformation.

> Ideally [determinants] would not be taught at all, but it's useful for doing
> exam problems.

This is outright ignorant and wrong. Determinants are fundamental to a myriad
of central concepts in calculus and also in engineering fields. For instance,
volume integrals or any integral over a parametrization rely on determinants.

~~~
llamaz
I did degrees in both math and engineering, please give me the benefit of the
doubt that I'm not making a 1st year university level mistake.

In the specific examples you give, 2-forms could be introduced instead to
yield the final formula for the Jacobian. I don't think this is an overkill,
because it's possible to use 2-forms without precisely proving the theorems
etc about them. This is more convenient for math/physics people, and engineers
care more about the final formula than about how exactly it was derived.

As another comment here pointed to, the idea that linear algebra is better off
without determinants was popularized by Axler - I'm not making this opinion up
out of nowhere.

------
activatedgeek
> Rotation matrices just describe the unit vectors of a new coordinate system

More generally, _every_ matrix describes _how_ a change of coordinate system
should happen. For any m x n (m rows, n columns) matrix, each of the the n
column vectors represent how the current coordinate system (which may or may
not be unit vectors) should be represented in the new coordinate system (for a
left multiplied matrix).

Whenever a matrix's determinant is zero, it means that you squashed some
dimensions. As you can imagine, when m is not equal to n, there will always be
dimension squashing. Even when they are equal, that can happen. If you take a
3 x 3 matrix and it transforms _all_ the 3-D vectors into only planes (which
are 2-D objects), the determinant will be zero. This would be stated as having
a rank of 2. More simply, you'd say the "volume" of the transform to be 0
(because planes have zero volume).

EDIT: Make sure to read important clarifications by JadeNB below.

~~~
JadeNB
> Whenever a matrix's determinant is zero, it means that you squashed some
> dimensions. As you can imagine, when m is not equal to n, there will always
> be dimension squashing.

Although these two sentences are correct individually, it may be worth it to
emphasise that they should not be read together: the determinant is defined
only for _square_ matrices. (One can do something like computing the
determinant of det(A^{adjoint}A) if you want a general numerical invariant
that behaves as you like—and then we're getting in the neighbourhood of the
SVD).

> If you take a 3 x 3 matrix and it transforms all the 3-D vectors into only
> planes (which are 2-D objects), the determinant will be zero.

Although your meaning seems clear, I think you may have misspoken slightly. Of
course 3D vectors are transformed by a 3 x 3 matrix into _vectors_ , not
planes. To me, what I think you mean would read better as "If you take a 3 x 3
matrix and there is a plane such that all 3D vectors are transformed into that
plane, then …", or, perhaps even better, "… such that the transforms of all 3D
vectors lie in that plane …".

~~~
activatedgeek
Those are very important points. I've edited the parent to point out this
clarifying comment.

~~~
JadeNB
Thanks! I should also have mentioned that, assuming we've got an m x n matrix
acting by multiplying vectors fed to it on the right, dimension squashing is
guaranteed only if n is less than m, not whenever they are unequal. (I guess
one could argue that, in the case where n is greater than m, dimension
squashing happens if you feed in vectors on the left.)

~~~
activatedgeek
Yes, I did mean dimension squashing in either direction - left or right.

------
mdtusz
> The first column of the rotation matrix is the new x-axis expressed in the
> old coordinate system, the second column is the y-axis and so on. An
> identity matrix would yield in no rotation since all unit vectors would be
> the same as the previous coordinate system.

It's a shame that this isn't made more clear in most tutorials and classes.
The idea of axis application order really does make things more confusing than
is needed.

~~~
ziotom78
I remember that in my freshman's year, my linear algebra teacher explained
that _every_ matrix (i.e., not only rotation matrices) has this property: each
column is the result of the operation on each basis element in the old
coordinate system. This is true even for rectangular matrices.

~~~
rocqua
Once you get this, you can start building matrices for any (finite
dimensional) 'vector space' not just the standard 'vectors as a list of
numbers'.

All you need to do is to pick a basis for your vector space, and you can start
to represent linear transformations as matrices w.r.t. that basis. Even cooler
is when you let go of specific basis choices, and start talking about
properties of linear transformations that do not depend on a choice of basis.
Things like a determinant, eigenvalues, etc.

------
jpeloquin
If we relax the restriction that the columns of the 3x3 transformation matrix
must be unit vectors, we also get the ability to apply scaling and shear
transformations. In solid mechanics, this is called the F tensor, and is used
to describe deformation of materials [1].

There is also an augmented 4x4 form if translation is needed as well:

    
    
      | Q ...  Δx |         | vx |
      | .      Δy |         | vy |
      | .      Δz |   dot   | vz |   = new vector
      | 0 0 0  1  |         | 1  |
    

where "Q" is the 3 × 3 transformation matrix (e.g., rotation matrix) that is
the subject of the OP, and [vx, vy, vz, 1] is the augmented form of a vector
"v" = [vx, vy, vz] that is being transformed.

The augmented form is especially useful for transforming voxel indices in an
3D image array to spatial coordinates, such as for MRI or CT image data [2].
It is helpful if the index coordinate system has its origin at zero (zero-
indexed arrays), like a normal coordinate system.

Finally, if we have many [x, y, z, 1] data points that each have an old and a
new position, we can compute the overall best-fit transformation from the old
to new positions with the least-squares solution to

    
    
      A x = b
    

where "A" is the (unknown) augmented matrix representing the transformation,
"x" is a 4 × n array storing the points' old augmented-form positions, and "b"
is a 4 × n array storing the points' new augmented-form positions. If we allow
the points to have arbitrary dimension, without any particular spatial
interpretation, we get least squares curve fitting and the foundation of
machine learning.

[1]
[http://homepages.engineering.auckland.ac.nz/~pkel015/SolidMe...](http://homepages.engineering.auckland.ac.nz/~pkel015/SolidMechanicsBooks/Part_III/Chapter_2_Kinematics/Kinematics_of_CM_02_Deformation_Strain.pdf)

[2]
[https://nifti.nimh.nih.gov/nifti-1/documentation/nifti1field...](https://nifti.nimh.nih.gov/nifti-1/documentation/nifti1fields/nifti1fields_pages/qsform.html)

~~~
shezi
What you describe is very standard in 3D graphics, and it's alluded to in the
article by the question at the end. You can even use the last row of the
matrix as scaling factors (you'll have to re-normalize the output vector).

From a mathematical standpoint, this is now a projective vector space.

Some references:

[https://en.wikipedia.org/wiki/Transformation_matrix](https://en.wikipedia.org/wiki/Transformation_matrix)

[https://www.quora.com/Why-does-OpenGL-use-4D-matrices-for-
ev...](https://www.quora.com/Why-does-OpenGL-use-4D-matrices-for-everything)

[https://guide.handmadehero.org/code/day362/](https://guide.handmadehero.org/code/day362/)

~~~
sannee
Note that projective spaces are not vector spaces. There is no meaningful way
of adding vectors nor multiplying by a scalar.

Meaning we have to be very careful about treating our projective
transformations as linear maps, as they are, in fact, nothing like that.

Of course people doing graphics often pick a section of the projective space
("last component is 1"), then map that onto R^3 and off they go, sort of
ignoring the "planes at infinity" and praying for the best. This is messy but
I do not see a better way.

------
dazzawazza
I was eight years in to professional videogame dev. When I decided to debug
matrices used to generate racing track surfaces by drawing the columns in the
game world when I realised they were X/Y/Z axis. I felt so stupid. I jokingly
told the other coders around me how stupid I was... turns out we were all
stupid.

One old timer laughed in the background and told us how he discovered this on
the Amiga a few years before.

------
vivekseth
I learned this concept by watching 3blue1brown's series on Linear Algebra:
[https://www.3blue1brown.com/essence-of-linear-algebra-
page](https://www.3blue1brown.com/essence-of-linear-algebra-page)

Would highly recommend. I feel like Grant's videos gave me a better
understanding of Linear Algebra than the course I took in college.

~~~
pengaru
The 3b1b videos are really high quality, but I don't find them particularly
useful when learning new material. The fancy animations distract and
mesmerize, I slip into a mode of being entertained, becoming mostly a waste of
time.

For subjects I've already learned however they can be useful for gaining new
visual perspectives.

On the topic of learning rotation matrices / linear algebra in video games,
I'd strongly recommend the Handmade Hero youtube channel. Casey explains these
subjects at length in multiple videos, using plain chalkboard-style drawings I
personally find far less distracting.

~~~
AlchemistCamp
They give the "feeling of learning", which is worth only a little. They also
give the motivation to learn, which is invaluable.

------
beefield
Funny. Only some months ago I needed to figure out how to represent unit
vectors of (a rotated) coordinate system in another coordinate system. After
some days of drawing on the paper I came to the conclusion that actually the
rotation vector columns are just that. But found no way to verify that claim
in the internet. Thank you for this.

~~~
leereeves
That's not only true for rotation matrices. The columns of _any_ matrix are
where the unit vectors in the original coordinate system are mapped to when
multiplied by the matrix.

~~~
MiroF
What are colleges teaching these days if not this??

~~~
exabyte
For me I think the difficulty was just reconciling reality with the concept of
a coordinate system. I think it was just some abstract concept that I
considered to be out of my cognitive reach of understanding. I just had no way
to anchor anything being said to me to a concept I had in my head at the time.

I remember this poor teaching fellow where I went to Lehigh University where I
studied Mechanical Engineering. He was foreign and had the honor of teaching
us Linear Algebra. He must have noticed that none of us had even the slightest
damn clue what he was talking about. So, the class before every exam, he would
literally just give go over the exam problems nearly verbatim and solve them.
I came out of that class with a good grade and not the slightest fucking
understanding of Linear Algebra beyond the very superficial algebraic laws of
multiplying tensor objects (i.e., scalars (0-D tensor), vectors (1-D tensor),
and matrices).

I'd like to thank Grant Sanderson (3Blue1Brown) and Mike Cohen (Udemy Linear
Algebra course using Matlab and Python) for teaching me linear algebra. And
don't forget Gilbert Strang, of course!! (find his course on MIT OCW) The
visualizations of the tensors transforming provided by the first two are what
really made Linear Algebra start to click for me. I want to go back to Gilbert
Strang's course now that I have a better geometric understanding of what is
happening and appreciate all his wisdom on the subject.

~~~
MiroF
As a physics major at the time, I think that learning physics in addition to
multivar and linear algebra is _so_ handy in terms of providing reference
material for intuitions.

Also, learning it as part of a numerical computing stack (like python) where
you can experiment and visualize is huge for building intuition.

------
smlckz
I remember asking in freenode irc channel ##math that why matrix
multiplication is defined like that, why not in any other way. I was told
that:

>> cuz the composition of linear maps are defined like that.

It took a long time to understand what they meant. Still I see how it is
taught to work out mechanically. Brave friends of mine went to the Wikipedia
page to come back with more confusion. Wikipedia is a great reference when you
know the subject well enough.

[https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Mathemat...](https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Mathematics/Advice_on_using_Wikipedia_for_mathematics_self-
study)

~~~
exmadscientist
> Wikipedia is a great reference when you know the subject well enough.

Some math pages on Wikipedia are great. Some are... horrid. A while back
(2017?) I couldn't remember the terms in the Taylor expansion of sqrt(x), so I
went to Wikipedia. The article was one of the most jumbled mishmashes I'd ever
tried to read. But, even though I'm qualified to fix it, I didn't dare wade
into Wikipedia politics. The rewrite would have taken a while (it's a long
article), but I can't even imagine how long shoving it through would have
taken!

------
layoutIfNeeded
I’ve encountered this misunderstanding many times, usually in the context of
creating a rotation matrix for a given direction and “up” vector. People look
baffled when I replace their elaborate Euler-rotation algorithm with three
lines of code:

    
    
        z = direction
        x = up.cross(z).normalized()
        y = z.cross(x)
        M = [x, y, z]

~~~
andi999
Could you explain this is more detail?

~~~
kaetemi
Creates a matrix with one axis in a given direction. Then a second axis at a
straight angle to that, based on a reference up axis. The third axis is simply
perpendicular to both. Position goes into the last vector of the matrix.

~~~
abaines
How come _y_ is _z.cross(x)_ and not _up_?

Edit: I think it has been answered by others - _up_ is in world-space, and the
camera is not necessarily aligned horizontally.

~~~
layoutIfNeeded
Yep, the up vector is not necessarily orthogonal to the direction vector
(which is also called “look at” vector in OpenGL [1]). Another approach would
be to set

    
    
        y = up.reject(direction).normalized()
    

where

    
    
        a.reject(b) = a - b.project(a)
    

[1] [https://www.khronos.org/registry/OpenGL-
Refpages/gl2.1/xhtml...](https://www.khronos.org/registry/OpenGL-
Refpages/gl2.1/xhtml/gluLookAt.xml)

------
adenadel
I want to share my favorite linear algebra textbook (Linear Algebra Done Right
by Axler)

[http://linear.axler.net/](http://linear.axler.net/)

It's meant for a second course in linear algebra and the focus is on abstract
vector spaces and linear transformations (rather than a table of numbers
perspective). It also doesn't use determinants until the last chapter.

~~~
sannee
LADR is probably the best linear algebra book around. It's a bit of a shame
that the "marketing" mostly involves the fact that it does not use
determinants.

The last edition also contains chapters on stuff like dual spaces and quotient
spaces, which are important yet very often missing in other textbooks.

That being said, his reliance on C/R in about half the book could be a problem
for people with CS background. Like, what if I want to compute determinants of
linear maps which are over Z_2? There are also no chapters on number wrangling
(like the gaussian elimination and friends), which is a big plus in my view
but possibly a problem for others.

~~~
adenadel
I think these are great points. I justify the lack of some of the fundamental
concepts like Gaussian elimination to be due to the fact that the book is
meant for a second course in linear algebra. My copy has dual and quotient
spaces and I think these are important topics to get an introduction to if
you're looking to learn functional analysis, topology, etc. Some discussion of
finite fields could be interesting. This has prompted me to look up linear
algebra over finite fields.

------
undershirt
It’s clearer in 2D. Rotating by angle `a` tilts the x-axis to (cos(a),
sin(a)), because that’s the very definition of cosine and sine (i.e. the x and
y components of angles on the unit circle). The y-axis just flips the
components and negates one, because that’s what 90° rotations do.

------
a_zaydak
This is so important! I spend my life writing EKFs for navigation systems
however did not have a good linear algebra education. It was a hard struggle!
Now, every time I have a new hire or intern, the first thing that I go over is
basic linear algebra and rotations including DCM, Euler angles, quaternions,
and so on. I also make sure they understand it graphically such as how you can
scale and project vectors using a matrix.

~~~
tomo_kallang
Hi, do you mind if I reach out to you? I am working on similiar projects and
somehow every filters I tried have some issues.

~~~
a_zaydak
Sure, just send me an email.

------
bawana
I thought I knew what rotation matrices were until I read this article. Now I
am confused. I used to think I needed a 3x3 matrix to rotate a point (with
x,y,z coordinates) in 3 D space. But the article refers to Rx,Ry and Rz EACH
of which are a 3x3 matrix.Why are there 3 matrices? To move an x coordinate to
a new coordinate system, I only need to displace it among the the existing
three axes. So each coordinate only needs a 3x1 vector to displace it . That a
makes a single 3x3 matrix.

~~~
kergonath
Any number of successive rotations can be represented as a single matrix. Both
are right at the same time: an arbitrary rotation (represented by a single
matrix) is a combination of 3 rotations about the axes of the reference frame
(represented by 3 matrices).

------
failuser
I'd say quatenions are a nicer way to represent rotations, but 4x4 matrix can
describe any projective transformation in 3d.

------
FabHK
Two remarks:

1\. > Right handed, z forward through the nose and x through the left ear.

Interesting, in aviation engineering the convention is different: the body
frame has x forward (positive rotation around x is right roll), y to the right
(... pitch up), z down (... right yaw).

2\. The best reference (reference, not explanation/textbook) for this whole
spiel (rotation matrices, Euler angles, quaternions) I've seen is a paper by
Diebel, _Representing attitude: Euler angles, unit quaternions, and rotation
vectors_

[https://www.astro.rug.nl/software/kapteyn-
beta/_downloads/at...](https://www.astro.rug.nl/software/kapteyn-
beta/_downloads/attitude.pdf)

(Or maybe I like it because "Diebels Alt" is my favourite local beer...
[https://en.wikipedia.org/wiki/Altbier](https://en.wikipedia.org/wiki/Altbier))

------
uzbit
Speaking of unlearning rotations, has anyone looked at
[https://bivector.net](https://bivector.net) ? Some other hacker posted it a
while back on another thread and I found it to be really intriguing.

------
IshKebab
This is a great clarification. It's a shame that Wikipedia is such a bad
reference for learning maths. I think it is a result of most maths articles
being edited by people who just learnt something and therefore don't
understand it enough to explain it well, and also want to show off their
knowledge rather than actually explain things.

Mathworld is much much better in general.

(Somebody's probably going to reply "Wikipedia is an encyclopaedia not a
tutorial".)

~~~
jeffreyrogers
Wikipedia is great when you already know a math topic and just need to look
something up, but is terrible at teaching math. Generally the level of
abstraction is too high and too general for a first introduction to a topic.

~~~
swimfar
I find that sometimes even when I'm familiar with a topic, it ends up being a
bad reference. This is especially true when they start introducing variables
and constants that haven't even been defined.

------
jheriko
i have also encountered this problem. i learned the nature of rotation from
first principles in order to solve these problems without documentation...
which has turned out to be a massive boon.

tutorials are the devil. they do not teach so much as demonstrate a lot of the
time.

its a sad place to be

------
rory_h_r
There's nothing wrong with understanding rotation matrices as rotations, it's
just that in their application it's not the right mental model. Ideally you'll
understand both representations and switch between them as needed.

------
saeranv
The way I think about it: any matrix multiplication consists of the linear
combination of the column vectors of your transformation matrix, with each
column vector 'weighted' by the the input vector's x, y, z, ... n values.

------
syntaxing
I think the biggest struggle with rotation matrix isn't the theory but the
nomenclature. Is it R(ZYX)?! Is it rotation axis rather than Euler angles?!
All the convention differences can make signficant mistake very quickly.

------
dmch-1
For some time I was surprised that anyone would represent rotations by
anything else but rotation matrices. I sometimes do find wikipedia useful for
quick answers for maths, however agree its quite disorganised.

------
seventytwo
Is there a good web app where you can play around in a visual 3D space with
transforms like this and other ones like quaternions?

~~~
DreamScatter
Yes, check out [https://bivector.net](https://bivector.net)

[https://github.com/enkimute/ganja.js](https://github.com/enkimute/ganja.js)

and my own geometric algebra also:

[https://github.com/chakravala/Grassmann.jl](https://github.com/chakravala/Grassmann.jl)

