# Problems with rotation

This topic is 2728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a problem with respect to rotations of my model, how are the images below:

http://i.imagehost.org/0390/f_01.jpg
http://i.imagehost.org/0815/f_02.jpg

In my class Door:

class Door    {        public Model DoorModel;        public Vector3 Position;        public Vector3 Rotation;        public float Scale;        public Matrix[] bonesOfDoor;        public Matrix Clip01;        public Matrix Clip02;        public Matrix Clip03;        public Matrix Clip04;        public Matrix worldTranslation;                public Door()        {            DoorModel = null;            Position = Vector3.Zero;            Rotation = Vector3.Zero;            worldTranslation = Matrix.Identity;            Scale = 1.0f;            Clip01 = Matrix.Identity;            Clip02 = Matrix.Identity;            Clip03 = Matrix.Identity;            Clip04 = Matrix.Identity;        }    }

 protected override void LoadContent()        {            // Create a new SpriteBatch, which can be used to draw textures.            spriteBatch = new SpriteBatch(GraphicsDevice);            MyHome.HomeModel = Content.Load<Model>("Model/Home");            MyDoor.DoorModel = Content.Load<Model>("Model/D02");            MyDoor.worldTranslation = Matrix.CreateTranslation(                new Vector3(0.562f, 3.511f, 1.224f ));                        MyDoor.bonesOfDoor = new Matrix[MyDoor.DoorModel.Bones.Count];            MyDoor.Clip01 = MyDoor.DoorModel.Bones["pull"].Transform;                        MyDoor.Clip04 = MyDoor.DoorModel.Bones["D02"].Transform;}

My method Update:

protected override void Update(GameTime gameTime)        {            lastKeyboardState = currentKeyboardState;            currentKeyboardState = Keyboard.GetState();            // Allows the game to exit            if (currentKeyboardState.IsKeyDown(Keys.Escape))                Exit();            if (currentKeyboardState.IsKeyDown(Keys.O))            {                MyDoor.DoorModel.Bones["D02"].Transform = Matrix.CreateRotationZ(0.1f)                *MyDoor.Clip04;                MyDoor.DoorModel.Bones["pull"].Transform = Matrix.CreateRotationZ(0.5f)                   * MyDoor.Clip01;                           }            if( currentKeyboardState.IsKeyUp(Keys.O) )            {                MyDoor.DoorModel.Bones["D02"].Transform = Matrix.CreateRotationZ(0.0f)                    * MyDoor.Clip04;                MyDoor.DoorModel.Bones["pull"].Transform = Matrix.CreateRotationZ(0.0f)                   * MyDoor.Clip01;                                          }

And my method Draw:

private void DrawDoor(Door m, GameTime gameTime)        {            Matrix[] transform = new Matrix[m.DoorModel.Bones.Count];            m.DoorModel.CopyAbsoluteBoneTransformsTo(transform);            foreach (ModelMesh mesh in m.DoorModel.Meshes)            {                foreach (BasicEffect effect in mesh.Effects)                {                                      effect.EnableDefaultLighting();                                        effect.Parameters["World"].SetValue(Matrix.CreateScale(m.Scale) *                        transform[mesh.ParentBone.Index] * Matrix.CreateTranslation(m.Position));                    effect.Parameters["View"].SetValue(gameCamera.ViewMatrix);                    effect.Parameters["Projection"].SetValue(gameCamera.ProjectionMatrix);                }                mesh.Draw();            }        }

I used to model the port and the program in DirectX Truespace View the model is ok, even running animations. What I do not understand is what is wrong according to what is displayed above and because the animations do not play the right way when I load the model.
I do not know if it is correct or not, but what I read in several posts is that XNA does not play animations, this is done by code.
So my two questions are:

1 - How to play the animations in the right way in XNA?
2 - How do you get the door open so that correct?

[Edited by - SnakeBr on August 31, 2010 8:08:36 PM]

##### Share on other sites
Consider using matrices to do this instead. It seems over-the-top to use an animation for such a simple door.

Let mHingeAlign be a translation matrix that translates the door such that the y-axis runs through the pivot of the door (i.e., translate it such that the hinge upon which the door swings is the origin). Now, let mRotate be a y-axis rotation matrix with the angle θ. Finally, let mWorldTranslate be a translation matrix that places the door into world-space (i.e. puts it in the door frame).

Now, to open the door, you simply vary θ with time until it reaches a given angle (the door is fully open) and stop. You then do the same but in reverse when you want to close it, waiting for θ to return to the original value.

The world matrix of the door is given by mWorld = mHingeAlign * mRotate * mWorldTranslate

(first you translate the hinge to the origin, then you rotate the door about the hinge axis a given amount, then you place the door in the world)

I hope you'll give it a shot using matrices, because I think you'll find it much easier to manage once you have it set up.

##### Share on other sites
Sorry for the delay in responding, but I confess that I do not quite understand what you said.
Could you give me a minimal example of this explanation?
I tried and tried again but the result is always the same.

Sorry for not having understood.

##### Share on other sites
I managed to make the model of door stop spinning around the axis of the house (I had to encapsulate the model of the door with the model home in Truespace) but another problem arose, the model of the door does not rotate as a common port, ie, it now rotate on its axis X (tried others nothing worked, only to turn a little right with the X axis of the door).
Using the example of simple animation on this site I redid the class, but would like the axis of the port was shifted to the left as the door rotates from the middle of it.
I've tried everything, researched and can not see where I'm wrong, follows the code for those who have more experience than me tell me what is wrong because I can not understand what happens:

My class Door:

class Door     {         public Model ModelDoor;         public Vector3 Position;         public Vector3 Rotation;         public float Scale;                 public ModelBone DoorBone;         public Matrix mDoorTransform;         public Matrix[] BoneTransforms;         public Door()         {             ModelDoor = null;             Position = Vector3.Zero;             Rotation = Vector3.Zero;             Scale = 1.0f;                 }     }

protected override void LoadContent()         {             // Create a new SpriteBatch, which can be used to draw textures.             spriteBatch = new SpriteBatch(GraphicsDevice);                         myDoor.ModelDoor = Content.Load<Model>("Model/House");             myDoor.DoorBone = myDoor.ModelDoor.Bones["Door"];             myDoor.mDoorTransform = myDoor.DoorBone.Transform;             myDoor.BoneTransforms = new Matrix[myDoor.ModelDoor.Bones.Count];                             // TODO: use this.Content to load your game content here         }

My Update method:
( Note: Do not put a timer because it is just a test to see if the port rotated or not )

 Matrix DoorRotation = Matrix.CreateRotationX(MathHelper.ToRadians( -90.0f )); myDoor.DoorBone.Transform = DoorRotation * myDoor.mDoorTransform;

I also tried the update method after seeing what has gone wrong:

Matrix mHingeAling = new Matrix(); Matrix mRotation = new Matrix(); Matrix mWordlTranslate = new Matrix(); Vector3 Pivot = new Vector3(-0.800f, 0.0f, 0.0f); mHingeAling = Matrix.Identity; mRotation = Matrix.Identity; mWordlTranslate = Matrix.Identity;                   Matrix.CreateTranslation(ref Pivot, out mHingeAling); mRotation = Matrix.CreateRotationX(MathHelper.ToRadians(-90.0f)); Matrix.Multiply(mHingeAling, mRotation); myDoor.mDoorTransform = mRotation * myDoor.mDoorTransform;

I'm trying, unable to move the point of orgem door to the left side and get a correct rotation of the door.

In my method Draw:

private void DrawDoor(Door m)         {                  m.ModelDoor.CopyAbsoluteBoneTransformsTo(m.BoneTransforms );                                   foreach (ModelMesh mesh in m.ModelDoor.Meshes)             {                 foreach (BasicEffect effect in mesh.Effects)                 {                     effect.EnableDefaultLighting();                      effect.World =                             m.BoneTransforms[mesh.ParentBone.Index];                                     effect.Parameters["View"].SetValue(gameCamera.ViewMatrix);                     effect.Parameters["Projection"].SetValue(gameCamera.ProjectionMatrix);                 }                 mesh.Draw();             }

I do not understand why the model does not rotate correctly.