XNA Model reseting BasicEffect matrix

Started by
9 comments, last by Kaze 14 years, 2 months ago
/// <summary>
/// simple rigid animation test based of xna tutorial
/// </summary>
public class Rig2
{
public Rig2(ContentManager content)
{
World = Matrix.Identity;
//get model from content manager
this.XnaModel = content.Load<Model>("rig2");

//get bone we want to move
Bone1 = this.XnaModel.Bones["Cube.002"];
//copy transform so we can move it realative to its original
//Transfomation rather than identity.
//***Problem part 1***
//We get our local copy from the model so if we call this later after another
//instance of this class has altered it the animation will be messed up
Bone1Transform = Bone1.Transform;
}
public Model XnaModel { get; private set; }
public ModelBone Bone1 { get; private set; }
public Matrix Bone1Transform { get; private set; }
public Matrix World { get; set; }

float ang = 0;
public void Update(GameTime time) {
ang += (float)time.ElapsedGameTime.TotalSeconds;
//rotate bone relative to its original transform and
//save the new transform to the bone
//***Problem part 2***
//We have to save the new transform back to the bone to
//calaculate the absolute transforms in the draw method
Bone1.Transform = Matrix.CreateRotationY(ang) * Bone1Transform;
}

public void Draw( Matrix view, Matrix projection)
{
//this is almost identical to the microsoft model rendering tutorials
Matrix[] transforms = new Matrix[XnaModel.Bones.Count];
XnaModel.CopyAbsoluteBoneTransformsTo(transforms);


foreach (ModelMesh mesh in XnaModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();

effect.View = view;
effect.Projection = projection;
effect.World =
transforms[mesh.ParentBone.Index] * World;

}
mesh.Draw();
}

}
}

This topic is closed to new replies.

Advertisement