# OpenGL Change Animation in OpenGL

## Recommended Posts

Hello all,

I am trying to do animation by rolling a cube (1x1x1) along x_Axis.  I just want to roll this cube 90 degrees along the edge (parallel with y_Axis) in three times, then stop.

Then rolling within 45 degrees. then stop.

When I use OpenGL to do animation, it always rolls in cycle time. I do not know how to make the animation with different actions as I draw in the below image.

rollingCUbe.bmp

##### Share on other sites
Posted (edited)

With transformations, there are order of operation rules. Rotation occurs at the origin (0, 0) and spins in place as you've observed. If you translate it first the width of your square along the x axis to align the edge you want to pivot on next at the origin, then the behavior will appear correct when you next rotate the halfPi. Translate back into position before you draw to complete the illusion. You will have to separately keep track of your progression through your cycle and movement logic.

Edited by GoliathForge

##### Share on other sites

Translate it so lower right corner is at (0,0,0) in model space, then rotate clockwise around Z axis, then translate back to original model position. Repeat until rotation is 90 degrees. Repeat the whole thing 3 times, record and apply total transformation from previous cycle's last frame before transforming on each subsequent cycle.

##### Share on other sites

"translate back to original model position" - this may be wrong I think.

##### Share on other sites
19 hours ago, GoliathForge said:

With transformations, there are order of operation rules. Rotation occurs at the origin (0, 0) and spins in place as you've observed. If you translate it first the width of your square along the x axis to align the edge you want to pivot on next at the origin, then the behavior will appear correct when you next rotate the halfPi. Translate back into position before you draw to complete the illusion. You will have to separately keep track of your progression through your cycle and movement logic.

Hi @GoliathForge, I can move to the world coord. then rotation then translation. However, it only has the final result (the new rotated cube) not the animation. I try to use the below function

glutTimerFunc(0, Timer, 0);

but it only happens for one rotation action.

global float angle=0.0f;

In the void Display(), rolling the cube with glRotatef(angle, 0, 0, 1); at the end of function with angle += 10.

I do not know when I can call the function for translation.

4
10 hours ago, VoxycDev said:

Translate it so lower right corner is at (0,0,0) in model space, then rotate clockwise around Z axis, then translate back to original model position. Repeat until rotation is 90 degrees. Repeat the whole thing 3 times, record and apply total transformation from previous cycle's last frame before transforming on each subsequent cycle.

for the 1st action, I translated the lower right corner to (0,0,0) then rotates from 0 -90 degree for animation. then the 2nd action is wrong with the coordinate.

In OpenGl when I used glutDisplayFunc(Display);  it always calls back the Display() function that I could not modify the next action.

##### Share on other sites
7 hours ago, tamlam said:

I try to use the below function


glutTimerFunc(0, Timer, 0); 

but it only happens for one rotation action.

The first parameter defines the interval the call will be made. You've specified in no time. (so it may only fire once, not sure on that)

Moving on...

Here is an example of one way, you could do it.

// global access
float rotation = 0.f;
float step = -2;

// inside an update or timer function
{
rotation -= elapsedTime * 40.f;
if (rotation < -90)
{
rotation = 0.f;
step += 1.f;
}
}

{
glMatrixMode(GL_MODELVIEW);

glTranslatef(step, 0, 0);
glRotatef(rotation, 0, 0, 1);

glTranslatef(0, 0, -5); // so I can see it
glColor3f(1.f, 1.f, 1.f);
glVertex3f(-1, 0, 0);
glVertex3f(0, 0, 0); // pivot point is already on the side(right bottom)
glVertex3f(0, 1, 0);
glVertex3f(-1, 1, 0);
glEnd();
}

I've cheated here perhaps with the arrangement of the vertices.  A normal rotation will appear correct the first 90°. What I've chosen to do is only use that range of 0° to 90° and when I reach my rotation, I reset to zero and advance the translation variable I'm tracking. In computer graphics, the goal is to create the illusion with the least amount of background work.

Have you considered a modern openGL environment?

##### Share on other sites
16 hours ago, GoliathForge said:

The first parameter defines the interval the call will be made. You've specified in no time. (so it may only fire once, not sure on that)

Moving on...

Here is an example of one way, you could do it.


// global access
float rotation = 0.f;
float step = -2;﻿

// inside an update or timer function
{
rotation -= elapsedTime * 40.f;
if (rotation < -90)
{
rotation = 0.f;
step += 1.f;
}﻿
}﻿

{
glMatrixMode(GL_MODELVIEW);

glTranslatef(step, 0, 0);
glRotatef(rotation, 0, ﻿0, 1);﻿﻿﻿

glTranslatef(0, 0, -5); // so I can see it
glColor3f(1.f, 1.f, 1.f);
glVertex3f(-1, 0, 0);
glVertex3f(0, 0, 0); // pivot point is already on the side(right bottom)
glVertex3f(0, 1, 0);
glVertex3f(-1, 1, 0);
glEnd();﻿
}

﻿ I've cheated here perhaps with the arrangement of the vertices.  A normal rotation will appear correct the first 90°. What I've chosen to do is only use that range of 0° to 90° and when I reach my rotation, I reset to zero and advance the translation variable I'm tracking. In computer graphics, the goal is to create the illusion with the least amount of background work.

Have you considered a modern openGL environment?

Thanks for comment.

I tried but it did not work. The problem is that in order to roll a cube along its edge. There are some steps (just like youtube link

1. translate the cube until the contact edge for rolling == Y_axis.)

2. rotate the cube with new coordinate (90 degrees) around Y_Axis.

3. translate the rotated cube to a new coordinate.

When we do animation (only for rotation) we have to do 3 steps in OpenGL, how can I only show the rotation because it has to pass these 3 above steps?

##### Share on other sites
7 hours ago, tamlam said:

Thanks for comment.

I tried but it did not work. The problem is that in order to roll a cube along its edge. There are some steps <chop>

I'm sorry you are having difficulty. The sample source was compiled and executed. The result on my screen looks like an on edge rolling square. Good luck with your studies.

##### Share on other sites
15 hours ago, GoliathForge said:

I'm sorry you are having difficulty. The sample source was compiled and executed. The result on my screen looks like an on edge rolling square. Good luck with your studies.

Thanks so much.

Can you please share your source code. I would like to test on my computer to see the result.

##### Share on other sites
Posted (edited)
3 hours ago, tamlam said:

I already did.   When I saw that you were using immediate mode opengl, I pulled one of my older frameworks and replaced the content of my update and draw methods with the code posted above. Nothing else hidden. You are more than welcome to browse my ogl2 framework code if you wish.  A link to the source I used is in this album, but again, on topic sample code is in the post above.

Edited by GoliathForge

## Create an account

Register a new account

• 16
• 9
• 10
• 18
• 12