rotation around center point between multiple objects
I am trying to rotate a series of object around the center point between the objects. However, I seem only to be able to rotate around either the origin or some random point but not the center point between the objects.
illustrated below.
object1 -----------center of rotation------------object2
The function used to draw the scene is below.
void DisplayListView::Show(FOR_List * main_list)
{
FOR * tmp = main_list->head;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < main_list->get_count(); i++)
{
glPushMatrix();
glLoadIdentity();
glTranslatef(moveX, moveY, moveZ);
glTranslatef(tmp->x, tmp->y, tmp->z);
glRotatef(cameraAngleX, 1, 0, 0); // pitch
glRotatef(cameraAngleY, 0, 1, 0); // heading
glTranslatef(-tmp->x, -tmp->y, -tmp->z);
if(tmp->dlist != -1)
{
glCallList(tmp->dlist);
}
glPopMatrix();
tmp = tmp->next;
}
glFlush();
}
if i understand your question correctly the following should work for u.
1 - work out the coordinates of the center point by adding up the X, Y and Z values of all the objects and then deviding each total by the amount of objects you have
2 - in your draw cycle right before you start to render your objects translate by the aboive cooirdinates and then rotate by the amount that you want all your objects to be rotated around the center point.
remember to keep incrementing the value by which you rotate each cycle if you want to make a animation out of it
the logic should be something like this
int centerX,centerY,centerZ;
for (i = 0;i < amountOfObjects;i++)
{
centerX = centerX + object.x
centerY = centerY + object.y
centerZ = centerZ + object.z
}
centerX = centerX / amountOfObjects;
centerY = centerY / amountOfObjects;
centerZ = centerZ / amountOfObjects;
glTranslate(centerX,centerY,centerZ);
glRotate(rotX,0,0);
glRotate(0,rotY,0);
rotX++; //if you want the next scene to be different
rotY++; //
//then draw the objects like you normally would
o and youl probley need to take out your glLoadIdentity() and put it as the first thing in your draw cycle before you do any translating or rotating
Home this helps :P
1 - work out the coordinates of the center point by adding up the X, Y and Z values of all the objects and then deviding each total by the amount of objects you have
2 - in your draw cycle right before you start to render your objects translate by the aboive cooirdinates and then rotate by the amount that you want all your objects to be rotated around the center point.
remember to keep incrementing the value by which you rotate each cycle if you want to make a animation out of it
the logic should be something like this
int centerX,centerY,centerZ;
for (i = 0;i < amountOfObjects;i++)
{
centerX = centerX + object.x
centerY = centerY + object.y
centerZ = centerZ + object.z
}
centerX = centerX / amountOfObjects;
centerY = centerY / amountOfObjects;
centerZ = centerZ / amountOfObjects;
glTranslate(centerX,centerY,centerZ);
glRotate(rotX,0,0);
glRotate(0,rotY,0);
rotX++; //if you want the next scene to be different
rotY++; //
//then draw the objects like you normally would
o and youl probley need to take out your glLoadIdentity() and put it as the first thing in your draw cycle before you do any translating or rotating
Home this helps :P
Thanks for the help. I've modified the code as illustrated below. However, the rotation isn't around the center point of the objects, but around a point out the objects.
Cheers
David
object1-----object1--X--object1-----object1
X is ideal center of rotation
object1-----object1-----object1-----object1-----X
X is the actual center or rotation
void DisplayListView::Show(FOR_List * main_list)
{
FOR * tmp = main_list->head;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < main_list->get_count(); i++)
{
centerX += tmp->x;
centerY += tmp->y;
centerZ += tmp->z;
tmp = tmp->next;
}
centerX = centerX / main_list->get_count();
centerY = centerY / main_list->get_count();
centerZ = centerZ / main_list->get_count();
glLoadIdentity();
// move back to start of list
tmp = main_list->head;
glTranslatef(centerX, centerY, centerZ);
glRotatef(rotX, 1, 0, 0); // pitch
glRotatef(rotY, 0, 1, 0); // heading
for(int i = 0; i < main_list->get_count(); i++)
{
glPushMatrix();
if(tmp->dlist != -1)
{
glCallList(tmp->dlist);
}
glPopMatrix();
tmp = tmp->next;
}
rotX++;
rotY++;
glFlush();
}
Cheers
David
object1-----object1--X--object1-----object1
X is ideal center of rotation
object1-----object1-----object1-----object1-----X
X is the actual center or rotation
void DisplayListView::Show(FOR_List * main_list)
{
FOR * tmp = main_list->head;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < main_list->get_count(); i++)
{
centerX += tmp->x;
centerY += tmp->y;
centerZ += tmp->z;
tmp = tmp->next;
}
centerX = centerX / main_list->get_count();
centerY = centerY / main_list->get_count();
centerZ = centerZ / main_list->get_count();
glLoadIdentity();
// move back to start of list
tmp = main_list->head;
glTranslatef(centerX, centerY, centerZ);
glRotatef(rotX, 1, 0, 0); // pitch
glRotatef(rotY, 0, 1, 0); // heading
for(int i = 0; i < main_list->get_count(); i++)
{
glPushMatrix();
if(tmp->dlist != -1)
{
glCallList(tmp->dlist);
}
glPopMatrix();
tmp = tmp->next;
}
rotX++;
rotY++;
glFlush();
}
ok try translating with the inverse of the center after rotating
glTranslatef(centerX, centerY, centerZ);
glRotatef(rotX, 1, 0, 0); // pitch
glRotatef(rotY, 0, 1, 0); // heading
glTranslatef(-centerX, -centerY, -centerZ);
glTranslatef(centerX, centerY, centerZ);
glRotatef(rotX, 1, 0, 0); // pitch
glRotatef(rotY, 0, 1, 0); // heading
glTranslatef(-centerX, -centerY, -centerZ);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement