Friday, December 17, 2010

XNA 3d translation, rotation and scaling, Windows Phone 7

I had a several cubes in a Windows Phone 7 XNA application. I wanted to animate the cubes by rotating them on their axis. Let us say the cube is at Vector3 (-offsetx, –offsety,0f) position.

I used the following code to spin it on its own axis it   worked fine. 

 rotAxis = new Vector3(-offsetx, -offsety, 0f);
worldlist[iworld] = Matrix.CreateFromAxisAngle(rotAxis, angle) * rectangleTransform;

The rectangle transform brought it to where I wanted it. However I ran into problems when I wanted to spin it any other axis using rotaxis for example

 rotAxis = new Vector3(1.0f, 0f, 0f);

Then I saw this nice article   Rotations and translations  by Riemer. By the way if you want to do something in XNA you have to read the great tutorials in this site. His XNA book is also great.

So I did the following code where I translated the cube to move it  to center. Then I can spin the cube  on any axis!

 rotAxis = new Vector3(1.0f, 0f, 0f);
worldlist[Selectcount] = Matrix.CreateTranslation(new Vector3(-offsetx, -offsety, 0f ))
*Matrix.CreateFromAxisAngle(rotAxis, angle)*rectangleTransform ;

1 comment: