1

I am having problem rotating a cube along its own axis and not some arbitrary position. The cube is a collection of other 27 cubes and I have succesfully managed to rotate the group of cubes but not in correct way. I mean when I rotated the cube in x-axis, it makes an orbit around the enter (0,0,0) and not in its own axis. How can i make the cube rotate about its own axis?

public void onDrawFrame(GL10 gl) 
{
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glScalef(0.8f, 0.8f, 0.8f);  
    int k=0;

    gl.glPushMatrix();
    gl.glRotatef(cubeRotX, 0.0f, 0.0f , 1.0f);
    gl.glRotatef(cubeRotY, 0.0f, 1.0f , 0.0f);
    for(int l=0; l<3; l++)
    {
        if(l == 2)  //To rotate only the first front polygon in 1.7f angle
        {
            gl.glPushMatrix();
            gl.glRotatef(rot, 0.0f, 0.0f, 1.0f);
        }
        for(int i=0; i<3; i++)
        {               
            for(int j=0; j<3; j++)
            {
                gl.glPushMatrix();
                gl.glTranslatef(-2.1f+(2.1f*i), -2.1f+(2.1f*j), -23.1f+(2.1f*l));
                cube[k++].draw(gl);
                gl.glPopMatrix();
            }

        }
        if(l ==2)
        {
            gl.glPopMatrix();
            if(rot >= 90.0f)
                rot = 90.0f;
            else
                rot += 4.0f;  
        }
    }
    gl.glPopMatrix();
    cubeRotX -= 5.0f;
    cubeRotY -= 5.0f;
}
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
neaGaze
  • 1,381
  • 22
  • 28
  • possible duplicate of [Is it possible to rotate an object around its own axis and not around the base coordinate's axis?](http://stackoverflow.com/questions/7724840/is-it-possible-to-rotate-an-object-around-its-own-axis-and-not-around-the-base-c) – SJuan76 May 07 '13 at 10:14
  • I think the difference is that in above link, rotation is done only for a single object but I have a group of objects to be rotated. How can I rotate a group of objects in their own axis? Would you mind take a look at my code? – neaGaze May 07 '13 at 10:23

1 Answers1

0

To rotate each individual cube, apply a rotation inside the inner glPushMatrix.

EDIT: I missundertood you. More information asked.

After your clarifications:

  • Load Identity
  • Apply general transform for all objects
  • Push matrix
  • Apply rotation for all child cubes around sun and the sun itself (a general rotation for all cubes around the sun, probably this could be no rotation at all depending on what do you want to archive)
  • Push matrix
  • Apply revolution for your "sun".
  • draw sun
  • Pop Matrix
  • For each inner/child cube:
    • Push matrix
    • Apply rot
    • draw cube
    • pop matrix
  • Pop Matrix
Trax
  • 1,890
  • 12
  • 15
  • I am not trying to rotate an individual cube but to rotate all the cubes as a whole around its own axis. I am facing problem just after the first glPushMatrix(which applies to the entire cube) where there are 2 Rotations i.e. through X and through Y. I am not able to rotate that around its own axis. It makes an orbit. – neaGaze May 07 '13 at 11:03
  • You said: " I mean when I rotated the cube in x-axis, it makes an orbit around the enter (0,0,0) and not in its own axis". What is supposed to be its own axis? I cannot get the idea of what are you trying to archive. But probably you need to adjust something at the begging of your function. – Trax May 07 '13 at 11:07
  • Lets put it this way: our earth both revolves (around the sun) and rotates(around its own axis). The way I want to achieve is only rotation not revolution of the entire cube. Here if we take (0,0,0) as the sun then the cube is actually revolving around it.I don't want that. how can it be done? – neaGaze May 07 '13 at 11:13
  • You would need to draw that cube (sun) not in the inner part of your function, but for example after rendering all (child) objects. I will try to give you an example when I find 10mins of my time. – Trax May 07 '13 at 11:16
  • I really appreciate your time but I am not trying to build a solar system. I am actually trying to make a rubik cube and its my initiative in opengl.The way I am trying to achieve it is by constructing all the 27 cubes and using Push/PopMatrices to group the smaller cubes. All my inner cubes work just fine. I am having problem at the rotation part right at the 4th step of your suggestion. The rotation actually causes the entire cube to rotate around the orbit with enter (0,0,0). That is my only problem. Once again thank you for your valuable time – neaGaze May 07 '13 at 11:35
  • I also tried glTranslate(0.0f, 0.0f, 0.0f) right after the rotation but it wont work. – neaGaze May 07 '13 at 11:38