PDA

View Full Version : Composed object transformation behaviour

xtracto
09-16-2005, 06:45 AM
So, I have an scene which is a "cube" composed of smaller cubes, like a rubik cube but the cubes are spaced CUBES_SPACE units on each size.

The problem is that when I apply a rotation function to the modelview matrix (after doing some translations, see below) the rotation does not make the behaviour I expect.

The way I create the composed cube is to call 3 loops:

from 1 to X
gltranslatef(x,0,0)
pushmatrix()
from 1 to Y
gltranslatef(0,y,0)
pushmatrix()
from 1 to Z
gltranslatef(0,0,z)
DrawSmallCube
end from 1 to z
popmatrix()
end from 1 to y
popmatrix()
end from 1 to x

Now, with this I get my cube drawn just fine, but then, what I want to do is to be able to rotate *the whole cube* on the X, Y or Z axis (just one axis each time) using the center of the composed cube as the pivot for rotation.

The way I try to do this is:
1. translate modelview matrix to (-BigcubeXlength/2, BigcubeYlength/2, BigcubeZlength/2).

Bigcube{X,Y,Z}length are the lenghts of the composed cube on each axis. And is calculated something like:
BigcubeXlength = NUMBER_OF_CUBES*CUBE_LENGTH+NUMBER_OF_SPACES*SPACE S_LENGTH

NUMBER_OF_SPACES is just NUMBER_OF_CUBES-1. Because if I have 4 cubes the actual big cube will be 4+3 units long (if each cube and space is 1 unit ong): for example the front face of my cube is made of small cubes (each # represents one small cube

# # # #
# # # #
# # # #
# # # #

I do this because IIRC, that will put the center of the cube on the (0,0,0 ) coordinates. I do that adding a "gltranslatef(...) function before the previous cycles.

Now my problem is that although teorethicaly it is right, when I rotate the cube it gets rotated from an arbitrary point inside the cube, as if my equations to get the center (the -BigcubeXlength/2 equations) where wrong.

I know these explaniation is horrible but if someone has any experience with this kind of models I would be really glad if she/he could help
me.

I am developing in DevC++ with OpenGL+SDL. I have some knowledge of GL although I am in any waya guru or anything similar, so I am sure it must be a tiny error. I can show all my code if someone is willing to help me.

Of course, if anyone knows of a good document[online or offline]/tutorial that talks about moving sets of objects on a scene and the pivot point I would also appreciate it as I think my problem is more mathematical than from opengl.

Cheers!

dorbie
09-16-2005, 09:13 PM
Call the whole cube rotation at the beginning of drawing the cube before you do all the smaller transformations which will then be relative to your root rotation.