Re: Summing matrices
 July 05, 2014 11:21PM Standards Committee Member James Jessiman Memorial Award Recipient LDraw Part Author/Reviewer Registered: 4 years agoPosts: 880
3D graphics are pretty much all about 4x4 and 1x4 matrix multiplication. (The last component of a coordinate's 1x4 matrix is 1, and can usually be ignored.) A typical 3D matrix will look like so:

```| A B C X |
| D E F Y |
| G H I Z |
| 0 0 0 1 |```

(Note that they are often mirrored across the diagonal axis of the matrix (so the last row would be X Y Z 1, instead of that being the last column. However, the above is the form I'm personally used to.)

A-I encode things like rotation and scale, and match the last 9 numbers in an LDraw type 1 line, while X, Y, and Z represent translations, and match the first 3 numbers in an LDraw type 1 line. The bottom row is typically exactly as I showed (0s and a 1). If you read the line type 1 portion of the LDraw spec, you will see this described.

What isn't explicitly given in the spec is equations to use for models nesting inside each other, but that is done by simply multiplying all the relevant 4x4 matrices together. Repeat that process for every submodel between the source and the destination, and you'll end up with a composite matrix that represents all the nested transformations. Multiply your original coordinate by that matrix, and you'll get the final transformed coordinate.

Note that with matrix multiplication, the order is important, unlike with scalar multiplication. (With matrices, A x B x C does not equal C x B x A.) Matrix multiplication is extremely simple, but very tedious, so I'm not going to write the 16 equations needed to multiply two 4x4 matrices together. (One equation for each element of the final 4x4 matrix.) But you can look it up and easily find those. Plus, Tim already gave code (or perhaps pseudo-code; I'm not sure) to do that.

Being honest, Tim answered the question fully, and my response here is perhaps redundant, but I'm trying to answer it using different phrasing that may (or may not) be easier to understand.
SubjectAuthorViewsPosted
Michael Horvath446July 03, 2014 06:16PM
Tim Gould218July 04, 2014 02:18AM
Michael Horvath217July 04, 2014 01:46PM
Tim Gould271July 05, 2014 05:36PM
Travis Cobbs252July 05, 2014 11:21PM
Philippe Hurbain230July 06, 2014 07:39AM
Tim Gould242July 06, 2014 05:38PM
Michael Horvath200July 08, 2014 08:24AM
Travis Cobbs252July 08, 2014 10:04AM

Sorry, only registered users may post in this forum.