# Thread: Rotating a moving object

1. ## Rotating a moving object

Hi,

I'm encountering the following problem. I've got a dinosaur which I'd like to rotate around a point that is not the origin. Well that's not the problem, but here it comes.

I'm moving the dinosaur using the numeric keyboard, 4+6 rotates, 8 moves, 2 stops the dinosaur. I cannot rotate the dinosaur and then translate it into it's correct position. This is my code:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

gluLookAt(camera[0], camera[1], camera[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

drawEnvironment();

if (move)
moveDino();

glTranslatef(10.5, 0.0, 1.55);
glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(-10.5, 0.0, -1.55);

drawDino();

glFlush();
glutSwapBuffers();
}

void keyboard(unsigned char key, int posx, int posy)
{
if (tolower(key) == 'q')
exit(0);

if (key == '8')
move = GL_TRUE;
else if (key == '4')
rotateDino(left);
else if (key == '6')
rotateDino(right);
else if (key == '2')
move = GL_FALSE;
}

---

As you can see, I try to rotate the dino itself around the y-axis at the point (10.5, 0.0, 1.55). But I have to translate the dino to the position (dino_pos[0], dino_pos[1], dino_pos[2]). I've tried to add those coordinates to the rotation point, but that doesn't work, at least not in the way I'm doing it .

2. ## Re: Rotating a moving object

You always rotate around the origin. But you can fake a rotation around an arbitary point by moving the objects to make the new center point to be at the origin.
So if you want to rotate around (10.5,0.0,-1.5) then move your dino with (-10.5,0.0,1.5), rotate it, then move back by (10.5,0.0,-1.5) thus you've rotated it around the new center point

Hope this helps
FooLman

3. ## Re: Rotating a moving object

The problem is translating the object to another position than (10.5, 0.0, 1.55). If I do that, the dino is making circles instead of rotating on the same position.

4. ## Re: Rotating a moving object

Hello !

Try this :

...
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(angle,0,1,0);
drawDino();
glPopMatrix();
...

I'm not home so I can't try it but it might work...

Osku

5. ## Re: Rotating a moving object

but after the rotation the dino has to be translated into its current position. That's the tricky business, at least for me...

6. ## Re: Rotating a moving object

Code :
```glTranslatef(10.5, 0.0, 1.55);
glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(-10.5, 0.0, -1.55);```
This code makes no sence to me.
I am not sure but i think it's the same as
glRotatef(angle, 0.0, 1.0, 0.0) only.

Maybe You need something like this ...
Code :
```glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(10.5, 0.0, 1.55);
drawDino();
glTranslatef(-10.5, 0.0, -1.55);```
[CS]

7. ## Re: Rotating a moving object

I think this is what you need

getNewPos()
{
float mat[4][4];
glGetFloatv(GL_MODELVIEW_MATRIX,(float *)mat);

Z+=deltaz*mat[2][2]+deltay*mat[2][1]+deltax*mat[2][0];
Y-=deltaz*mat[1][2]+deltay*mat[1][1]+deltax*mat[1][0];
X+=deltaz*mat[0][2]+deltay*mat[0][1]+deltax*mat[0][0];
}

where Z is your original z position
X is your original x position
and Y is your original y position
and the deltas are the speed you want your dino to move ....

run this everyframe (or time step or whatever)

then in the display func do this

glRotatef(-roll, 0,0,1);
glRotatef(pitch,1,0,0);
glRotatef(-yaw,0,1,0);
glTranslatef(X, -Y, Z);

of course your original xyz and rpy should be

(10.5, 0.0, 1.55)
and
(0,0,0) respectively

//to move to a _PARTICULAR_ position is easy too just figure out the deltas

deltaz= zdest-zorig
deltax= xdest-xorig
deltay= ydest-yorig

cheers

write back to tell me if this works

8. ## Re: Rotating a moving object

I think you've been misunderstood. You want the dinosaur to be at position {10.5,0,1.55} and to rotate in place there, right? When you say you want him to rotate around that point, people think you want him to move in a circle around that point.

glTranslate is not relative and is not affected by glRotate. However, glRotate IS affected by glTranslate. If the dino is centered around the origin, and you call glRotate, he'll rotate in place. If you call glTranslate first, then glRotate, he'll rotate in a circle around the origin, since he no longer is at the origin.

Basically, the radius of his rotation circle is his distance from the origin. If he's at the origin, that distance is zero. To create third-person games, you translate the camera back a bit (0,0,-Amount) and then call glRotate.

Anyway, what you want is this:

Code :
```
glPushMatrix( ); // Save ModelView
//
glRotatef( angle, 0, 1, 0 );
glTranslatef( 10.5, 0, 1.55 );
drawDino( );
//
glPopMatrix( ); // Restore ModelView```
[This message has been edited by CGameProgrammer (edited 12-12-2000).]

9. ## Re: Rotating a moving object

What my code should do is rotate you around an arbitrary point in space (X, Y, Z) and then you can translate with respect to that point and your rotation

so if you are at 1,0,0 and have rotated so the dino is facing the positive x axis and you want to move forward you just move and you will be rotating around 2,0,0 and you will be there too

when I mean move I mean move the center of your object and when I say rotate around I mean the center of your object is at that point and you are spinning about it (not in a circle)

10. ## Re: Rotating a moving object

What my program should do is this. This is my dino:

x---------------|
|---------------|
|-------------o-|
|---------------|
|---------------|

'x' is at the origin, and 'o' is the position to rotate around. This is because 'x' is the end of the dino's tail, and 'o' is the point between the legs. So if I rotate around 'o' it looks a bit more real. I've used Edward Angel's book to see how to rotate around a point that is not the origin (see page 180-181 if you got it too). This is the code that is used:

glMatrixMode(GL_MODELVIEW);
glTranslatef(4.0, 5.0, 6.0);
glRotatef(45.0, 1.0, 2.0, 3.0);
glTranslatef(-4.0, -5.0, -6.0);

So first you translate, then rotate, and then translate back. This works if my dino (read pos 'x') is at the origin. So I do this:

glMatrixMode(GL_MODELVIEW);
glTranslatef(10.5, 0.0, 1.55);
glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(-10.5, 0.0, -1.55);

And it works if my dino is at the origin. But now the dino position is variable, so the dino is at (dino_pos[0], dino_pos[1], dino_pos[2]). I still need to rotate around the point (10.5, 0.0, 1.55), but when I want to translate it back to the correct position, it doesn't work. Now it rotates around in circles, instead of rotating at the same point.

I've tried translating the dino back to (dino_pos[0] - 10.5, 0.0, dino_pos[2] - 1.55), and I've also tried to switch the signs, but nothing works.

I hope this makes the problem a bit clearer.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•