Moving and Rotating Objects

Hi

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

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?

hi cwzcwz,

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

This is the pseudo code.

glMatrixMode( GL_MODELVIEW );
glPushMatrix();

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

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.

Youâ€™re question is very vague.

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

Cheers!