• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
IceCave

Matrix parent

9 posts in this topic

Hi,

 

I have watched a video about matrices from this guy

https://www.youtube.com/watch?v=KI0KZzqC6sA

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

Edited by IceCave
0

Share this post


Link to post
Share on other sites

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

Edited by Buckeye
2

Share this post


Link to post
Share on other sites

*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

0

Share this post


Link to post
Share on other sites

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.

Edited by Buckeye
0

Share this post


Link to post
Share on other sites

 


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.

0

Share this post


Link to post
Share on other sites

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

Edited by IceCave
0

Share this post


Link to post
Share on other sites

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.

Edited by Buckeye
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

 


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

0

Share this post


Link to post
Share on other sites

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.

Edited by Buckeye
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0