I have model A contained within model B contained in model C. How do I determine the position of model A relative to model C's coordinate system? I know that the XYZ coordinates can simply be added. But what about rotation? Thanks!

**Summing matrices**

Here's some pseudo-code. I don't think an excel spreadsheet would tell you anything much.

Michael Horvath Wrote:I know that the XYZ coordinates can simply be added.

Actually no they cannot. You must rotate the displacement of C relative to B.

Tim

Code:

`# Multiply a vector by a matrix`

function Xn=MatVec(R,X)

for i from 1 to 3

Xn(i)=0

for j from 1 to 3

Xn(i)=Xn(i)+R(i,j)*X(j)

end

end

endfunction

# Multiply two matrices

function Rn=MatMat(R1,R2)

for i from 1 to 3

for j from 1 to 3

Rn(i,j)=0

for k from 1 to 3

Rn(i,j)=Rn(i,j)+R1(i,k)*R2(k,j)

end

end

end

endfunction

# Find the rotation and displacement of A relative to C [XCA and RCA]

# using # the rotation and displacement of A relative to B [XBA and RBA]

# and B relative to C [XCB and RCB]

function [XCA,RCA]=Compound(XBA,RBA,XCB,RCB)

RCA=MatMat(RCB,RBA)

XCBp=MatVec(RBA,XCB)

for i from 1 to 3

XCA(i)=XBA(i) + XCBp(i)

end

endfunction

Code:

`| 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.

http://www.mathsisfun.com/algebra/matrix...lator.html

Useful when you have just a handful calculations to do and don't want to create a program just for that.

Travis Cobbs Wrote: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.

The more answers, the merrier And I think your explanation is better.

I should note that with my background (maths and physics) I tend towards the 3x3 + 3 interpretation. But the 4D notation certainly has its benefits and is popular with 3D graphics people for a reason.

Tim

**« Next Oldest | Next Newest »**

Users browsing this thread: 1 Guest(s)