2
votes

I have an object which i want to move around in a circle in Open GL/ Visual Studio. But so far, the object is only rotating around itself.

How can i make it move in a circular way?

Currently, it just translates the object instantly, but not over the course of my rotation animation.

This is my code in the display function - because i was told if you want the object not to rotate around itself, you need to translate. But so far, my object just ignores the translation here completely and still revolves around itself..

glRotatef(0.0f, 0, 1, 0);
glTranslatef(2.0, 0.0, 0.0);
glRotatef(angle, 0.0, -1.0, 0.0);   

drawExcavator(); // draws the object

And this is my animating function for later on to define the angle i use in the glRotatef call:

void animate() {

    // calculating the time needed for the animation 
    static long time = clock();
    long oldTime = time;
    float diffTime;
    time = clock();
    diffTime = ((float)(time - oldTime)) / ((float)CLOCKS_PER_SEC); // taken from the exercise sheets

    // checking if the animation has not been stopped:
    if (!pause) {
        angle += diffTime*rotateSpeed;
        elapsedTime += diffTime;
        frameCount++;

        // adding up the frames so that they are shown in the window:
        if (elapsedTime > 1.0)
        {
            // counting the fps so that they are outprinted in the window line:
            fps = (float)frameCount / elapsedTime;
            fps = fps / 100; // for correct frame numbers 
            elapsedTime = 0.0;
            frameCount = 0.0;
        }
    }
}
1

1 Answers

4
votes

You have to swap the translation and rotation operation:

glRotatef(angle, 0.0, -1.0, 0.0);   
glTranslatef(2.0, 0.0, 0.0);


Explanation:

Translation: See the documentation of glTranslate:

glTranslate produces a translation by x y z . The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix.

Rotation: See the documentation of glRotate:

glRotate produces a rotation of angle degrees around the vector x y z . The current matrix (see glMatrixMode) is multiplied by a rotation matrix with the product replacing the current matrix.


The translation matrix looks like this:

Matrix4x4 translate;

translate[0] : ( 1,  0,  0,  0 )
translate[1] : ( 0,  1,  0,  0 )
translate[2] : ( 0,  0,  1,  0 )
translate[3] : ( tx, ty, tz, 1 )

And the rotation matrix around Y-Axis looks like this:

Matrix4x4  rotate;
float      angle;

rotate[0] : ( cos(angle),  0, sin(angle), 0 )
rotate[1] : ( 0,           1, 0,          0 )
rotate[2] : ( -sin(angle), 0, cos(angle), 0 )
rotate[3] : ( 0,           0, 0,          1 )   


The result of translate * rotate is this:

model[0] : ( cos(angle),  0,  sin(angle), 0 )
model[1] : ( 0,           1,  0,          0 )
model[2] : ( -sin(angle), 0,  cos(angle), 0 )
model[3] : ( tx,          ty, tz,         1 )

enter image description here


The result of rotate * translate is:

model[0] : ( cos(angle),                     0,   sin(angle),                     0 )
model[1] : ( 0,                              1,   0,                              0 )
model[2] : ( -sin(angle),                    0,   cos(angle),                     0 )
model[3] : ( cos(angle)*tx - sin(angle)*tx,  ty,  sin(angle)*tz + cos(angle)*tz,  1 )

enter image description here