# Rotation

18 replies to this topic

### #1TheLionKing

Valued Member

• Members
• 143 posts

Posted 21 September 2003 - 06:29 PM

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

<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #2Dia

DevMaster Staff

• 1120 posts

Posted 22 September 2003 - 03:30 AM

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.

### #3anubis

Senior Member

• Members
• 2225 posts

Posted 22 September 2003 - 03:59 AM

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
If Prolog is the answer, what is the question ?

### #4TheLionKing

Valued Member

• Members
• 143 posts

Posted 22 September 2003 - 04:10 AM

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 ...!
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #5anubis

Senior Member

• Members
• 2225 posts

Posted 22 September 2003 - 04:13 AM

is the object located at the origin of the world ???
If Prolog is the answer, what is the question ?

### #6Dia

DevMaster Staff

• 1120 posts

Posted 22 September 2003 - 04:15 AM

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.

### #7anubis

Senior Member

• Members
• 2225 posts

Posted 22 September 2003 - 04:19 AM

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
If Prolog is the answer, what is the question ?

### #8TheLionKing

Valued Member

• Members
• 143 posts

Posted 22 September 2003 - 05:01 AM

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: !!!
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #9november9

New Member

• Members
• 4 posts

Posted 22 September 2003 - 08:56 AM

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

### #10TheLionKing

Valued Member

• Members
• 143 posts

Posted 22 September 2003 - 09:40 AM

It worked in Translation. And SDK documentation and other tutorials also mention it. :blush:
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #11anubis

Senior Member

• Members
• 2225 posts

Posted 22 September 2003 - 02:47 PM

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
If Prolog is the answer, what is the question ?

### #12TheLionKing

Valued Member

• Members
• 143 posts

Posted 22 September 2003 - 03:16 PM

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:
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #13anubis

Senior Member

• Members
• 2225 posts

Posted 22 September 2003 - 06:03 PM

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
If Prolog is the answer, what is the question ?

### #14TheLionKing

Valued Member

• Members
• 143 posts

Posted 23 September 2003 - 12:33 AM

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 ???
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #15TheLionKing

Valued Member

• Members
• 143 posts

Posted 23 September 2003 - 01:25 AM

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
}

<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #16Dia

DevMaster Staff

• 1120 posts

Posted 23 September 2003 - 01:47 AM

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() )

### #17TheLionKing

Valued Member

• Members
• 143 posts

Posted 23 September 2003 - 03:13 AM

: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: ?
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

### #18anubis

Senior Member

• Members
• 2225 posts

Posted 23 September 2003 - 03:51 AM

right...
If Prolog is the answer, what is the question ?

### #19TheLionKing

Valued Member

• Members
• 143 posts

Posted 23 September 2003 - 02:19 PM

: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: !
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users