# D3DXMatrixRotationYawPitchRoll Question

This topic is 5465 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 class that rotates a 2D Rectangle with the orgin up in the top left corner. I need it to rotate in the center of the rectangle. Is there any way to do this?

##### Share on other sites
Translate so the origin is at the centre, then rotate, then translate back again.

##### Share on other sites
How do you translate so the orgin is at the center. If i translate to the center coordinates then the top left will be in the center and move the rectangle, but then it would still rotate from the top left. then when I translate it back it would be like my problem before it rotates from the top left.

##### Share on other sites
simple...use D3DXComputeBoundingBox().
ex (where m_Box is a class that holds a LPD3DXMESH )

BYTE *ppData = NULL;
DWORD dwNumberOfVerts = 0, dwStride = 0;
D3DXVECTOR3 topRight, lowLeft, *firstPos = NULL, vecCenter;

dwNumberOfVerts = m_Viper->GetNumVertices();
dwStride = D3DXGetFVFVertexSize( m_Viper->GetMesh()->GetFVF() );

firstPos = (D3DXVECTOR3*)ppData;

hr = D3DXComputeBoundingBox( firstPos, dwNumberOfVerts, dwStride, &lowLeft, &topRight );
if( FAILED( hr ) )
{
return hr;
}

then use the midpoint theorem to calculate the center.

D3DXVECTOR3 center = D3DXVECTOR3( center.x = (topRight.x - lowLeft.x)/2, center.x = (topRight.y - lowLeft.y)/2, center.x = (topRight.z - lowLeft.z)/2 );

then use D3DXMatrixTranslation()....

D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, fCenter.x, fCenter.y, fCenter.z );

note that I have not tested this code...but it should work.
hope that helps

##### Share on other sites
on this line...

dwStride = D3DXGetFVFVertexSize( m_Viper->GetMesh()->GetFVF() );

replace m_Viper with m_Box..so it should look like this...

dwStride = D3DXGetFVFVertexSize( m_Box->GetMesh()->GetFVF() );

..guess thats what u get from copy and paste.

##### Share on other sites
Or you could just use D3DXMatrixTransformation2D() and specify a rotation center =)

If you use ID3DXSprite it's even simpler.

##### Share on other sites
How do I use D3DXMatrixTransformation2D? I get this error C2065: 'D3DXMatrixTransformation2D' : undeclared identifier.
I've linked the libraries and header files. I can use other D3DX functions.
And how do I use the sprite transformation, all I found on msdn was a SetTransform that only sets position.

##### Share on other sites
[\quote] How do I use D3DXMatrixTransformation2D?[\quote]

I don't think that there is such a function in D3D8, but I cannot say if D3D9 has it.

To rotate your rectangle, you must "simply" do this:
- Move it with its center to the coordinate origin (0, 0, 0). That is, you must use D3DXMatrixTranslation() to build a translation matrix with the movement (you need to move it in 2 dimensions only, so I think in your case only moving on x and y axis is necessary, you should set the movement on z axis to 0).
- Then you must rotate the rectangle around the z axis with the angle you wish. To built a z axis rotation matrix use D3DXMatrixRotationZ().
- You need to translate it back if its off-center, so you must built a translation matrix again with the same movements in x and y axis (with reversed sign).
- After you created these matrixes you must multiply them together, in the order the transformations should be executed: (Translation - Rotation - Translation)
Use D3DXMatrixMultiply().
- At last, you must find a way to draw it: You can easily cast a D3DXMatrix to a D3DMATRIX and set it with SetTransform():

Device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &TransformationMatrix);

EDIT: Of course, you must draw your rectangle afterwards, using Device->DrawPrimitive() e.g.
If this is not clear enough I can post code that does this, it is not really much.

I hope this helps.

##### Share on other sites
Thanks for the post, but Im really new at this and I suck at math so can you please post your code? thanks

##### Share on other sites
Yes I can:

here is the Render-Function (not tested):

#define VERTEX D3DFVF_XYZ

struct D3DVERTEX
{
float x, y, z;
};

bool Render(LPDIRECT3DDEVICE8 pDevice)
{

D3DVERTEX Rectangle[4];

// this rectangle lies on the x-y-plane (all z coordinates of
// the points are 0)

Rectangle[0].x = 0;
Rectangle[0].y = 0;
Rectangle[0].z = 0;

Rectangle[1].x = 2;
Rectangle[1].y = 0;
Rectangle[1].z = 0;

Rectangle[2].x = 2;
Rectangle[2].y = -1;
Rectangle[2].z = 0;

Rectangle[3].x = 0;
Rectangle[3].y = -1;
Rectangle[3].z = 0;

// now you must shift the rectangle to the coordinate centre
// (0, 0, 0). The centre of the rectangle is (1, -0.5, 0). So
// you must shift it -1 into x-direction and 0.5 into y-
// direction.

D3DXMATRIX World, Translation, Rotation, AntiTransl, Temp;

D3DXMatrixTranslation(&Translation, -1, 0.5, 0);

// Now rotate it, lets say around 45°
D3DXMatrixRotationZ(&Rotation, PI / 4);

// Shift it back to where it originally was, AND maybe into
// depth so projection doesn't skip it
D3DXMatrixTranslation(&AntiTransl, 1, -0.5, 4);

// Multiply these matrices together
D3DXMatrixMultiply(&Temp, &Translation, &Rotation);
D3DXMatrixMultiply(&World, &Temp, &AntiTransl);

// Don't cull
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
// Set the FVF
// Set the transformation
pDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &World);

pDevice->BeginScene();
// Render the rectangle
pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &Rectangle,
sizeof(D3DVERTEX));
pDevice->EndScene();

return true;
}

int WinMain(....)
{
// Create the main object
// Create the device here
// Create and Set projection here

// Render-loop
...
Render(pDevice);
...

return 0;
}

If you don't know how to create main object, device, projection and set up the window / loops, you should refer to the D3D8 docu, this would simply take too long here.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 13
• 14
• 45
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!