Question

I'm using the Kube application from Android API Samples for an application I'm working on.

The sample creates a model of the Rubik's Cube in OpenGL made out of 27 small cubes. Afterwards the cube is being scrambled in the animate() method by rotating random layers of the cube, after each rotation the layers are being set accordingly in a process I don't fully understand

 // adjust mPermutation based on the completed layer rotation
             int[] newPermutation = new int[27];
             for (int i = 0; i < 27; i++) {
                newPermutation[i] = mPermutation[mCurrentLayerPermutation[i]];
 //                     newPermutation[i] = mCurrentLayerPermutation[mPermutation[i]];
             }
             mPermutation = newPermutation;
             updateLayers();

The sample works fine but for some reason when I change the direction of the rotation to true, the cubes start overlapping each other, as if the layers weren't updated correctly.
Help would be appreciated :) Cubes overlapping

Was it helpful?

Solution

I solved the problem myself, so in case anyone is interested:
changing the direction to true makes the cube rotate in the other direction, which is like rotating three times in the regular direction so in order to save to cubes new location correctly I need to save the new location three times, as seen in the code below:

             if (mAngleIncrement < 0) { // checks the turning direction
             int[] newPermutation = new int[27];
             for (int i = 0; i < 27; i++) {
                newPermutation[i] = mPermutation[mCurrentLayerPermutation[i]];

             }
             mPermutation = newPermutation;
             updateLayers();
         }
         else {
             int[] newPermutation = new int[27];
             for (int i = 0; i < 27; i++) {
                 newPermutation[i] = mPermutation[mCurrentLayerPermutation[i]];
             }
             mPermutation = newPermutation;

             newPermutation = new int[27];
             for (int i = 0; i < 27; i++) {
                 newPermutation[i] = mPermutation[mCurrentLayerPermutation[i]];
             }
             mPermutation = newPermutation;

             newPermutation = new int[27];
             for (int i = 0; i < 27; i++) {
                 newPermutation[i] = mPermutation[mCurrentLayerPermutation[i]];
             }
             mPermutation = newPermutation;
             updateLayers();

         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top