0
\$\begingroup\$

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.CreateTranslation(Position);

    Matrix[] transforms = new Matrix[MyModel.Bones.Count];
    MyModel.CopyAbsoluteBoneTransformsTo(transforms);

    int count = transforms.Length - 1;
    foreach (ModelMesh mesh in MyModel.Meshes)
    {
        mesh.ParentBone.Transform = transforms[count];
        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;
        effect.EnableDefaultLighting();
    }
    mesh.Draw();
}

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

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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

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

    // 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.CreateRotationY(modelRotation)
                * 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 );
\$\endgroup\$
1
  • \$\begingroup\$ Thahks, this works! :D \$\endgroup\$ Nov 24, 2012 at 10:45

You must log in to answer this question.

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