How do I attatch "WeaponMachineGun" to "PlayerMarine"'s hand?

Jun 17, 2009 at 10:17 PM


Sorry about this, I'm sure you get asked this everyday...

I have the PlayerMarine and gun model that comes with the source, the animation runs fine but I having problems updating the position of the gun model so that it is attatched to the marine's hand.

I've tried loads of suggestions from these forums and others...yet the same problem exists: The gun model is in the wrong position and not moving...if I turn off the animations it fits perfectly to the static model's hand, so I must be using the wrong collection of bones. Have a look:


            for (int i = 0; i < this.SkinnedModel.SkeletonBones.Count; i++)
                if (this.SkinnedModel.SkeletonBones[i].Name == "R_Hand")
                    matrixHand = this.AnimationController.SkinnedBoneTransforms[i];
            Matrix transformedHand = matrixHand * this.World;

            //Animation.WeaponController.Update(gameTime.ElapsedGameTime, transformedHand);

            //Matrix transformedHand = this.AnimationController.poseTransform * this.AnimationController.SkinnedBoneTransforms[RIGHT_HAND_BONE_ID];// this.SkinnedModel.SkeletonBones[RIGHT_HAND_BONE_ID].InverseBindPoseTransform *
                           //  Matrix.CreateFromQuaternion(this.AnimationController.LocalBonePoses[RIGHT_HAND_BONE_ID].Orientation)
                           // * Matrix.CreateScale(this.AnimationController.LocalBonePoses[RIGHT_HAND_BONE_ID].Scale)
                           // * Matrix.CreateTranslation(this.AnimationController.LocalBonePoses[RIGHT_HAND_BONE_ID].Translation);
                            //* Matrix.CreateTranslation(Position);

            playerWeapon.Update(gameTime, transformedHand);


base.Update() updates the playerModel. The first section of the code I found on the xna forums and the commented out section I found on these forums... although none of them work?? Maybe my world matrix is wrong?

I'll try and post a pic of what is happening when I run it.

Any help Appreciated!

Cheers,                                                                                                                                                                                                                                                                                                                                                          Evan.

Jun 18, 2009 at 7:12 AM

I struggled a while to, but now I got a version that works for me:

        void CalculateTransform()
            Transformation = BindPoseTransform
                * SkeletalModel.AnimationController.SkinnedBoneTransforms[BoneIndex]
                * Matrix.CreateScale( ParentScale.Scale )
                * ParentRotation.Rotation
                * Matrix.CreateTranslation( ParentPosition.Position );

            Transformation.Decompose( out scale, out rotation, out position );

BindPoseTransform is only computed at load time:

            BindPoseTransform = Matrix.Invert( SkeletalModel.Model.SkeletonBones[BoneIndex].InverseBindPoseTransform );

            //As an optimization, we multiply the offset values with BindPoseTransform:
            BindPoseTransform = rotOffset * BindPoseTransform * Matrix.CreateTranslation( posOffset );


Note: the ParentPosition, -Rotation and -Scale are from our hierachical component based system.
In your case these will probably be something like charcter position, -rotation and -scale.