onsdag 4. juni 2008

The Magic of the Matrix

Matrix nodes are used to attach graphics to control points.
In this article I'll try to explain how they work.

1. A matrix node influences the coordinate system of sub nodes.

For example:


When you move matrix1, all sub nodes also move.
Since the matrix itself is invisible, it works perfect as a group tool.

2. Combining multiple matrices result into one, new coordinate system.

For example:



equals

A * B * C * D = X

Where matrix X is used to draw the sub graphics.

What happens behind the scene when the graphics is attached to control points?

All matrix nodes got an 'Attach' action. This is used to 'glue' the matrix to control points.

First, put the graphics you want to attach into a matrix node.



Then, with bone tool, create two control points.



Select the 'body' matrix and use the 'Attach' action:





The 'Attach' action creates a new matrix node named 'bodyMatrix'.



If we select 'bodyMatrix' we see the center is at 'point1' and rotating in direction of 'point2'.



'bodyMatrix' has the following expressions:

bodyMatrix.Offset = point1.Pos

bodyMatrix.Rotate = Rotate(point2.Pos;point1.Pos;.Rotate)

These epressions are executed at run time.

Notice the properties of 'body' also have changed.
This is to compensate for the Offset and Rotate property of 'bodyMatrix'.

Before:



After:



To understand this we need to think of matrix nodes as kind of math with just one law:

A * 1/A = 1

1/A equals the inverted A matrix.
1 equals a matrix node with default values.

To calculate the new values of 'body', the following formula is used:

oldbody = oldbody

bodyMatrix * 1/bodyMatrix * oldbody = oldbody

bodyMatrix * (1/bodyMatrix * oldbody) = bodyMatrix * newbody = oldbody

newbody = 1/bodyMatrix * oldbody

Matrix math is not only used in Stickman & Elemento but in most 2D and 3D games. If you can't understand them, don't worry. It just works!

1 kommentar:

DGCombs sa...

Very interesting explanation of matrices. Thanks! That helps a lot. Now I understand why my doggie's tail went spinning off to the left.

- Dan