My Skinned Model Moving Shadow sample on my
web site happens to include the code for attaching one model to another. Try having a look at that. I use it so the model can hold a weapon in it's hand.
You appear to be using the skin positions rather than the bone positions. I would have assumed you would get an odd offset result where your second object is close to where you want it but not quite right.
The code I use with the worldTransforms is very simple especially if you already know the bone ID. I use a bone map that saves the string name to the bone ID in the ModelProcessor just by looping through the bones.
Anyway in summary:
Update the moving model, so the bones are in the position you want.
Get the worldTransforms of every bone and select just the bone you need.
Use that transform for the world position of the object you want to move with your first
This is from the Model's Update function:
| public void Update(TimeSpan timeSpan, bool bRelativeToCurrentTime, Matrix mtxMoveModel) |
| { |
| if (bCanAnimate) |
| { |
| // update the main model position |
| animationPlayer.Update(timeSpan, bRelativeToCurrentTime, mtxMoveModel); |
| // update all attached models with the position of the main model |
| foreach (AttachedModels item in itemsAttached) |
| { |
| // Move the model to where the model's parent is. |
| item.modelAttached.setWorldPosition(animationPlayer.GetWorldTransforms()[item.idBone] * mtxWorldPosition); |
| } |
| } |
| } |
| |
I hope most of the above is obvious but note that the WorldTransforms need to be multiplied by the original models WorldPosition to get the world position for your other model.
This is another model function:
| public void setWorldPosition(Matrix mtxPosition) |
| { |
| mtxWorldPosition = mtxPosition; |
| } |
| |
| |
Note that when called in the above Update example it is applied to the other model not the original moving model. In my case the attached model is of the same class as the model it is attached to.
This is from the animationPlayer:
| /// <summary> |
| /// Gets the current bone transform matrices, in absolute format. |
| /// </summary> |
| public Matrix[] GetWorldTransforms() |
| { |
| return worldTransforms; |
| } |
| |
I think it's directly from the Skinned Model sample but I've written so much code it's hard to remember that far back.
I hope it helps.
Regards
**
Well on the way to creating a 3D First person controls shooter with Over the Shoulder view... Another few YEARS and it'll be done!
http://games.discoverthat.co.uk/ - Skinning Sample Dude for Blender and XNA Parallel Spilt Shadow Maps plus other stuff...
My game development blog - Well a few notes from time to time...