Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

http://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();
        }
    }
share|improve this question
    
try to switch multiplication order like this: effect.World = Model1TransfoMatrix[0]*model2Transform; –  Vodáček Dec 18 '12 at 13:40
    
it's the same result sory –  Mehdi Bugnard Dec 26 '12 at 18:33
    
Is it possible to retrieve the current position and rotation of a bone in my model? –  Mehdi Bugnard Dec 26 '12 at 18:34
    
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? –  Vodáček Dec 27 '12 at 11:25
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
yess thank'sss ! –  Mehdi Bugnard Mar 14 '13 at 13:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.