Moving and Rotating Objects

Hi

I am new to programming in OpenGL and any help will be very much appreciated :slight_smile:

I am currently trying to make the rectangle that I have drawn move in a circle. I was told that it will require some Mathematics regarding differentiating the x and y positions of the rectangle with respect to time and angle.

How do I go about doing that?

Please help. Thanks!

hi cwzcwz,

Provided I understood you correctly, I think what you want to do it rather straight forward.

Just use glRotatef(…) calls to make your rectangle rotate. I’ll assume you want your rectangle about the origin about the z-axis.

This is the pseudo code.

glMatrixMode( GL_MODELVIEW );
glPushMatrix();

/////////////////////////////////////////////
glRotatef( angle, 0.0, 0.0, 1.0 );
////////////////////////////////////////////

glBegin( GL_QUADS );

glVertex2f( x1, y1 );
glVertex2f( x2, y1 );
glVertex2f( x2, y2 );
glVertex2f( x1, y2 );

glEnd();

glMatrixMode( GL_MODELVIEW );
glPopMatrix();

Let me know if this is what you wanted. :slight_smile:

You’re question is very vague.
Please elaborate a little more in your next post.

What I understand from you’re question is that you want to rotate the rectangle about its center but it is unclear whether you want it to rotate uniformly or in a predetermined haphazard fashion.

We’ll if you want uniform rotation its simple. All you need to do is increment (or decrement) you’re angle in the pseudo code provided by MatchStickMan in certain steps (say 5 degrees every step) and run you’re animation using the glutTimerFunc.

This is nothing but a timer call-back where you can run any segment of you’re code periodically. For animations usually you would simply want to redraw you’re screen so you call a glutPostRedisplay.

heres the pseudo code for the timer call back

void timer(int i)
{
glutPostRedisplay();
glutTimerFunc(TIME,timer,1); //here TIME is you’re
// timer period in
// milliseconds.
}

you can register the time call back by including the following line in you’re main function

glutTimerFunc(TIME,timer,1);

As for the haphazard rotation. You do need certain math depending on you’re requirements.

For a controlled rotation you may want to refer to my post “Rotating a cube with the mouse”.

Sorry for not being clear enough.

What I meant was something like having a rectangle and moving it around a pivot, not the centre of the rectangle but something like about an imaginary point on the screen.

Eventually I would like to develop it to move it with the help of differential equations.

That would be pretty easy. First translate to the point where you would like to place you’re axis. Then rotate the rotate by the amount you would like to rotate in the desired sense. Finally translate to a point which places you’re rectangle at the required distance from your axis. Then draw your rectangle.

Here is an example where I rotate the rectangle about one of its vertex. Assume the vertices are v1 v2 v3 v4.


void draw()
{
   ...
   ...
   
   glTranslatef(v1);
   glRotatef(angle_step,0.0f,0.0f,1.0f);
   glTranslatef(center_of_rect);
   draw_rect();
}

I hope this cleared your doubt.

Hi

Thanks all for the response. Got it working already :smiley:

Cheers!