Matrix parent

Started by
8 comments, last by Buckeye 10 years ago

Hi,

I have watched a video about matrices from this guy

about matrices with a parent matrix.

So when you have a box on a merry-go-round with a matrix describing the position of the box and a matrix describing the merry-go-round, how to combine them together like if the box would be mounted on the merry-go-round.

However I don't really get how this is working. I tried to recode his example but get a total different result

(I can't see the rest of his code so I can only do what he says).

2D Example

This is my matrix describing the merry-go-round:


Matrix4f m_mgr = new Matrix4f();
m_mgr.translate( 10, 10 );
m_mgr.rotate( 30 );
m_mgr.scale( 5, 5 );

This is my matrix describing the box in general:


Matrix4f m_box = new Matrix4f();
m_box.translate( 0, 0 );
m_box.rotate( 45 );
m_box.scale( 2, 2 );

When drawing the objects I am doing this (as said in the video) (My mgr is not constantly spinning like in the example)


ogl.setTexture( t_mgr );
ogl.sendMatrix( m_mgr );
ogl.draw();

// set textures and stuff for box
ogl.setTexture( t_box );
ogl.sendMatrix( m_mgr.invert() * m_box );
ogl.draw();

Sadly this seems totally wrong and not at all like in the video.

He says that to get the local matrix we have to calculate m_mgr.invert() * m_box

However now I have a tiny hard to see box at the origin.

What my expected behaviour is, that the box is relative in space to the merry go round.

A space ship in a game i.e. has a lot single turrets mounted on it. When the space ship moves and turns the turrets also move and turn relative to the space ship. When creating a boss enemy which is in many games just a big version of the standard units, the turrets are also relative to the spaceship bigger and placed relative correctly.

I thought thats what matrices are actually for. If this is acquired by calculating the matrices manually for every turret, I lost my confidence that matrices are actually good for anything.

So again, what am I doing wrong?

Thanks you very much in advance,

- Icca

Advertisement

That video is the most complicated explanation of parent-child relationships I ever heard. I didn't finish watching the video so I'm not sure what he said about using matrix inverses.

If you have an object, such as a turret attached to a ship, which you want to rotate in place, calculate a matrix (say Mturret) for it, to include the turret's position with respect to the ship, and the turret's local rotation.

If the object has a parent object (for a turret, the parent would be the ship), calculate a matrix for the parent (translation, scale, rotation) - Mship - to orient the ship in "space."

The "space" coordinates of the turret are simply Mship * Mturret

The order of those matrix calcs: I'm assuming that you're using OGL and that OGL uses column-major matrices. If you're using row-major matrices, the calc would be Mturret * Mship

If you have another turret, calculate Mturret2 and render it with Mship * Mturret2

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

*looks at code*

You have to Rotate first THEN translate

For example this right here

m_mgr.translate( 10, 10 );
m_mgr.rotate( 90 );

This moves your object to location (10,10) then you rotated it by 90 putting the object in a new location at (-10,10).

If you did it in this order

m_mgr.rotate( 90 );

m_mgr.translate( 10, 10 );

It rotates first at (0,0) then it gets moves to (10,10) after translate is called.

Same with child to parent. For your example of the battle ships

Apply rotation to turret's matrix

Apply parent offset translate to turret matrix(This aligns the turret in the correct spot on the ship)

Apply Parent's matrix to the turret's matrix


You have to Rotate first THEN translate

That's incorrect. He's working with column-major matrices.

EDIT: I got interrupted as I was typing.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


You have to Rotate first THEN translate

That's if he's working with column-major matrices.

I actually don't follow what you mean. Translate and rotation both operate the same way with row/column matrices. If you have a point at 1,1 and translate it to 5,5 it moves to 5,5. If you try to rotate after translate you will still rotate around the axis. Usually when an artist makes a game asset like a ball the center of the ball would be at (0.0) so no matter which way you rotate the ball is in the same place. When you translate the ball all of the vertice's positions change to the new offset to maintain the same shape of the ball. So if we translate the ball to (5,0) and then rotate by 90 degrees the center of the ball would actual change positions from (5,0) to (0,5) or (0,-5) depending on how you set up your rotation function.

If you have an object, such as a turret attached to a ship, which you want to rotate in place, calculate a matrix (say Mturret) for it, to include the turret's position with respect to the ship, and the turret's local rotation.

If the object has a parent object (for a turret, the parent would be the ship), calculate a matrix for the parent (translation, scale, rotation) - Mship - to orient the ship in "space."

The "space" coordinates of the turret are simply Mship * Mturret

The order of those matrix calcs: I'm assuming that you're using OGL and that OGL uses column-major matrices. If you're using row-major matrices, the calc would be Mturret * Mship

If you have another turret, calculate Mturret2 and render it with Mship * Mturret2

I tried that and it works... somehow. It's simple. Thanks.

I also came up with that because it is in my opinion the most logical way to accomplish this, however my result is looking a little bit different.

This is because my parent was scaled in width and height differently, because the width to height ratio of the vertices were not correct.

So the child object did also change it's size depending on it's rotation.

Like this: (I tried it completly new and used a tank this time instead of a spaceship or a merry-go-round)

fbe6d0aeac.png

262a3d24c1.png

As you can see, the tower of the tank changes it's size depending on its rotation. So I thought this was incorrect, without taking into consideration that this might be because of the different width/height-ratio of the parent matrix.

I tried to eliminate that by removing the scale value again but with no luck. I guess I have to do this directly by adjusting the vertices.


// parent / child relationship
matrix.reset();

matrix.translate( x, y );
matrix.rotate( degree );
matrix.scale( w / parentWidth, h / parentSize ); // relative size to parent

matrix.multiplyMatrixTo( p_matrix ); // matrix = p_matrix * matrix

Thank you very much!

[...]

As it was said. Matrix multiplications are read from right to left (or in this case: down to up).

When calculating this

> roationMatrix2 * scalingMatrix * translationMatrix * rotationMatrix1 * pointInSpace = newPointInSpace

the newPointInSpace would be pointInSpace rotated by M1 then translated then scaled and then roated by M2 again.

Thanks for your help

- Icca

EDIT: The information in this post is INCORRECT. See my embarrassing retraction below.

I got interrupted in the middle of my previous post. See the edit above.


Translate and rotation both operate the same way with row/column matrices

Actually, they do not. Given a rotation matrix Mrot, and a translation matrix Mtrans, to combine them requires a different order, dependent on whether they're row-major or column major.

Mtotal = Mrot * Mtrans for column-major (e.g., OpenGL)

Mtotal = Mtrans * Mrot for row-major (e.g., DirectX)

Similar order of multiplication holds for multiplying vectors by matrices, also.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I got interrupted in the middle of my previous post. See the edit.


Translate and rotation both operate the same way with row/column matrices

Actually, they do not. Given a rotation matrix Mrot, and a translation matrix Mtrans, to combine them requires a different order, dependent on whether they're row-major or column major.

Mtotal = Mrot * Mtrans for column-major (e.g., OpenGL)

Mtotal = Mtrans * Mrot for row-major (e.g., DirectX)

Similar order of multiplication holds for multiplying vectors by matrices, also.

Oh ok, thank you for explaining that. I used DirectX and OpenGL. I guess DirectX hides that kind of information or something. All the tutorials and books I was reading with also follow the rotation first then translate,

Like in this http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5

D3DXMATRIX matRotateX;
D3DXMATRIX matRotateY;
D3DXMATRIX matRotateZ;
D3DXMATRIX matScale;
D3DXMATRIX matTranslate;

D3DXMatrixRotationX(&matRotateX, D3DXToRadian(50.0f));
D3DXMatrixRotationY(&matRotateY, D3DXToRadian(50.0f));
D3DXMatrixRotationZ(&matRotateZ, D3DXToRadian(50.0f));
D3DXMatrixScaling(&matScale, 5.0f, 1.0f, 1.0f);
D3DXMatrixTranslation(&matTranslate, 40.0f, 12.0f, 0.0f);

d3ddev->SetTransform(D3DTS_WORLD,
&(matRotateX * matRotateY * matRotateZ * matScale * matTranslate));

Also I don't really believe OpenGL really uses Row Major or Column Major math as it is the programmers choice to decide what they want to do since really both are a transpose of each other.

http://www.opengl.org/archives/resources/faq/technical/transformations.htm

9.005 Are OpenGL matrices column-major or row-major?

For programming purposes, OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix, where indices are numbered from 1 to 16 as described in section 2.11.2 of the OpenGL 2.1 Specification.

Column-major versus row-major is purely a notational convention. Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices. The OpenGL Specification and the OpenGL Reference Manual both use column-major notation. You can use any notation, as long as it's clearly stated.

Sadly, the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.


Translate and rotation both operate the same way with row/column matrices

Actually, they do not. Given a rotation matrix Mrot, and a translation matrix Mtrans, to combine them requires a different order, dependent on whether they're row-major or column major.

Mtotal = Mrot * Mtrans for column-major (e.g., OpenGL)

Mtotal = Mtrans * Mrot for row-major (e.g., DirectX)

Similar order of multiplication holds for multiplying vectors by matrices, also.

This is mostly true, except 2 things:

1. The terms row-major and colum-major are used to describe the order of flattening a 2D structure (matrix) for storage in linear memory. It has nothing to do with the mathematical definition of the matrix product (treating vectors as matrices where one dimension has length 1) and hence the fact that the matrix product is not commutative. It is the distinction between row vectors (or matrices) and column vectors (or matrices) which is to be considered here.

2. The order "apply scaling to the original vector, apply rotation to the formerly resulting vector, and apply the translation to the formerly resulting vector" is not fix. Truly its is often convenient to compose a transformation this way because it preserves the references to which the particular transformations work. E.g. scaling changes the space so that the original and the resulting principal axes are still coincident (only their scale has changed). However, there are situations where this is not wanted. Orbiting, for example, can be implemented by translating the object and rotating the result.

In the end the order of transformations is dependent on both, whether one uses column or else row vectors, and on the use case.


Similar order of multiplication holds for multiplying vectors by matrices, also.

The order of the terms of a matrix vector product is indeed defined mathematically. The prescription requires that the count of columns of the left matrix is identical to the count of rows of the right matrix. This requires a column vector to be placed on the right side of a matrix vector product, and otherwise a row vector on the left side. There is no other choice. The same mathematical constraint is not given when 2 transformation matrices are to be multiplied, because the transformations are build by 4x4 columns/rows (assuming homogeneous co-ordinates for 3D graphics).

MAJOR ERROR: (haegarr - why didn't you catch this and embarrass me? I have to embarrass myself now)


Mtotal = Mrot * Mtrans for column-major (e.g., OpenGL)

Mtotal = Mtrans * Mrot for row-major (e.g., DirectX)

The above equations that I posted are incorrect! Mea culpa. Mea culpa. Mea culpa.

Correction:

In general, the correct order (correcting terms I used from row-major to row-vector and column-major to column-vector [see haegarr's explanation of the difference] ):

For column-vector matrices, the order of multiplication for components to be applied to a single object is TRS (translate * rotate * scale) - e.g., Mtotal = Mtrans * Mrot

For row-vector matrices, the order of multiplication for components to be applied to a single object is SRT (scale * rotation * translate)- e.g., Mtotal = Mrot * Mtrans

The order of matrix multiplication to orient a child object to it's parent object is (correctly indicated above):

For column-vector matrices (e.g., OGL), MchildTotal = Mparent * MchildLocal

For row-vector matrices (e.g., DirectX), MchildTotal = MchildLocal * Mparent

@haegarr: thanks for the clarification on row-major vs. row-vector. You are, of course, correct ( reference this ).

Apologies to all.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement