XNA- Transforming children

Posted by user1806687 on Game Development See other posts from Game Development or by user1806687
Published on 2012-11-23T21:10:46Z Indexed on 2012/11/23 23:22 UTC
Read the original article Hit count: 218

Filed under:
|
|

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

© Game Development or respective owner

Related posts about XNA

Related posts about c#