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.

So, I have a Model stored in MyModel, that is made from three meshes. If you loop thrue MyModel.Meshes the first two are children of the third one. And was just wondering, if anyone could tell me where is the problem with my code.

This method is called whenever I want to programmaticly change the position of a whole model:

public void ChangePosition(Vector3 newPos)
    Position = newPos;

    MyModel.Root.Transform = Matrix.CreateScale(VectorMathHelper.VectorMath(CurrentSize, DefaultSize, '/')) * Matrix.CreateFromAxisAngle(MyModel.Root.Transform.Up, MathHelper.ToRadians(Rotation.Y)) * 
        Matrix.CreateFromAxisAngle(MyModel.Root.Transform.Right, MathHelper.ToRadians(Rotation.X)) * Matrix.CreateFromAxisAngle(MyModel.Root.Transform.Forward, MathHelper.ToRadians(Rotation.Z)) * 

    Matrix[] transforms = new Matrix[MyModel.Bones.Count];

    int count = transforms.Length - 1;
    foreach (ModelMesh mesh in MyModel.Meshes)
        mesh.ParentBone.Transform = transforms[count];

This is the draw method:

foreach (ModelMesh mesh in MyModel.Meshes)
    foreach (BasicEffect effect in mesh.Effects)
        effect.View = camera.view;
        effect.Projection = camera.projection;
        effect.World = mesh.ParentBone.Transform;

The thing is when I call ChangePosition() the first time everything works perfectlly, but as soon as I call it again and again. The first two meshes(children meshes) start to move away from the parent mesh.

Another thing I wanted to ask, if I change the scale/rotation/position of a children mesh, and then do CopyAbsoluteBoneTransforms() will children meshes be positioned properlly(at the proper distance) or would achieving that require more math/methods?

Thanks in advance

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

if you want change the whole model position that is not the code:

   // Copy any parent transforms.
    Matrix[] transforms = new Matrix[myModel.Bones.Count];

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in myModel.Meshes)
        // This is where the mesh orientation is set, as well 
        // as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
            effect.World = transforms[mesh.ParentBone.Index] * 
                * Matrix.CreateTranslation(modelPosition);

for moving other meshes... you have to change the bone... but it maybe quite hardcore... is better to use an animation tool

Keep in mind that the Bone transform is relative to its parent bone transform and play with it... You can do something like this:

        Vector3 scale, traslation;
        Quaternion rotation;

        myModel.Bones[4].Transform.Decompose( out scale, out rotation, out traslation );

        rotation *= Quaternion.CreateFromAxisAngle( Vector3.UnitY, ( float ) gameTime.TotalGameTime.TotalSeconds / (2 * MathHelper.Pi) / 8 );

        myModel.Bones[4].Transform = Matrix.CreateScale( scale ) * Matrix.CreateFromQuaternion( rotation ) * Matrix.CreateTranslation( traslation );
share|improve this answer
Thahks, this works! :D –  user1806687 Nov 24 '12 at 10:45
add comment

Your Answer


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.