Rotate an Object.. Help! - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Rotate an Object.. Help!

Posts: 1Member
Hi, I'm programming a 3d world in Opengl. But i spend a lot of time trying to solve one case.
How to make an object to rotate for some angle when Camera gets closer to it???
For Camera movement i use gluLookAt.

• Posts: 5Member
You want the object to rotate when the camera approaches with the rest of the world remaining stationary, or do you want the camera to rotate around the object in the world?

I'm certainly no expert, but I think if you are trying to use gluLookAt, you are going to have to calculate the coordinates of the camera position based on the angle you want and the distance away from the object. It would probably easier and less expensive to use calls to glTranslate and glRotate.

If you want the camera to rotate around the object, maybe this would work...
RenderFunction()

[code]
glTranslatef(x, y, z); //where x, y, z are the coordinates of the object you want to rotate around.

glRotatef(angle, 0.0f, 1.0f, 0.0f); // Where angle is the rotation angle around the object and the y-axis is "up" in your world
glRotatef(elevation, 1.0f, 0.0, 0.0f); // Where "elevation" is the elevation angle above the terrain
glTranslatef(0.0f, 0.0f, -distance); //Where distance is the distance from the object

// Render your world, with the object at x, y, z using appropriate transformation calls

[/code]

Now, all you have to do is change "angle" and "elevation" periodically to make the camera rotate around the object.

If you want the object to actually rotate, just change the model-view matrix prior to drawing the object that is rotating like this:

[code]
// Draw some stuff in the world

glPushMatrix();
glTranslatef(objectX, objectY, objectZ);
glRotatef(angle, 0.0f, 1.0f, 0.0f)
// draw the object
glPopMatrix();

//Draw the rest of the stuff in the world

[/code]

In this case, you can change the value of angle based on the distance of the camera from the object.

That is my two cents. Hope it helps (and actually works)...or maybe I totally misunderstood what you wanted.
• Posts: 5Member
One small correction on my previous post...you would have to call glLoadIdentity() after glPushMatrix() in the alst bit of code for it to work as I suggested, but you probably noticed that.
• Posts: 5Member
One small correction on my previous post...you would have to call glLoadIdentity() after glPushMatrix() in the alst bit of code for it to work as I suggested, but you probably noticed that.