Common way to store model transformations - Game Development Stack Exchange most recent 30 from gamedev.stackexchange.com 2019-08-24T05:12:59Z https://gamedev.stackexchange.com/feeds/question/58094 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://gamedev.stackexchange.com/q/58094 0 Common way to store model transformations redreggae https://gamedev.stackexchange.com/users/29818 2013-06-25T14:47:58Z 2013-06-25T21:45:14Z <p>I ask myself what's the best way to store the transformations in a model class. What I came up with is to store the translation and scaling in a Vector3 and the rotation in a Matrix4.</p> <p>On each update (frame) I multiply the 3 matrices (first build a Translation and Scaling Matrix) to get the world matrix. In this way I have no accumulated error.</p> <pre><code>world = translation * scaling * rotation </code></pre> <p>Another way would be to store the rotation in a quaternion but then I would have a high cost to convert to a matrix every time step. If I lerp the model I convert the rotation matrix to quaternion and then back to matrix.</p> <p>For speed optimization I have a global dirty flag and one for each transformation so that I only do a matrix multiplication if necessary.</p> <pre><code>if (isDirty) { world = translation if (isScaled) { world *= scaling } if (isRotated) { world *= rotation } } </code></pre> <p>Is this a common way or is it more common to have only one Matrix4 for all transformations? And is it better to store the rotation only as quaternion? For info: Currently I'm building a CSS3D engine in Javascript but these questions are relevant for every 3D engine.</p> https://gamedev.stackexchange.com/questions/58094/-/58121#58121 0 Answer by Quonux for Common way to store model transformations Quonux https://gamedev.stackexchange.com/users/29278 2013-06-25T18:29:46Z 2013-06-25T21:33:56Z <p>I would prefer</p> <p>world = translation * scaling * rotation</p> <p>you save the translation and scaling and rotation matrix in each object and recalculate the matrices only if the transation/scaling/rotation changed.</p> <p>In the rendering loop you transform the object after the world matrix.</p> <p>The reason for this against the cached version is simple, it doesn't branch (except in the multiplication operation, but the compiler or you can inline/optimize it so it works without branches) and its more simple.</p> <p><strong>About the Quaternions</strong>: You use Quaternions only if the rotate operations are used very frequently (Forward kinematic, IK) because of the Quaternion&lt;->Matrix conversation overhead.</p>