Applying matrix state changes

Started by
2 comments, last by Kest 16 years, 4 months ago
I have an object which has a matrix transform. And I have a gadget that is attached to the object. The object and gadget update their transforms seperately. But when the gadget updates, I need it to change the gadget transform so that it looks like it's attached to the object's transform. I thought this was pretty simple, but I'm having a lot of trouble. When the gadget is first created, I read and record the object's current matrix state: gadget.ObjectState = object.Transform; Each time the gadget updates, I do the following: gadget.Transform *= Inverse( gadget.ObjectState ); gadget.ObjectState = object.Transform; gadget.Transform *= gadget.ObjectState; Or in english: Remove the previous known transform of the object from the gadget transform; Record the new object transform; Multiply the new object transform into the gadget transform; It doesn't work. The gadget object just stretches and flips out. Can anyone help me understand where I've wandered off track?
Advertisement
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.
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?

I appreciate your help.
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.

This topic is closed to new replies.

Advertisement