Rotation

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 21, 2003 at 18:29

Hi,

Rotation is something that is very easy to understand, but it is giving me a really big headick now. :blink:

My problem is When I rotate on X or Y Axis by using D3DXMatrixRotationX() or Y() it rotated around the screen not at a point. Rotating on Z rotates it on the point.

I have coded before but now things are getting a bit messy. When ever I use the code that I use for Translation of Objects, then these things happen. Everything seems to be fine :angry: .

It also happened when I was coding in OpenGL a month back. But I accomplished this task using Push and Pop Stack Layers :blush: .

Is there anything like this in Direct3D9?

The code is fine … ;7

D3DXMATRIXA16 World; // World CoOrdinates
float Angle = 0.0f; // Angle of Rotation

D3DXMatrixRotationY (&World, Angle); // Set Rotation on Y-Axis
D3DDevice->SetTransform (D3DTS_WORLD, &World); // Transform

Angle += 0.05f; // Increment in Angle

Please help :confused: !!!

18 Replies

Please log in or register to post a reply.

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 22, 2003 at 03:30

It depends on where the camera is located. What you are doing is rotating around the camera. Are you doing any translation? If you want to rotate around a point, then you’ll need to do the rotation and then do the translation and not the opposite.

As for glPushMatrix(), I don’t think there’s an equivalent function in DirectX.

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 22, 2003 at 03:59

i guess there is a way to load matrices in directx. it should be very easy to implement your own matrix stack if you really need to do that. i think i even saw that done once in a rendering api that supported both dX and oGL

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 22, 2003 at 04:10

I am learning Dx9, so I don’t know much of work for DX :confused: .

I had done translation before with the same code. What I did was I used the code that I used for translation and removed it and implement rotation at that place.

Rotation on Z Axis seems to work fine … but X & Y works like I am using translation also …? They are rotating around the axis :eek: .

For the camera, it is fixed and I am not moving it …!

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 22, 2003 at 04:13

is the object located at the origin of the world ???

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 22, 2003 at 04:15

Well, since you are doing translation, I guess that’s why. Your object is not located in the origin and that’s why it rotating around the screen. You have to first do the rotation and then do that translation afterwards.

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 22, 2003 at 04:19

i think he meant that he replaced the translation code with the rotation. my guess is that the object isn’t located at the origin and that he somehow rotates the object around the origin

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 22, 2003 at 05:01

I am not doing any translation of any sort here … I am just using rotation and I am changing the Worl Coordinates after doing rotation. I had the same idea as you guys … but if I was doing translation too then if I use Z - Axis rotation with translation … the object should have done zooming in and out with movement … but it is not. :eek:

Rotating it on Z - Axis just rotates it around the Z - Axis with top point as its center … where as on X & Y it rotates around the screen with the center of the screen as its center …! :confused:

I think I am explaining right :sigh: !!!

1a4db177cdeb3cc98de534ac46f7564e
0
november9 101 Sep 22, 2003 at 08:56

Have you tried defining your world matrix as a D3DXMATRIX, I’ve never seen it defined as a D3DXMATRIXA16?

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 22, 2003 at 09:40

It worked in Translation. And SDK documentation and other tutorials also mention it. :blush:

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 22, 2003 at 14:47

are you rendering a 3ds model or something like that ??? sometimes people don’t care to center their models around the origin. so while correctly rotating the object around the origin in objectspace it would look like the model gets rotated around the origin of the world if you try to place the object there

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 22, 2003 at 15:16

I am just drawing a simple triangle with three vertices.

First is the Bottom Left.
Second is the Bottom Right.
Third is the Top one.

I have even tried changing their orders. :confused:

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 22, 2003 at 18:03

could you post the coordinates of your triangle… i don’t quite get this… if you do no translation right now and the triangle is centered. how could it rotate around anything else but it’s origin

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 23, 2003 at 00:33
sVERTEX Triangle [3] = {
                         {-0.5f, -0.5f, 2.5f, D3DCOLOR_XRGB (255, 0, 0)}, // Bottom  Left Point
                         { 0.5f, -0.5f, 2.5f, D3DCOLOR_XRGB (0, 255, 0)}, // Bottom Right Point
                         { 0.0f,  0.5f, 2.5f, D3DCOLOR_XRGB (0, 0, 255)}, // Top Point
                        };

Is there any way to set center of origin for this triangle ???

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 23, 2003 at 01:25

I am setting 3 types of transformations i.e. Projection, View & World. Code is below :

Projection

float AspectRatio = (float)Width / Height; // Ratio between Width & Height
 // Initialize Perspective View for the Screen
 D3DXMatrixPerspectiveFovLH (&Projection, // Projection Matrix
               1.0f,    // Field of View
               AspectRatio, // Aspect Ratio of the Screen
               0.0f,    // Nearest Clipping Plane
               100.0f);   // Farthest Clipping Plane
 
 // Setup Projection Matrix for the Screen
 Graphics->SetTransform (D3DTS_PROJECTION, &Projection);

View

D3DXMATRIXA16 Camera; // Camera
 
 D3DXVECTOR3 Position  (0.0f, 0.0f, -2.0f); // Position of the Camera
 D3DXVECTOR3 Direction  (0.0f, 0.0f, 0.0f); // Direction of the Camera Looking at
 D3DXVECTOR3 UpDirection (0.0f, 1.0f, 0.0f); // Upward Direction for the Camera
 
 // Initialize Camera for the Game
 D3DXMatrixLookAtLH (&Camera,    // Camera
           &Position,   // Position of the Camera
           &Direction,  // Direction of the Camera Looking at
           &UpDirection); // Upward Direction of the Camera
 
 // Setup Camera on the Screen
 Graphics->SetTransform (D3DTS_VIEW, &Camera);

Rotation is the same as shown before, but here is the code that does the drawing :

static float Angle = 0.0f;   // Angle of Rotation
 
 KeysPressed (); // Check for the Keys pressed and process them
 
 D3DXMatrixRotationX (&World, Angle);
 
 // Transform the CoOrdinates according to the World CoOrdinates
 Graphics->SetTransform (D3DTS_WORLD, &World);
 
 Graphics->Clear (0,            // Number of Areas
         NULL,          // Areas to clear
         D3DCLEAR_TARGET |    // Clear the Screen with the Colour defined below
         D3DCLEAR_ZBUFFER,    // Clear the Depth Buffer with the value given below
         D3DCOLOR_XRGB (0, 0, 0), // Colour of the Screen
         1.0f,          // Dpeth (Z) Buffer
         0);           // Stencil Buffer
 
 Graphics->BeginScene (); // Begin drawing the Scene
   
   // Bind the Vertex Buffer with the Stream Data
   Graphics->SetStreamSource (0,         // Data Stream Starting Number
                TriangleVB,    // Vertex Buffer for the Triangle
                0,         // Offset
                sizeof (sVERTEX)); // Size of the Vertex Structure
   
   // Draw
   Graphics->DrawPrimitive (D3DPT_TRIANGLELIST, // Type of Vertex (Triangle with 3 Vertices)
               0,         // Index Number of Vertex to begin with
               1);         // Number of Vertex to Draw
 
 Graphics->EndScene (); // Finish drawing the Scene
 
 // Present Back Buffer contents on the Screen
 Graphics->Present (NULL, NULL, NULL, NULL);
 
 Angle += 0.05f;
 
 if (Angle >= 360.0f)
   {
   Angle = 0.0f; // Set Angle back to 0 degrees
   }

Please help :confused: !

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 23, 2003 at 01:47

Your triangle is not in the origin!! (i.e. it is not centered)
As you can see, it is “translated” -2.5 in the z axis.

What you need to do is draw the triangle at the center (i.e. z = 0), do the rotation, and then translate 2.5 in z axis (using D3DXMatrixTranslate() )

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 23, 2003 at 03:13

:rolleyes: Can it be? I was rotating the world around 0 all the time while drawing it around 2.5 Axis?

Maybe you are right. I will try it out when I will go home, I have just reached office now.

I am stupid :lol: ? Why didn’t I think of that before?

So the same thing is with OpenGL right :blush: ?

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 23, 2003 at 03:51

right…

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 23, 2003 at 14:19

:yes: Thanks guys! It worked :blush: !

It was just a minor mistake but it gave me a real big headick … :lol: !

I never knew about that … THANKS A LOT GUYS :yes: !