Results 1 to 5 of 5

Thread: Rotating a slice of a Rubik cube

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Rotating a slice of a Rubik cube

    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

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Sep 2004
    Location
    Prombaatu
    Posts
    1,386

    Re: Rotating a slice of a Rubik cube

    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

  3. #3
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    2

    Re: Rotating a slice of a Rubik cube

    Hi
    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:

    Quote Originally Posted by Brolingstanz
    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 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?

  4. #4
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    2

    Re: Rotating a slice of a Rubik cube

    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.
    ┐┐??

  5. #5
    Administrator Regular Contributor khronos's Avatar
    Join Date
    Feb 2000
    Location
    Montreal
    Posts
    349

    Re: Rotating a slice of a Rubik cube


Similar Threads

  1. Help for Rubik's Cube Rotation of each slice...
    By sabith in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-19-2011, 11:29 AM
  2. Rotation Problem with Rubik┤s Cube
    By faMouZ* in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 05-26-2009, 03:51 AM
  3. creating random rotations for a rubik's cube
    By AstroJazz in forum OpenGL: General
    Replies: 1
    Last Post: 04-10-2003, 12:16 PM
  4. scrambling a rubik's cube using srand() C++ and opengl
    By AstroJazz in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-09-2003, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean