# Applying matrix state changes

## Recommended Posts

Kest    547

##### Share on other sites
Zipster    2359
The proper transformation can be generated directly from the current state and the states of all parents whenever you need it. Ideally it should be calculated once and stored until invalidated. All the matrix operations involved with inverting the previous transformation and applying the new one is probably eroding your floating-point values rather quickly, which would cause stretching and other wackiness.

The transformation hierarchy can be updated recursively using something like this:
void updateTransform(Node* node, const Matrix& transform){   node->transform = transform * node->currentState;   if(node->child)      updateTransform(node->child, node->transform);}

Where the current state is the local space of the object relative to the parent space, and the transform is what's passed directly to the API as the world matrix. The multiplication order reverses for row vectors.

##### Share on other sites
Kest    547
The problem is that the child isn't always attached to the object. It's flying through the air on it's own, then comes in contact with an object and sticks to it like a parent. Even if I do keep a relative state of the child object, I need to convert the child's absolute world transform to a relative-to-parent transform when it attaches. That seems to be where I'm messing things up.

I thought I could just invert the parent's transform and multiply it into the child transform to get the relative state. Like this:

On attach:

child.RelativeState = child.Transform * Inverse( parent.Transform );

On child update:

child.Transform = child.RelativeState * parent.Transform;

Am I doing something wrong?

##### Share on other sites
Kest    547
I found the problem. I was generating a random starting transform of the child object using a velocity vector:

Transform.GetForward() = Normalized( Velocity );
Transform.GetRight() = RandomCross( Transform.GetForward() );
Transform.GetUp() = Cross( Transform.GetForward(), Transform.GetRight() );
Transform.GetTranslation() = Position;

But I neglected to initialize the "unused" portions of a 4x4 matrix. Specifically, the last float on each row. There was no detectable problem until I tried multiplying it with another matrix - the parent.

Sorry, I guess I didn't even know what my problem was before I posted.