## OpenGl and Rotating images

DukeLeto
Unregistered

Post: #1
This is probably an easy question, but It has me stumped...

I have 3 objects on the screen in a line that I would like to rotate as a group around a point. I'm able to rotate the objects individually, but their location does not chage. How can I rotate the objects around a point so that they will remain in a line once the rotation is complete?

Example
Stating image:
Code:
`- - -`

I can do this:
Code:
`| | |`

but I can't determine how to do this:
Code:
```/ / /```

Thanks.
Moderator
Posts: 335
Joined: 2002.04
Post: #2
the openGL rotation function only changes each object's rotation about its own axis, the rotation of the objects around a central point requires moving the objects. If you're in 2D, the formula is

x = radius * cos(angle) + centre_x
y = radius * sin(angle) + centre_y

you'd move the objects to these positions and use the glrotate to turn them in the right direction (as you're already doing)
drzeip
Unregistered

Post: #3
I may be wrong but can't you just call glRotate before calling glTranslate and then the translate call will be affected by the rotate call as the matrix won't have been reset?

e.g.

Code:
```glRotate( 30.f , 0.f , 0.f , 1.f ); drawObject(); glTranslate(  -1.f , 0.f , 1.f ); // move to left object drawObject(); glTranslate(  2.f , 0.f , 1.f ); // move to right object drawObject();```

I think......
Member
Posts: 70
Joined: 2004.06
Post: #4
Yes, you can do that. It's how I would have done it.

Matrix calls are cumulative, that is each call will act on the state left over from the previous one. You can use push and pop or load the identity matrix again to get a fresh one. Something that got me for a while was that any modifications (translate, rotate etc) you wanted to do to the objects had to be done before you drew it. This was backwards to what I was used to but it is entirely logical.

By the way, in your example you changed the z coordinate as well as the x one. You should not do it more than once, because otherwise (due to it being cumulative) you went und up having translated two units forward on the z-axis, which I doubt it what you intended.
drzeip
Unregistered

Post: #5
Oops you're right I didn't mean to do that!! Stuff like that is fairly standard with my coding...
Member
Posts: 233
Joined: 2003.05
Post: #6
Use Zwilink's method if the actual coordinates of the moving objects are needed at some point.

For example, in SpaceBarrage I was going to avoid calculating everything and just use OpenGL's rotate methods (and I mostly did), but then I wanted to have the ships break into pieces I suddenly needed to know the coordinates of each point on the rotated ship. You can't get those with just OpenGL calls.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Moderator
Posts: 769
Joined: 2003.04
Post: #7
Well... you can get the current transformation matrix and multiply it by your point, so if you have a "vector x matrix" fuction you can do most of your stuff with gl calls.