Align object to bone problem

Nov 8, 2010 at 11:08 AM
Edited Nov 8, 2010 at 11:23 AM

I've been trying to figure out how to convert a bone position/rotation to world space to align another model to it but am having a lot of trouble.  There are a few things, a lot of things, everything, that is driving me crazy with this problem.  There are already a few threads on it, but they are several years old and the examples hard to implement since little things are left out here and there...

http://xnanimation.codeplex.com/Thread/View.aspx?ThreadId=59867
http://xnanimation.codeplex.com/Thread/View.aspx?ThreadId=34626

What I was trying...

In the world is the character class, with "rotation", "position" and "animation_controller".

In the object I'm trying to attach, it gets a reference to the parent (as "parent").  Then the object update method has...

int origin_bone = parent.model.Model.Bones["chi"].Index;

Matrix world_sys = Matrix.CreateRotationX(parent.rotation.X)
   * Matrix.CreateRotationY(parent.rotation.Y) 
   * Matrix.CreateTranslation(new Vector3(parent.position.X, parent.position.Y, parent.position.Z));

Matrix parent_sys = world_sys
*parent.animation_controller.SkinnedBoneTransforms[origin_bone];

collision.Center = Vector3.Transform(new Vector3(0, 0, 0), parent_sys);

I've tried a lot of different things, switching around the world and parent matrix (I think they're backwards right now actually), changing the origin vector of the final transform to the parent origin and the bone itself.  Things that just make more questions?  Is rotation supposed to come first?  When building a new matrix?  Is rotation not included in the retun of "SkinnedBoneTransforms[bone]"?  Does rotation come before translation when making a matrix?  A big problem is, if I wanted to use the decompose approach, how to convert quaternions to radians?  Could I ask more questions?

When getting a bone matrix, does it always have to be the first bone or is there a way to get the end of the chain and have the returned matrix be relative to the model origin?  I think I can constrain one bone to another in max so that the weapon bone is always it's own chain, but I don't know how to make sure.

When I decompose a bone matrix and print the output position and rotation, the position of the bone sometimes has weird figures.  I could understand the rotation being off a few degrees from it's place inside max since we're dealing with matrices, but the Y of the bone sometimes has a negative value when it should be a positive, resulting in the item I'm trying to attach appearing well underneath the character and below the floor.

Any help would really be appreciated.  It's quiet here, but I doubt I'm the only one looking for this every now and then, it really is a useful animation library.