This project is read-only.

modifying the world matrix

Apr 16, 2009 at 1:42 AM
Edited Apr 16, 2009 at 5:02 AM
I managed to get a sample model and animation loaded into my game, however I am not quite sure how to move and rotate the model around after that. In my draw code, I tried using effect.World like I would in the standard XNA pipeline but it seems to have no effect. I've only been working with XNA and C# for about a week so...if anyone could help I would appreciate it. Here is my draw method in my model's class.

 public void Draw(Camera camera, AnimationController animationController)
        {
            foreach (ModelMesh modelMesh in model.Model.Meshes)
            {
                foreach (SkinnedModelBasicEffect effect in modelMesh.Effects)
                {
                    // Set the animated skeleton from the animation controller
                    effect.Bones = animationController.SkinnedBoneTransforms;

                    // Set the camera
                    effect.View = camera.view;

                    effect.Projection = camera.projection;
             
                    effect.World = rotation * Matrix.CreateTranslation(position);
                
                    //angle modified by left/right arrow keys
                    rotation2 = Matrix.CreateFromAxisAngle(Vector3.Up, angle);
                    forward = Vector3.Transform(Vector3.Forward, rotation2);        

              
                    effect.Material.DiffuseColor = new Vector3(0.8f);
                    effect.AmbientLightColor = new Vector3(0.2f);
                    effect.LightEnabled = true;
                    effect.EnabledLights = EnabledLights.Two;
                    effect.PointLights[0].Color = Vector3.One;
                    effect.PointLights[0].Position = new Vector3(100);
                }
                modelMesh.Draw();
            }

and my update method:

public void Update()
        {
            float rot = 0.0f;
                       
            KeyboardState kb = Keyboard.GetState();
            if(kb.IsKeyDown(Keys.Left))
            {
                rot += 0.1f;
                angle += 0.1f;
                rotation *= Matrix.CreateRotationY(rot);
            }
            if (kb.IsKeyDown(Keys.Right))
            {
                rot -= 0.1f;
                angle -=0.1f;
                rotation *= Matrix.CreateRotationY(rot);
            }
            if(kb.IsKeyDown(Keys.Up))
            {
                velocity = new Vector3(10.0f, 10.0f, 10.0f);
                position -= velocity * forward;
            }
            if (kb.IsKeyDown(Keys.Down))
            {
                velocity = new Vector3(10.0f, 10.0f, 10.0f);
                position += velocity * forward;
            }
           
        }
Apr 16, 2009 at 4:13 PM
Im not too sure on this but i think you need to times by the world matrix and for the movement of position use the CreateTranslation matrix EG:
public void Update()
        {
            float rot = 0.0f;
                       
            KeyboardState kb = Keyboard.GetState();
            if(kb.IsKeyDown(Keys.Left))
            {
                rot += 0.1f;
                angle += 0.1f;
                world *= Matrix.CreateRotationY(rot);
            }
            if (kb.IsKeyDown(Keys.Right))
            {
                rot -= 0.1f;
                angle -=0.1f;
                world *= Matrix.CreateRotationY(rot);
            }
            if(kb.IsKeyDown(Keys.Up))
            {
                velocity = new Vector3(10.0f, 10.0f, 10.0f);
                position -= velocity * forward;
                world *=  Matrix.CreateTranslation(position);
            }
            if (kb.IsKeyDown(Keys.Down))
            {
                velocity = new Vector3(10.0f, 10.0f, 10.0f);
                position += velocity * forward;
                world*= Matrix.CreateTranslation(position);
            }
           
        }

I aint tested this code, but i hope it works :)
Apr 16, 2009 at 4:41 PM
Thanks for the reply ymor but it turns out my problem was in the way that my class manager called the update method of this class, its fixed and working now.

However about what you said...that is something I still don't really understand. In some tutorials I've followed, the translations/rotations are done by multiplying the world matrix by the new rotation or translation..however in this particular case if I multiply it I will get unexepected results that don't work out, but if I just set it to equal the new rotation/translation it works fine.
Apr 16, 2009 at 4:58 PM
Ah yes you are also updating the world matrix in your draw method if you used the code i posted, thats why it would output unexpected results. Sorry :x
Glad you got it working though :D