Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rotation

Started by The Kid Dec 4, 2001 at 9:48 PM 9 replies 800+ views
Original Post
The Kid
The Kid
Just a quick question that I''m hoping someone can help me with. I''m writing a minidemo, and of course it has rotations. Well I want to rotate something, but its not at the world center, so it rotates around the world center instead of its center, like I want it to. Right now I''m using glRotatef(). I''m not sure if I''ll have to use matrix rotations, or what, but I would much appriciate it if you could help me out here. This is part of my term project for school. Thanx. Either information, or a place where I can get information would be greatly appriciated. Thanx again. The Kid I don''''t know what the future holds, but I know who holds the future.
I don''t know what the future holds, but I know who holds the future.
Wavarian
Wavarian
So what you should do is move the object back to the world''s origin, rotate it, and translate it back to where it was.

ie. say you had object''s center point at (41,20,9)

do this:

gltranslatef(-41,-20,-9)
glrotatef(angle,axis etc)
gltranslatef( 41, 20, 9)
drawObject()
RabidChild
RabidChild
I think the functions you''re looking for are glPushMatrix() and glPopMatrix(). If you have the "red book," you may want to look it up in there; if you don''t you can find it online at www.opengl.org. These functions let you temporarily change where the origin and axes are, do manipulations and rendering between the calls, and then restore the original orientations. Hope that helps some.
--Ben CarterRomans 8:38-39
NaliXL
NaliXL
I''m having difficulties with this concept at the moment too. Haven''t tried it though. Is it really so that an object that you rotate using glrotatef always rotates around the point where you created it? Why can''t you just pass 6 parameters : the x, y, z rotation and the x, y, z coordinates of the point it should be rotating about? Isn''t this a method that only takes a lot of unneccesary processing power?

Also, I''ve noted that you seem to rotate things object by object you''ve created. So if I''ve created a scene with 5 cubes, and rotated all of those a bit, how can I rotate the whole scene after that? If you use glrotatef, won''t that just rotate the last created cube a bit further?
Newbie programmers think programming is hard.Amature programmers think programming is easy.Professional programmers know programming is hard.
Fox Mc Cloud
Fox Mc Cloud
There are two ways of understanding how matrix transformations operate : local coordinates and global coordinates. It doesn''t change anything to the code you will get, it''s just the way of thinking that is different.

- If you consider that the translations and rotations all happen with respect to the fixed global world coordinates, which are based at 0,0,0 and never move, then you must write the transformations in the REVERSE order.
So if you want to translate your object, and then rotate the object around the global world center (where the object was before the translation), you must FIRST write glRotate, and THEN glTranslate. Reverse order gives you global coordinates.

- If you consider that the translations and rotations all happen with respect to the local, moving coordinates, which are based at the center of your object and move with each transformation, then you must write them in the right order.
In this case, if you translate your object, then rotate it around its center, then translate it again, the last translation will happen with respect to the rotated axes. You just code the translations in this order and they will work as expected.

You can move and transform the camera as well as the scene that way. Any transformation to the view has the effect of the inverse transformation to the camera. This is because you can''t tell the difference between an object that moved 10 meters away from you, or if you moved 10 meters backwards. Thank Einstein for this. So if you think "ok, now I must rotate my camera 10 degrees around the Y axis", you just have to rotate the scene -10 degrees, with glRotate.

If you didn''t understand this well, well it''s because I cannot replace your precious Red Book, which I hope you all have... And if you don''t, go buy it, it''s really useful and explains all this. And buy NeHe''s cd too :D.

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
The Kid
The Kid
I''m not sure everyone understood my problem completely. What I have is a scene with a little ball in it. I want to be able to rotate the scene with the keyboard or mouse, and still have the ball move left and right on the same axis. Basically I want to rotate things without rotating the axis. I''m not sure how to get this to work right.

RabidChild - How exactly would I use glPush and PopMatrix to achieve this effect. I''ve been fiddling with glPush and PopMatrix for a long time, and I haven''t been able to get it to work right.

thanx to all. This is one of my school projects, and I need just a little help to get my back on track.

The Kid
PS - God hold the future.


I don''''t know what the future holds, but I know who holds the future.
I don''t know what the future holds, but I know who holds the future.
Fox Mc Cloud
Fox Mc Cloud
Your problem is not too difficult
When creating an OpenGL scene, you must first move the camera, then place the objects on the scene.
Like I said, the fact that you do the camera moves before, will have the effect that your next transformations can be considered relatively to the absolute center of the world.

So, first you reset the camera, then rotate it around the Y and / or X axis :

glLoadIdentity();
glRotatef(AngleY, 0.0, 1.0, 0.0);
glRotatef(AngleX, 1.0, 0.0, 0.0);

Then you move your camera along the rotated Z axis

glTranslatef(0.0,0.0,CameraDistance);

Finally you can move your ball as you wish

glTranslatef(BallX,BallY,BallZ);

You could also rotate the object or do anything you wish with it instead of that last line
(I haven''t tested the code but it should be ok, tell me if there are problems.)

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
ATronic
ATronic
I believe you would translate out along the x axis before the rotation. And why use gltranslatef for each object instead of simply modifiying the coordinates in glvertex calls. Its much faster...

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Fox Mc Cloud
Fox Mc Cloud
No it''s not

You would need to add the value to each vertex, instead of just calling glTranslatef one time for each object. Try that if you have 10 objects with 1000 vertices in them that''s 10 glTranslatef or 30000 additions.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
ATronic
ATronic
For high polycount maybe, but matrix transformations can be expensive.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.