Rotation about the center of a triangle. Stuck

Started by
1 comment, last by jgoemat 18 years, 10 months ago
OK i'm trying to get this but its not quite working out. Using direct3D and C++. Ive got a triangle drawn onscreen with coords (-100, -67), (0, 133), (100, -67) which puts the centre of gravity (rotation point) at approx (0,0). If the triangle stays stationary i can rotate it about its centre without a problem using matrices D3DXMatrixRotationZ(&matrixRotationZ, fRotationAngle); The problem i'm having is when it has been moved about on screen to another location. Im trying to translate it to the origin, rotate it, then rotate it, then translate it back but it doesnt quite work and ends up rotating about a random point. This is what im doing: D3DXMatrixTranslation(&matrixTranslation, -fCentreX, -fCentreY, 0.0f); //puts it to the origin D3DXMatrixRotationZ(&matrixRotationZ, fRotation); //rotates D3DXMatrixTranslation(&matrixTranslationBack, +fCentreX, +fCentreY, 0.0f); //puts it back resultantMatrix = matrixTranslation*matrixRotationZ*matrixTranslationBack fCentreX and fCentreY is the centre position of the triangle, starts at (0,0) and get increased or decreased as the triangle moves about. what it actually does is messes with the position of the triangle and rotates it about a wrong origin, then if i try to translate it again it seemingly ignores the position and rotation and goes back to where it was last time i called the translate function. Any ideas what im doing wrong? I put it on this board because im guessing its a problem with my math and not strictly my code.
Advertisement
Woah! No need for all that!

If you want to translate the triangle then rotate it, you do exactly that. Theres no need to move it back to the origin. The thing that you're having trouble with is matrix ordering. Transforms are applied to the geometry in the same order that they are multiplied.

So, if you wanted to move the triangle forward on an angle, you'd rotate then translate.

If you wanted to move the triangle to a point, then turn it, you'd translate then rotate.
I think your code should work, IF you had already moved the triangle to fCentreX, fCentreY. If the triangle's points haven't been moved by fCentreX, fCentreY already though, then your first translation will move them away from the origin.

Let's say you have one point at (1.0, 0.0, 0.0) and you want to move it +10X and +10Y, and rotate it about the center by 180 degrees. I think the result you are expecting is that the new point would be at (9.0, 10.0, 0.0), right? If you take the original point however:

1) Translate to (-9, -10, 0)
2) Rotate to (9, 10, 0)
3) Translate to (19, 20, 0)

I would keep the points that define your triangle relative to the origin, then apply just the second two matrices to rotate it first then move it out into the world:

1) skipped, triangle still at (1, 0, 0)
2) Rotate to (-1, 0, 0)
3) Translate to (9, 10, 0)

Your way that should work:
1) Translate to (11, 10, 0) (done ahead of time)
2) Translate to (1, 0, 0) (back at origin)
3) Rotate to (-1, 0, 0)
4) Translate to (9, 10, 0) (rotated about (10, 10, 0))

If you want to move the triangle 5 units to the right:
5) Translate to (14, 10, 0) (center is now (15, 10, 0))

Now if you want to rotate again, the center is (15, 10, 0)
6) Translate to (-1, 0, 0) (back to origin)
7) Rotate to (1, 0, 0)
8) Translate to (16, 10, 0)

Debug your program and try executing each of the matrices in order instead of combining them and you should be able to figure out which step is causing the problem.

This topic is closed to new replies.

Advertisement