PDA

View Full Version : Rotating a slice of a Rubik cube



custard
01-21-2009, 04:43 AM
Hi,

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?]

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

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glTranslatef(center.getX(), center.getY(), center.getZ());
glRotatef(angle, plane == x, plane == y, plane == z);
glTranslatef(-center.getX(), -center.getY(), -center.getZ());
draw();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

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? :) My intuition tells me it'll have to be the first one, but...]

Thanks,
custard

Brolingstanz
01-21-2009, 05:36 AM
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 ;-)

Dave_IM
05-05-2011, 10:38 PM
Hi
Im writing a Rubik cube in C++ and OpenGL.
I already tried whit glRotatef() but it does not work...Now im using a class quaternion i wrote for quaternion rotation, works 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:



just renormalize after each turn


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 im a little lost here.

I was normalizing the cube vertexes but it makes each little cube (of the 27) smaller and doesnt fix the problem..

any suggestions?

Dave_IM
05-06-2011, 09:19 AM
Ive 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.
??

Khronos_webmaster
05-06-2011, 09:59 AM
Perhaps this will help:
http://gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_ro tation#Normalizing_a_quaternion