4
\$\begingroup\$

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();
        }
    }
\$\endgroup\$
4
  • \$\begingroup\$ try to switch multiplication order like this: effect.World = Model1TransfoMatrix[0]*model2Transform; \$\endgroup\$
    – Vodáček
    Commented Dec 18, 2012 at 13:40
  • \$\begingroup\$ it's the same result sory \$\endgroup\$ Commented Dec 26, 2012 at 18:33
  • \$\begingroup\$ Is it possible to retrieve the current position and rotation of a bone in my model? \$\endgroup\$ Commented Dec 26, 2012 at 18:34
  • \$\begingroup\$ I think yes, from matrix, why are you using root bone instaned of bone of the right hand? Do you have image with result that you have? \$\endgroup\$
    – Vodáček
    Commented Dec 27, 2012 at 11:25

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .