Wednesday, January 8, 2020

xna - How attach a model with another model on a specific bone?


I meet a difficulty attached to a model to another model on a "bone" accurate. I searched several forums but no result. I saw that many people have asked the same question but no real result see no response. Thread found :


How to attach two XNA models together?



How can I attach a model to the bone of another model?


https://stackoverflow.com/questions/11391852/attach-model-xna


But I think it is possible.


Here is my code example attached a "object" of the hand of my player


private void draw_itemActionAttached(Model modelInUse)
{
Matrix[] Model1TransfoMatrix = new Matrix[this.player.Model.Bones.Count];
this.player.Model.CopyAbsoluteBoneTransformsTo(Model1TransfoMatrix);
foreach (ModelMesh mesh in modelInUse.Meshes)
{

foreach (BasicEffect effect in mesh.Effects)
{
Matrix model2Transform = Matrix.CreateScale(1f) * Matrix.CreateFromYawPitchRoll(0, 0, 0);

effect.World = model2Transform * Model1TransfoMatrix[0]; //root bone index
effect.View = arcadia.camera.View;
effect.Projection = arcadia.camera.Projection;
}
mesh.Draw();
}

}

Answer



If you use this approach, you need to multiply the world matrix in the effect with the world matrix of the bone of model2:


private void draw_itemActionAttached(Model modelInUse)
{
Matrix[] Model1TransfoMatrix = new Matrix[this.player.Model.Bones.Count];
Matrix[] Model2TransfoMatrix = new Matrix[modelInUse.Bones.Count];
this.player.Model.CopyAbsoluteBoneTransformsTo(Model1TransfoMatrix);
modelInUse.CopyAbsoluteBoneTransformsTo(Model2TransfoMatrix);
foreach (ModelMesh mesh in modelInUse.Meshes)

{
foreach (BasicEffect effect in mesh.Effects)
{
Matrix model2Transform = Matrix.CreateScale(1f) * Matrix.CreateFromYawPitchRoll(0, 0, 0);

effect.World = Model1TransfoMatrix[0] * Model2TransfoMatrix[mesh.ParentBone.Index] * model2Transform;
effect.View = arcadia.camera.View;
effect.Projection = arcadia.camera.Projection;
}
mesh.Draw();

}
}

Also you should multiply the matrices in this order to get the best results. If you want to save some time you also could remove the model2Transform matrix because it is the identity matrix and thus changes nothing but the time needed to multiply the matrices.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...