Rotating a slice of a Rubik cube


I’m just writing a Rubik cube in C++ and OpenGL. It’s the first time I use OpenGL, so I don’t see clearly what things I have to model manually, and what I can have OpenGL doing it for me.

First thing I did was to draw a Rubik cube. I used Cube objects, each having 6 Side objects, and for each side I keep the information of 4 Vertex objects. So in the end I am drawing those sides using GL_QUADS [Question: I’m not sure I need that Vertex class, or I could simply use an OpenGL data structure for the vertices information]

Now, when I want to rotate a Slice of the Rubik cube, I am not sure if I have to manually rotate all those vertices, or I can have OpenGL doing it for me. The two options I’m thinking about are:

  1. I have a Slice::rotate that will do a 90º rotation in steps. It runs in a separate thread, so that the display function can be called during the rotation, and each step be shown. For each step, it calls Cube::rotate(Vertex rotationAxis, angle). Then, in Cube::rotate, I have to update the cube’s position, and call Side::rotate for each of the sides, which will basically update all the vertices.

I think I need loads of matrix algebra here: creating vectors (rotationAxis->vertexToRotate), rotating those vectors, and updating them [Question: do you know if boost ublas would be a good option?]

  1. I could simply rotate a slice with some OpenGL code in Slice::rotate. Something like:

    glTranslatef(center.getX(), center.getY(), center.getZ());
    glRotatef(angle, plane == x, plane == y, plane == z);
    glTranslatef(-center.getX(), -center.getY(), -center.getZ());

Then, I would just update my model information (cubes’ positions, side’s vertices) at the end of a rotation. The problem I see here is the display function. If it’s called in the middle of a rotation, I should keep account of those cubes that are being moved, so that when I am about to draw them, I do all those OpenGL transforms.

[Question: what option to go for, if any of these two? :slight_smile: My intuition tells me it’ll have to be the first one, but…]


I’ve got a little Rubik’s cube game I’ve been fiddling with. I use a set of beveled boxes (single instance translated and rotated to taste) and quaternions to rotate the slices. With quats you can spin all day without error propogation, just renormalize after each turn (nice if you plan to animate the turn slowly over time).

Convenient user interface is the tricky bit, imho :wink:

I´m writing a Rubik cube in C++ and OpenGL.
I already tried whit glRotatef() but it does not work…Now i´m using a class quaternion i wrote for quaternion rotation, work´s every time, but I want to rotate 90 degrees each slice and it rotates like 95°… the slice doesn’t match up whit the rest of the cube…

I was figuring out how to fix it and i came across this:

can someone explain to me what is that i have to renormalize??
is it the cube vertexes after rotating? or the vertex of the rotation?
I already normalize the vertex so i´m a little lost here.

I was normalizing the cube vertexes but it makes each little cube (of the 27) smaller and doesn´t fix the problem…

any suggestions?

I´ve already managed to fix the 90 degree problem by sending an angle of 1.5708.
but when i rotate more than 1 axis, for example first on x 90° and then on z 90° it rotates the second stack considering other axis…

still thinking what to renormalize.

Perhaps this will help: