How to orbit a point in a 3d space

I’m having a trouble drawing a scene of mine.

I have a world, which is composed of a board, and a robot on the board. The board’s vertex coordinates never change (that is, they get the same glvertex3f values every frame). The robot can move left and right on the board. Its vertex value’s don’t change either, but gltranslatef is called before it is drawn. I only change the x parameter to gltranslatef for the robot.

Now how do I get the scene to orbit from a fixed distance around the robot?

I’m stuck on this, nothing I try works, it just seems to rotate around odd points that aren’t central to the robot.

To rotate the camera around the robot you can use the gluLookAt routine.

first you define a distance between the camera and the center of the model. Second you set a initial angle to camera (0, for example). So each frame you increment this angle. The operations bellow will rotate the camera around the robot y axis:

camera_position_x = translation_value_x+distancecossine_of_angle
camera_position_y = translate_value_y
camera_position_z = translation_value_z+distance
sine_of_angle

look_vector_x= -cossine_of_angle+camera_position_x
look_vector_y = camera_position_y
look_vector_z= -sine_of_angle+camera_position_z

up_vector = (0,1,0)

NOTE1: the values of translation_value_(x,y,z) above are the values you pass to glTranslatef.

NOTE2: the code above works if your model center in MODEL space is 0,0,0. If it isn’t you must add to camera_position_(x,y,z) above the x,y,z of your model center.

i didn’t test this code but i think it’s right

marcio

Did you try the projection matrix? I think you should do that while you initialize the projection matrix:

glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glFrustum(…);
glTranslated(-RobotX,-RobotY,-RobotZ);
glRotated(SomeAngle,0,1,0);
glTranslated(RobotX,RobotY,RobotZ);

glMatrixMode(GL_ModelView);
glLoadIdentity();
/* Draw your scene (don’t translate the robot here */
.
.
.

Well, it’s not exactly like this because it depends on your program, but the key is the projection matrix.

Careful with using the projection matrix to perform transformations though… in many OpenGL applications for example fog won’t work correctly if transformations are applied to the projection matrix.
Every rotation, translation or scaling that can be done with the projection matrix can be represented by transformations in the modelview matrix anyway, so it’s probably better to stick with GL_MODELVIEW.

Originally posted by Dodger:
[b]Careful with using the projection matrix to perform transformations though… in many OpenGL applications for example fog won’t work correctly if transformations are applied to the projection matrix.
Every rotation, translation or scaling that can be done with the projection matrix can be represented by transformations in the modelview matrix anyway, so it’s probably better to stick with GL_MODELVIEW.

[/b]

If what Dodger had said is right then you may need to modify the code like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glFrustum(…);

glMatrixMode(GL_ModelView);
glLoadIdentity();
glTranslated(-RobotX,-RobotY,-RobotZ);
glRotated(SomeAngle,0,1,0);
glTranslated(RobotX,RobotY,RobotZ);
glPushMatrix();
/* Draw your scene (don’t translate the robot here */
.
.
.
glPopMatrix();

Thanks for the comment Dodger

Wow, it seems you all got it wrong.

Well, I did some reading up, and what I worked out was basically this:

You pretend that the camera is the last object drawn, then think up the code of how to manipulate it into the right position. Assume that the camera starts off a 0,0,0 position with 0,0,0 rotation on every frame.

Then, you take the code you’ve written for that camera, and put it at the START of your code (not the end), put it in reverse order, and reverse the magnitudes. Also put glLoadIdentity before you do this so that you are sure that it starts off at 0,0,0.

Thats the process to work camera transformations basically. I don’t like gluLookAt, because I don’t understand it. I like to understand how something works generally, and it works perfectly now.