Project

General

Profile

Download (8.24 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / object / RubikCube.java @ 39e74052

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 beb325a0 Leszek Koltunski
package org.distorted.object;
21 0c52af30 Leszek Koltunski
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24
25 411c6285 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectRotate;
28 f0fa83ae Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
29 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
31 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
32 411c6285 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
33 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
34 411c6285 Leszek Koltunski
import org.distorted.library.type.Static1D;
35 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 27a70eae Leszek Koltunski
class RubikCube extends RubikObject
41 0c52af30 Leszek Koltunski
{
42 e844c116 Leszek Koltunski
  // the three rotation axis of a RubikCube. Must be normalized.
43 9cd7695f Leszek Koltunski
  static final Static3D[] AXIS = new Static3D[]
44 efef689c Leszek Koltunski
         {
45
           new Static3D(1,0,0),
46
           new Static3D(0,1,0),
47
           new Static3D(0,0,1)
48
         };
49
50 37a25788 Leszek Koltunski
  private static final int[] FACE_COLORS = new int[]
51 efef689c Leszek Koltunski
         {
52
           0xffffff00, 0xffffffff,   // AXIS[0]right (right-YELLOW) AXIS[0]left (left  -WHITE)
53
           0xff0000ff, 0xff00ff00,   // AXIS[1]right (top  -BLUE  ) AXIS[1]left (bottom-GREEN)
54
           0xffff0000, 0xffb5651d    // AXIS[2]right (front-RED   ) AXIS[2]left (back  -BROWN)
55
         };
56
57
  // All legal rotation quats of a RubikCube of any size must have all four of their components
58
  // equal to either 0, +-1, +-0.5 or +-sqrt(2)/2.
59
  // Here's how to compute this:
60
  // 1) compute how many rotations there are (RubikCube of any size = 24)
61
  // 2) take the AXIS, angles of rotation (90 in RubikCube's case) compute the basic quaternions
62
  // (i.e. rotations of 1 basic angle along each of the axis) and from there start semi-randomly
63
  // multiplying them and eventually you'll find all (24) legal rotations.
64
  // 3) linear scan through those shows that the only floats in those 24 quats are those 7 given
65
  // below.
66 9f4c44fe Leszek Koltunski
  //
67
  // Example program in C, res/raw/compute_quats.c , is included.
68 efef689c Leszek Koltunski
  private static final float[] LEGALQUATS = new float[]
69
         {
70
           0.0f ,
71
           0.5f ,
72
          -0.5f ,
73
           1.0f ,
74
          -1.0f ,
75
           0.5f*((float)Math.sqrt(2)) ,
76
          -0.5f*((float)Math.sqrt(2))
77
         };
78 411c6285 Leszek Koltunski
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
82
    {
83
    super(size,quatCur,quatAcc,texture,mesh,effects);
84
    }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 10a2e360 Leszek Koltunski
  Static3D[] getCubitPositions(int size)
89 a10ada2a Leszek Koltunski
    {
90 10a2e360 Leszek Koltunski
    int numCubits = size>1 ? 6*size*size - 12*size + 8 : 1;
91
    Static3D[] tmp = new Static3D[numCubits];
92 beb325a0 Leszek Koltunski
93 49f67f9b Leszek Koltunski
    float diff = 0.5f*(size-1);
94 a10ada2a Leszek Koltunski
    int currentPosition = 0;
95 beb325a0 Leszek Koltunski
96 a10ada2a Leszek Koltunski
    for(int x = 0; x<size; x++)
97
      for(int y = 0; y<size; y++)
98
        for(int z = 0; z<size; z++)
99
          if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
100 beb325a0 Leszek Koltunski
            {
101 49f67f9b Leszek Koltunski
            tmp[currentPosition++] = new Static3D(x-diff,y-diff,z-diff);
102 a10ada2a Leszek Koltunski
            }
103 47ba5ddc Leszek Koltunski
104 a10ada2a Leszek Koltunski
    return tmp;
105
    }
106 47ba5ddc Leszek Koltunski
107 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109 a10ada2a Leszek Koltunski
  float[] getLegalQuats()
110
    {
111 efef689c Leszek Koltunski
    return LEGALQUATS;
112 a10ada2a Leszek Koltunski
    }
113 47ba5ddc Leszek Koltunski
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 411c6285 Leszek Koltunski
  int getNumFaces()
117 a10ada2a Leszek Koltunski
    {
118 411c6285 Leszek Koltunski
    return FACE_COLORS.length;
119
    }
120 47ba5ddc Leszek Koltunski
121 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123
  float getScreenRatio()
124
    {
125
    return 0.5f;
126
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
  VertexEffectSink getSink(int size)
131
    {
132
    Static3D center = new Static3D(0,0,0);
133
    Static4D region = new Static4D(0,0,0,0.72f);
134
    float strength;
135
136
    switch(size)
137
      {
138
      case 1 : strength= 1.1f; break;
139
      case 2 : strength= 1.5f; break;
140
      case 3 : strength= 1.8f; break;
141
      case 4 : strength= 2.0f; break;
142
      default: strength= 3.0f - 4.0f/size;
143
      }
144
145
    return new VertexEffectSink( new Static1D(strength), center, region );
146
    }
147
148 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  Static3D[] getRotationAxis()
151
    {
152
    return AXIS;
153
    }
154
155 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156 f174b34a Leszek Koltunski
// paint only the square with lower-left corner at (face*TEX_H,0) and side length TEX_H
157 411c6285 Leszek Koltunski
158
  void createFaceTexture(Canvas canvas, Paint paint, int face)
159
    {
160
    final int S = TEXTURE_HEIGHT;
161
    final int R = TEXTURE_HEIGHT/10;
162
    final int M = TEXTURE_HEIGHT/20;
163
164
    paint.setColor(FACE_COLORS[face]);
165
    canvas.drawRoundRect( (face*S+M), M, (face*S+M) + (S-2*M), M + (S-2*M), R, R, paint);
166 a10ada2a Leszek Koltunski
    }
167 70b76549 Leszek Koltunski
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 89a11f7b Leszek Koltunski
  MeshBase createCubitMesh(int cubit, int vertices)
171 a10ada2a Leszek Koltunski
    {
172 411c6285 Leszek Koltunski
    final int MESHES=6;
173
174
    Static3D axisY  = new Static3D(0,1,0);
175
    Static3D axisX  = new Static3D(1,0,0);
176
    Static3D center = new Static3D(0,0,0);
177
    Static1D angle  = new Static1D(0);
178
179
    MatrixEffect[] effectsY = new MatrixEffect[2];
180
    effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
181
    effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
182
183
    MeshBase[] meshes = new MeshRectangles[MESHES];
184
    for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(vertices,vertices);
185
186
    angle.set(0);
187
    meshes[4].apply(effectsY);  // front
188
    angle.set(90);
189
    meshes[0].apply(effectsY);  // right
190
    angle.set(180);
191
    meshes[5].apply(effectsY);  // back
192
    angle.set(270);
193
    meshes[1].apply(effectsY);  // left
194
195
    MatrixEffect[] effectsX = new MatrixEffect[2];
196
    effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
197
    effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
198
199
    angle.set( 90);
200
    meshes[3].apply(effectsX);  // bottom
201
    angle.set(-90);
202
    meshes[2].apply(effectsX);  // top
203
204
    return new MeshJoined(meshes);
205 a10ada2a Leszek Koltunski
    }
206 e844c116 Leszek Koltunski
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// PUBLIC API
209
210
  public int getBasicAngle()
211
    {
212
    return 4;
213
    }
214 39e74052 Leszek Koltunski
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public int returnRowFromOffset(float offset)
218
    {
219
    return (int)(getSize()*offset);
220
    }
221 0c52af30 Leszek Koltunski
}