d3d rotating triangle about its center

Started by
3 comments, last by GameDev.net 18 years, 10 months ago
I posted this on the maths and physics forum first but its here now too :) Anyway i'm made a direct3d prog and i'm trying to get a triangle to rotate and move about the screen. Its not quite working out though. Ive got a triangle with coords (100,-67), (-100, -66) and (0,133) which puts the center of rotation at (0,0). I blit it to the centre of the screen and it will rotate no problem. Moving it about the screen works sorta as well. the problem i'm having is combining the two. Here is my code for rotation and translation. void rotateLeft(float angle) { fRotationAngle += angle; D3DXMatrixTranslation(&matrixTranslation, -fCentreX, -fCentreY, 0.0f); D3DXMatrixRotationZ(&matrixRotationZ, fRotationAngle); D3DXMatrixTranslation(&matrixTranslationBack, +fCentreX, +fCentreY, 0.0f); resultantMatrix = matTranslation*matRotationZ*matTranslationBack; return; } void MoveForwards(void) { fCentreY += 1.0f; D3DXMatrixTranslation(&matrixTranslation,fCentreX,fCentreY,0.0f); resultantMatrix = matrixTranslation; } fCentreX and fCentreY are the coords of the centre of the triangle (starting at (0,0) and increasing/decreasing as necessary. - Whats happening is: I blit the triangle to the center of the screen, i can then rotate it no problem. I can move the triangle up the screen (only direction ive programmed in so far), but after that it will not rotate properly. After its been moved upscreen, calling the rotate function will instantly blit the triangle back to its original position (centre of the screen) and it will rotate it about the centre point of where the triangle should be (where it was after translation). Calling the moveforwards again will then put the triangle back to exactly where it was the last time the function ended. My rendering function is basically: Device->SetTransform(D3DTS_VIEW, &resultantMatrix); Device->SetStreamSource(0,pTriangleVB,0,sizeof(D3DVERTEX)); Device->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); I hope ive explained it clearly enough and i hope some one can offer me some advice where i'm going wrong.
Advertisement
Try translating the View Matrix (as you are already).
and rotating the World Matrix
resultantMatrix = matTranslation*matRotationZ*matTranslationBack;

You could simplify this by making it:
resultantMatrix = matRotationZ*matTranslation;

You need the TranslationBack matrix. Remember, matrix order is important. You can think about it in a left to right fashion. You rotate around the z axis with matRotationZ, then translate out to the matTranslation point.

EDIT: I think this article is good reading.
Chris ByersMicrosoft DirectX MVP - 2005
Ahaaaa thanks you two that seems to have almost solved it.

Its working almost perfectly, the only problem im having now is, after ive called MoveForwards (which translates it up the screen), calling rotate will jump the object up the screen by a few pixels (depending on how far its moved).

Thats probably a problem with my fCenterY variable going off somewhere..
I'll have to work on that more tomorrow (unless someones got more advice for me :P)

Thanks though guys!
The operation:

resultantMatrix = matRotationZ * matTranslation;

already combines translation and rotation. So you don't need to use your MoveForwards function.

This topic is closed to new replies.

Advertisement