View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Animation with glut

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1rgc183  Members

Posted 02 September 2013 - 11:59 PM

I am trying to do animation using glut. I have 2 squares overlapping to each other and i want other square to rotate around this central object without rotating themselves. I want to rotate in orbit manner. I am calculating circular path using parametric equation and translating that one of the square to that distance. i am calling this function in a loop

Here is my code which is not working as per expected. Let me know where it is going wrong.
I have added expected image as well


void DrawShapes()
{
static float angle = 0;

glClear(GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 0.3f, 0.2f);

glBegin(GL_POLYGON);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();

static int i = 0;
distance_x = 0.3 * cos(i*3.14 / 180);
distance_y = 0.3* sin(i*3.14 / 180);
i++;
if (i > 360)
i = 0;

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(distance_x, distance_x, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();

glPopMatrix();
glutSwapBuffers();

}


### #2Brother Bob  Moderators

Posted 03 September 2013 - 03:44 AM

So what is wrong with it?

### #3stevo5800  Members

Posted 03 September 2013 - 04:29 AM

Use glRotatef to rotate your object. Best way to do this is to move the dot to 0,0, then move the object the same distance, then move it back. For example Let's say the dot was a 10,10 and the object to rotate around the dot was at 10, 20. Then I would move the dot - 10 in the x and - 10 in the y putting it at 0,0. Then You move the objet the same exact distance -10 in the x and - 10 in the y. Then you do you call glRotatef then move the object and dot back the same amount you moved it. That's how it goes in theory. But in OpenGL the matrix are calculated in reverse.

Example

glTranslatef(10.0f ,10.0f ,0.0f); // this gets called third
glRotatef(1.0f, 0.0f, 0.0f)// this gets called second
glTranslatef(-10.0f ,-10.0f ,0.0f); //this gets called first


A bit strange yea but this is how it works, the last movement you make is the first and vis versa. Hope this explains a bit.

Also note if you need the coordinates after a rotate you could do something like this

GLfloat matrix2[16];

glGetFloatv(GL_MODELVIEW_MATRIX, matrix2);

double x ,y;

//get the x and y from the matrix is self
if (matrix2 != NULL)
{
x = matrix2[12];
y = matrix2[13];
}


Also note you can get any value you want from the matrix.

Edited:

The problem with your code is this line static int i = 0; Static means it never changes, and on top of that you declare the int inside the function. So that means each time it calls draw shape() you are setting i = 0; so it never increases. To resolve this make int i = 0 global. Or you could used a glRotatef and not worry about all the extra equations.

Edited by stevo5800, 03 September 2013 - 10:29 AM.

### #4rgc183  Members

Posted 04 September 2013 - 03:12 PM

Use glRotatef to rotate your object. Best way to do this is to move the dot to 0,0, then move the object the same distance, then move it back. For example Let's say the dot was a 10,10 and the object to rotate around the dot was at 10, 20. Then I would move the dot - 10 in the x and - 10 in the y putting it at 0,0. Then You move the objet the same exact distance -10 in the x and - 10 in the y. Then you do you call glRotatef then move the object and dot back the same amount you moved it. That's how it goes in theory. But in OpenGL the matrix are calculated in reverse.

Example

glTranslatef(10.0f ,10.0f ,0.0f); // this gets called third
glRotatef(1.0f, 0.0f, 0.0f)// this gets called second
glTranslatef(-10.0f ,-10.0f ,0.0f); //this gets called first


A bit strange yea but this is how it works, the last movement you make is the first and vis versa. Hope this explains a bit.

Also note if you need the coordinates after a rotate you could do something like this

GLfloat matrix2[16];

glGetFloatv(GL_MODELVIEW_MATRIX, matrix2);

double x ,y;

//get the x and y from the matrix is self
if (matrix2 != NULL)
{
x = matrix2[12];
y = matrix2[13];
}


Also note you can get any value you want from the matrix.

Edited:

The problem with your code is this line static int i = 0; Static means it never changes, and on top of that you declare the int inside the function. So that means each time it calls draw shape() you are setting i = 0; so it never increases. To resolve this make int i = 0 global. Or you could used a glRotatef and not worry about all the extra equations.

Thanks for your reply. I tried calling the sequence gltranslate, glrotate and gltranslate but it did not  work properly. I want to move objects in orbital manner around the centre object.

In my above code, i found the mistake it should be

glTranslatef(distance_x, distance_y, 0.0);


How can I achieve the same effect with glrotatef and not by above manipulation ? I dont want object to rotate around itself.

Edited by rgc183, 04 September 2013 - 03:37 PM.

### #5rgc183  Members

Posted 04 September 2013 - 03:37 PM

So what is wrong with it?

I found my mistake in glTranslatef call. when i changed it to

glTranslatef(distance_x, distance_y, 0.0);


it works.

Now, how can i achieve the same result with glRotatef ? I tried but the motion is not orbial.

Edited by rgc183, 04 September 2013 - 03:38 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.