<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[(Linear) Algebra Toolbox&nbsp;1]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>I&#8217;m prone to ranting about people making their lives more complicated than necessary with regards to math. Which I&#8217;ve been doing quite often lately. So rather than just pointing fingers and shouting &#8220;you&#8217;re doing it wrong!&#8221;, I thought it would be a good idea to write a post about what I consider good practice when dealing with problems in linear algebra and (analytic) geometry. This is basically just a collection of short articles. I have enough material to fill several posts; I&#8217;m just gonna write more whenever I feel like it. (Don&#8217;t worry, I didn&#8217;t abandon my series on Debris, we just had a few gorgeous weekends in a row, and living as I do in the Pacific Northwest sunny weekend days are a limited resource, so I didn&#8217;t do a lot of writing lately).</p>
<h3>Get comfortable manipulating matrix expressions</h3>
<p>In high school algebra, you learn how to deal with problems such as solving <img src="https://s0.wp.com/latex.php?latex=2x-5+%3D+3&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=2x-5+%3D+3&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=2x-5+%3D+3&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="2x-5 = 3" class="latex" /> for x using algebraic manipulations. The exact same thing can be done with expressions involving vectors and matrices, albeit with slightly more restrictive rules. It&#8217;s worthwhile to get familiar (and comfortable) with this. The most important rules to keep in mind are:</p>
<ul>
<li>Vector and matrix addition/subtraction can be handled exactly the same way as their scalar counterparts.</li>
<li>Since for matrices in general <img src="https://s0.wp.com/latex.php?latex=AB+%5Cne+BA&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=AB+%5Cne+BA&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=AB+%5Cne+BA&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="AB &#92;ne BA" class="latex" />, multiplying an equation by a matrix must be done consistently &#8211; either from the left on both sides, or from the right on both sides.</li>
<li>As an exception to the above, multiplying a matrix/vector etc. by a scalar is commutative, so the direction doesn&#8217;t matter, and the scalar can be moved around in the expression to wherever it is convenient.</li>
<li>Multiplying any scalar equation by 0 turns it into &#8220;0 = 0&#8221; which is true but useless. For matrix equations, the equivalent is that the matrices you multiply by must be non-singular, i.e. they must be square and have non-zero determinant.
</ul>
<p><b>Example:</b> Consider the map <img src="https://s0.wp.com/latex.php?latex=T+%3A+x+%5Cmapsto+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T+%3A+x+%5Cmapsto+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T+%3A+x+%5Cmapsto+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T : x &#92;mapsto Ax + t" class="latex" />. This is an affine transformation, i.e. a linear transformation followed by a translation. Let&#8217;s solve this for x: <img src="https://s0.wp.com/latex.php?latex=y+%3D+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=y+%3D+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=y+%3D+Ax+%2B+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="y = Ax + t" class="latex" /> &hArr; <img src="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+%28Ax+%2B+t%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+%28Ax+%2B+t%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+%28Ax+%2B+t%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="A^{-1} y = A^{-1} (Ax + t)" class="latex" /> &hArr; <img src="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+A+x+%2B+A%5E%7B-1%7D+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+A+x+%2B+A%5E%7B-1%7D+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%3D+A%5E%7B-1%7D+A+x+%2B+A%5E%7B-1%7D+t&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="A^{-1} y = A^{-1} A x + A^{-1} t" class="latex" /> &hArr; <img src="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%2B+%28-A%5E%7B-1%7D+t%29+%3D+x&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%2B+%28-A%5E%7B-1%7D+t%29+%3D+x&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D+y+%2B+%28-A%5E%7B-1%7D+t%29+%3D+x&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="A^{-1} y + (-A^{-1} t) = x" class="latex" />. The only interesting step here is multiplying from the left by <img src="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=A%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="A^{-1}" class="latex" />, the rest is very basic (and shown in more detail than I will use for the rest of this post). Note the result is in the same form as the expression we started with &#8211; meaning that the inverse of an affine map (when it exists) is again an affine map, not very surprisingly.</p>
<h3>Matrices depend on the choice of basis</h3>
<p>This one is really important.</p>
<p>In linear algebra, the primary objects we deal with are vectors and linear transforms &#8211; that is, functions that map vectors to other vectors and are linear. <b>Neither concept involves coordinates</b>. Particularly among programmers, there&#8217;s this meme that &#8220;vector&#8221; is basically just fancy math-speak for an array of numbers. <b>This is wrong</b>. A vector is a mathematical object, just like the number ten. The number ten can be <em>represented</em> in lots of different ways: as text, as &#8220;10&#8221; in decimal notation, as &#8220;1010&#8221; in binary notation, as &#8220;X&#8221; using roman numerals and so forth. These representations are good for different things: decimal and binary have reasonably simple algorithms for addition, subtraction, multiplication and division. Roman numerals are reasonably easy to add and subtract but considerably trickier to multiply, and the textual representation is related to the pronunciation of numbers when we read them out loud, but completely useless to do arithmetic on.</p>
<p>Writing a vector as tuple of numbers <img src="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+2+%26+3+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+2+%26+3+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+2+%26+3+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;begin{pmatrix} 1 &amp; 2 &amp; 3 &#92;end{pmatrix}^T" class="latex" /> is a particular representation that means &#8220;one times the first basis vector, plus two times the second basis vector, plus three times the third basis vector&#8221;. This representation depends on the choice of basis vectors. Any non-zero vector can end up represented as <img src="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5Cend%7Bpmatrix%7D%5ET&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;begin{pmatrix} 1 &amp; 0 &amp; 0 &#92;end{pmatrix}^T" class="latex" />, given the right choice of basis. Similarly, a matrix depends on the basis for the source and destination vector spaces, and a given linear transform can have any number of different representations, corresponding to different choices of basis vectors. The important bit here is that even though the numbers can be made almost anything, they&#8217;re still describing the same mathematical object, and everything that&#8217;s true for this mathematical object will be true no matter which representation we pick.</p>
<p>This means that we have a considerable amount of freedom in choosing a convenient representation. As long as we&#8217;re writing code that performs computation, we&#8217;ll still drop down to matrices and numbers eventually. But generally, we want to postpone that choice until we absolutely have to &#8211; the later we nail it down, the more information we have to make the resulting computation easier. And a lot of stuff can be done without ever choosing an explicit basis or coordinate system at all.</p>
<h3>What&#8217;s in a matrix?</h3>
<p>Now I&#8217;ve explained what the values in the coordinate representation of a vector mean, but I haven&#8217;t yet done the same for the values in a matrix. Time to fix this.  Note that with a matrix, there&#8217;s generally two vector spaces (and hence two bases) involved, so we have to be careful about which is which. Here&#8217;s a simple matrix-vector product expanded out using the usual matrix multiplication rule and then rearranged slightly:</p>
<p><img src="https://s0.wp.com/latex.php?latex=Ax+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+x_1+%5C%5C+x_2+%5C%5C+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Ax+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+x_1+%5C%5C+x_2+%5C%5C+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Ax+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+x_1+%5C%5C+x_2+%5C%5C+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Ax = &#92;begin{pmatrix} a_{11} &amp; a_{12} &amp; a_{13} &#92;&#92; a_{21} &amp; a_{22} &amp; a_{23} &#92;end{pmatrix} &#92;begin{pmatrix} x_1 &#92;&#92; x_2 &#92;&#92; x_3 &#92;end{pmatrix}" class="latex" /> = <img src="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+x_1+%2B+a_%7B12%7D+x_2+%2B+a_%7B13%7D+x_3+%5C%5C+a_%7B21%7D+x_1+%2B+a_%7B22%7D+x_2+%2B+a_%7B23%7D+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+x_1+%2B+a_%7B12%7D+x_2+%2B+a_%7B13%7D+x_3+%5C%5C+a_%7B21%7D+x_1+%2B+a_%7B22%7D+x_2+%2B+a_%7B23%7D+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+x_1+%2B+a_%7B12%7D+x_2+%2B+a_%7B13%7D+x_3+%5C%5C+a_%7B21%7D+x_1+%2B+a_%7B22%7D+x_2+%2B+a_%7B23%7D+x_3+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;begin{pmatrix} a_{11} x_1 + a_{12} x_2 + a_{13} x_3 &#92;&#92; a_{21} x_1 + a_{22} x_2 + a_{23} x_3 &#92;end{pmatrix}" class="latex" /> = <img src="https://s0.wp.com/latex.php?latex=x_1+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%5C%5C+a_%7B21%7D+%5Cend%7Bpmatrix%7D+%2B+x_2+%5Cbegin%7Bpmatrix%7D+a_%7B12%7D+%5C%5C+a_%7B22%7D+%5Cend%7Bpmatrix%7D+%2B+x_3+%5Cbegin%7Bpmatrix%7D+a_%7B13%7D+%5C%5C+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=x_1+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%5C%5C+a_%7B21%7D+%5Cend%7Bpmatrix%7D+%2B+x_2+%5Cbegin%7Bpmatrix%7D+a_%7B12%7D+%5C%5C+a_%7B22%7D+%5Cend%7Bpmatrix%7D+%2B+x_3+%5Cbegin%7Bpmatrix%7D+a_%7B13%7D+%5C%5C+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=x_1+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%5C%5C+a_%7B21%7D+%5Cend%7Bpmatrix%7D+%2B+x_2+%5Cbegin%7Bpmatrix%7D+a_%7B12%7D+%5C%5C+a_%7B22%7D+%5Cend%7Bpmatrix%7D+%2B+x_3+%5Cbegin%7Bpmatrix%7D+a_%7B13%7D+%5C%5C+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="x_1 &#92;begin{pmatrix} a_{11} &#92;&#92; a_{21} &#92;end{pmatrix} + x_2 &#92;begin{pmatrix} a_{12} &#92;&#92; a_{22} &#92;end{pmatrix} + x_3 &#92;begin{pmatrix} a_{13} &#92;&#92; a_{23} &#92;end{pmatrix}" class="latex" />.</p>
<p>This is straightforward algebra, but it&#8217;s key to figuring out what&#8217;s matrices actually <em>mean</em>. Note that the vectors appearing in that last expression are just the first, second and third columns of our matrix A, respectively. Those are vectors in the &#8220;output&#8221; vector space, which is 2-dimensional here; there&#8217;s also a 3-dimensional &#8220;input&#8221; vector space that x lives in. Often the two spaces are the same and use the same basis, but that is not a requirement! Anyway, if we plug in <img src="https://s0.wp.com/latex.php?latex=x_1+%3D+1%2C+x_2+%3D+x_3+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=x_1+%3D+1%2C+x_2+%3D+x_3+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=x_1+%3D+1%2C+x_2+%3D+x_3+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="x_1 = 1, x_2 = x_3 = 0" class="latex" /> (corresponding to the first basis vector in our input vector space), the result will just be the first column of A. Similarly, we&#8217;ll get the second and third columns of A if we choose different &#8220;test vectors&#8221; with the one in a different place.</p>
<p>And this is the connection between matrices and the linear transforms they represent: to form a matrix, just transform the i-th vector of your &#8220;input&#8221; basis and write the resulting vector represented in your &#8220;output&#8221; basis into column i. This also means that it&#8217;s trivial to write down the matrix for a linear transform if you know what that transform does to your basis vectors. Note that here and elsewhere, if you use row vectors instead of column vectors, you write xA, and the role of rows and columns are reversed.</p>
<h3>Change of basis</h3>
<p>Now to give an example for why these distinctions matter, let&#8217;s look at a concrete problem: suppose you have a 3D character model authored in a tool that uses a right-handed coordinate system with +x pointing to the right, +y pointing up, and +z pointing out of the screen, and you want to load it in a game that has a left-handed coordinate system with +x pointing right, +y pointing up (same as before) and +z pointing into the screen. Let&#8217;s also say that character comes (as character models tend to do) complete with a skeleton and transform hierarchy. Assume for simplicity that the transforms are all linear and represented by 3&#215;3 matrices. In reality they&#8217;ll be at least affine and probably be represented either as homogeneous 4&#215;4 matrices or using some combination of 3&#215;3 rotation matrices or unit quaternions, translation vectors and scale factors, but that doesn&#8217;t change anything substantial here.</p>
<p>So what do we have to do to convert everything from the modeler&#8217;s coordinate system to our coordinate system? It&#8217;s just a few sign flips, right? How hard can it be? Turns out, harder than it looks. While it&#8217;s easy enough to convert vertex data, matrices present more of a challenge, and randomly flipping signs won&#8217;t get you anywhere &#8211; and even if you luck out eventually, you still won&#8217;t know <em>why</em> it works now (you can ask anyone who&#8217;s ever tried this).</p>
<p>Instead, let&#8217;s approach this systematically. Imagine the model you&#8217;re trying to load in 3D space. What we want to do is <em>leave the model and transforms exactly as they are</em>, but express them in a different basis (coordinate system). It&#8217;s important to get this right. We&#8217;re going to be modifying the matrices and coordinates, true, but we&#8217;re not actually changing the model itself. Computationally, &#8220;express this vector in a different basis that has the z axis pointing in the opposite direction&#8221;, &#8220;reflect this vector about the z=0 plane&#8221; and &#8220;flip the sign of the z coordinate&#8221; are all equivalent, but they correspond to different conceptual approaches. When you perform a change in basis, it&#8217;s clear what has to happen with matrices representing transforms (namely, they have to change transforms to). A reflection is a transform &#8211; you might want to apply it to the whole model (at which point it&#8217;s best expressed as another matrix you pre-multiply to your transform hierarchy), you might want to reflect part of it, or you might want to just reflect some vertices. All of these actually modify the model (not just your representation of it), and it&#8217;s not clear which one is the &#8220;right&#8221; one. And so forth.</p>
<p>Anyway, enough of philosophy, back to the subject at hand: We have two different bases for the same 3D space: the one used by the modeler (let&#8217;s call it basis M) and the one used by your game (basis G). We want to leave the model unchanged and change basis from M to G. The linear transform that leaves everything unchanged is the identity transform I that sends every vector to itself. As mentioned above, every matrix depends on not only the linear transform it represents, but also the basis chosen for the input and output vector space. Let&#8217;s make that explicit by adding them as subscripts. We can then write down a matrix that takes a vector expressed in basis M, applies the identity transform (=leaves everything unchanged), then expresses the result in basis G. In our case, the x and y basis vectors in both G and M are the same, while the a vector expressed as <img src="https://s0.wp.com/latex.php?latex=1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1z" class="latex" /> in basis M corresponds to <img src="https://s0.wp.com/latex.php?latex=-1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=-1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=-1z&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="-1z" class="latex" /> in basis G (and vice versa). Putting it all together, we can write down the <em>change-of-basis matrix</em> to G from M:</p>
<p><img src="https://s0.wp.com/latex.php?latex=I_%7BGM%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5C%5C+0+%26+1+%26+0+%5C%5C+0+%26+0+%26+-1+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BGM%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5C%5C+0+%26+1+%26+0+%5C%5C+0+%26+0+%26+-1+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BGM%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%26+0+%26+0+%5C%5C+0+%26+1+%26+0+%5C%5C+0+%26+0+%26+-1+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{GM} = &#92;begin{pmatrix} 1 &amp; 0 &amp; 0 &#92;&#92; 0 &amp; 1 &amp; 0 &#92;&#92; 0 &amp; 0 &amp; -1 &#92;end{pmatrix}" class="latex" />.</p>
<p>If we wanted to go the opposite way (to M from G), we would use <img src="https://s0.wp.com/latex.php?latex=I_%7BMG%7D+%3D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BMG%7D+%3D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BMG%7D+%3D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{MG} = I_{GM}^{-1}" class="latex" />. Now what can we do with this? Well, first of, we can use it to convert vectors expressed in M to vectors expressed in G:</p>
<p><img src="https://s0.wp.com/latex.php?latex=x_G+%3D+I_%7BGM%7D+x_M&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=x_G+%3D+I_%7BGM%7D+x_M&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=x_G+%3D+I_%7BGM%7D+x_M&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="x_G = I_{GM} x_M" class="latex" /></p>
<p>Who would&#8217;ve thought, we end up sign-flipping the z coordinate. Written like this, it just seems like a verbose (and inefficient) way to write down what we already know. But note that the subscript notation allows us to see at a glance which basis we&#8217;re currently in. More importantly, we can see when we&#8217;re trying to chain together transforms that work in different bases. This comes in handy when dealing with trickier objects, such as matrices: note that the transforms stored in the file will both accept and produce vectors expressed in basis M; by our convention, we&#8217;d write this as <img src="https://s0.wp.com/latex.php?latex=T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T_{MM}" class="latex" />. Now say we want to get the corresponding transform in basis G &#8211; well, we could try the same thing that worked for vectors and just multiply the right form of the identity transform from the left:</p>
<p><img src="https://s0.wp.com/latex.php?latex=T_%7BGM%7D+%3D+I_%7BGM%7D+T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T_%7BGM%7D+%3D+I_%7BGM%7D+T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T_%7BGM%7D+%3D+I_%7BGM%7D+T_%7BMM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T_{GM} = I_{GM} T_{MM}" class="latex" /></p>
<p>Oops &#8211; see what happened there? That gives us a matrix that produces vectors expressed in basis G, but still expects vectors expressed in basis M as input. To get the true representation of T in basis G, we need to throw in another change of basis at the input end:</p>
<p><img src="https://s0.wp.com/latex.php?latex=T_%7BGG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BMG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T_%7BGG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BMG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T_%7BGG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BMG%7D+%3D+I_%7BGM%7D+T_%7BMM%7D+I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T_{GG} = I_{GM} T_{MM} I_{MG} = I_{GM} T_{MM} I_{GM}^{-1}" class="latex" /></p>
<p>So to convert everything to basis G, we need multiply all the vectors with <img src="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{GM}" class="latex" /> on the left, and multiply all matrices with <img src="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{GM}" class="latex" /> from the left and <img src="https://s0.wp.com/latex.php?latex=I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BGM%7D%5E%7B-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{GM}^{-1}" class="latex" /> from the right (the resulting double sign-flip &#8220;from both sides&#8221; is the bit that&#8217;s really hard to stumble upon if you approach the whole issue in &#8220;let&#8217;s just keep flipping signs until it works&#8221; mode). Alternatively, we can leave all the vectors and transforms as they are, happily working in basis M, but then throw in a single <img src="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I_%7BGM%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I_{GM}" class="latex" /> at the top of the transform hierarchy of the model, so that all of it gets transformed into the &#8220;game&#8221; basis before the rest of the rendering / physics / whatever code gets to see it. Either way works. And not only that, I hope that by now it&#8217;s clear <em>why</em> they work.</p>
<p><b>Note:</b> By the way, this convention of explicitly writing down the source and destination coordinate systems <em>for every single matrix</em> is good practice  in all code that deals with multiple coordinate systems. Names like &#8220;world matrix&#8221; or &#8220;view matrix&#8221; only tell half the story. A while back I got into the habit of always writing both coordinate systems explicitly: <code>world_from_model</code>, <code>view_from_world</code> and so forth. It&#8217;s a bit more typing, but again, it makes it clear which coordinate system you&#8217;re in, what can be safely multiplied by what, and where inverses are necessary. It&#8217;s clear what <code>view_from_world * world_from_model</code> gives &#8211; namely, <code>view_from_model</code>. More importantly, it&#8217;s also clear that <code>world_from_model * view_from_world</code> doesn&#8217;t result in anything useful. This stuff prevents bugs &#8211; the nasty kind where you keep staring at code for 30 minutes and say to yourself &#8220;well, it <em>looks</em> right to me&#8221;, too.</p>
<h3>Implement your solution, not your derivation</h3>
<p>This ties in with <a href="https://fgiesen.wordpress.com/2010/10/21/finish-your-derivations-please/">one of my older rants</a>. This is not a general rule you should always follow, but it&#8217;s definitely the right thing to do for performance-sensitive code.</p>
<p>In the example above, note how I wrote all transforms involved as matrices. This is the right thing to do in deriving your solution, because it results in expressions that are easy to write down and manipulate. But once you have the solution, it&#8217;s time to look at the results and figure out how to implement them efficiently (provided that you&#8217;re dealing with performance-sensitive code that is). So if, for some perverse reason, you&#8217;re really bottlenecked by the performance of your change-of-basis code, a good first step would be to not do a full matrix-vector multiply &#8211; in the case above, you&#8217;d just sign-flip the z component of vectors, and similarly work out once on paper what has to happen to the entries of T during the change of basis. </p>
<p>I&#8217;ll give more (and more interesting) examples in upcoming posts, but for now, I think this is enough material in a single batch.</p>
]]></html></oembed>