Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCube.java @ 411c6285

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 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedTexture;
30 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
31 411c6285 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
32 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
33 411c6285 Leszek Koltunski
import org.distorted.library.type.Static1D;
34 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 27a70eae Leszek Koltunski
class RubikCube extends RubikObject
40 0c52af30 Leszek Koltunski
{
41 411c6285 Leszek Koltunski
  private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
42
  private static final float[] LEGAL = new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
43
44
  // axisXright (right-YELLOW) axisXleft (left-WHITE) axisYright (top-BLUE) axisYleft (bottom-GREEN) axisZright (front-RED) axisZleft (back-BROWN)
45
  private static final int[] FACE_COLORS   = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
50
    {
51
    super(size,quatCur,quatAcc,texture,mesh,effects);
52
    }
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 a10ada2a Leszek Koltunski
  int[][] getCubitPositions(int size)
57
    {
58
    int[][] tmp = new int[getNumCubits(size)][3];
59 beb325a0 Leszek Koltunski
60 a10ada2a Leszek Koltunski
    int currentPosition = 0;
61 beb325a0 Leszek Koltunski
62 a10ada2a Leszek Koltunski
    for(int x = 0; x<size; x++)
63
      for(int y = 0; y<size; y++)
64
        for(int z = 0; z<size; z++)
65
          {
66
          if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
67 beb325a0 Leszek Koltunski
            {
68 a10ada2a Leszek Koltunski
            tmp[currentPosition][0] = x;
69
            tmp[currentPosition][1] = y;
70
            tmp[currentPosition][2] = z;
71 0c52af30 Leszek Koltunski
72 a10ada2a Leszek Koltunski
            currentPosition++;
73
            }
74
          }
75 47ba5ddc Leszek Koltunski
76 a10ada2a Leszek Koltunski
    return tmp;
77
    }
78 47ba5ddc Leszek Koltunski
79 b32444ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we don't create or
81
// render the inside)
82
83
  int getNumCubits(int size)
84
    {
85
    return size>1 ? 6*size*size - 12*size + 8 : 1;
86
    }
87
88 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89 a10ada2a Leszek Koltunski
// All legal rotation quats of a RubikCube of any size must have all four of their components
90
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
91 beb325a0 Leszek Koltunski
92 a10ada2a Leszek Koltunski
  float[] getLegalQuats()
93
    {
94 411c6285 Leszek Koltunski
    return LEGAL;
95 a10ada2a Leszek Koltunski
    }
96 47ba5ddc Leszek Koltunski
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 411c6285 Leszek Koltunski
  int getNumFaces()
100 a10ada2a Leszek Koltunski
    {
101 411c6285 Leszek Koltunski
    return FACE_COLORS.length;
102
    }
103 47ba5ddc Leszek Koltunski
104 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  void createFaceTexture(Canvas canvas, Paint paint, int face)
107
    {
108
    final int S = TEXTURE_HEIGHT;
109
    final int R = TEXTURE_HEIGHT/10;
110
    final int M = TEXTURE_HEIGHT/20;
111
112
    paint.setColor(FACE_COLORS[face]);
113
    canvas.drawRoundRect( (face*S+M), M, (face*S+M) + (S-2*M), M + (S-2*M), R, R, paint);
114 a10ada2a Leszek Koltunski
    }
115 70b76549 Leszek Koltunski
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 411c6285 Leszek Koltunski
  MeshBase createCubitMesh(int vertices)
119 a10ada2a Leszek Koltunski
    {
120 411c6285 Leszek Koltunski
    final int MESHES=6;
121
122
    Static3D axisY  = new Static3D(0,1,0);
123
    Static3D axisX  = new Static3D(1,0,0);
124
    Static3D center = new Static3D(0,0,0);
125
    Static1D angle  = new Static1D(0);
126
127
    MatrixEffect[] effectsY = new MatrixEffect[2];
128
    effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
129
    effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
130
131
    MeshBase[] meshes = new MeshRectangles[MESHES];
132
    for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(vertices,vertices);
133
134
    angle.set(0);
135
    meshes[4].apply(effectsY);  // front
136
    angle.set(90);
137
    meshes[0].apply(effectsY);  // right
138
    angle.set(180);
139
    meshes[5].apply(effectsY);  // back
140
    angle.set(270);
141
    meshes[1].apply(effectsY);  // left
142
143
    MatrixEffect[] effectsX = new MatrixEffect[2];
144
    effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
145
    effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
146
147
    angle.set( 90);
148
    meshes[3].apply(effectsX);  // bottom
149
    angle.set(-90);
150
    meshes[2].apply(effectsX);  // top
151
152
    return new MeshJoined(meshes);
153 a10ada2a Leszek Koltunski
    }
154 70b76549 Leszek Koltunski
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 411c6285 Leszek Koltunski
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
158 a10ada2a Leszek Koltunski
    {
159 411c6285 Leszek Koltunski
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
160
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
161
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
162
163
    return false;
164 a10ada2a Leszek Koltunski
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 b32444ee Leszek Koltunski
  private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
169
170
  static
171
    {
172 411c6285 Leszek Koltunski
    // axisXright (right-YELLOW) axisXleft (left-WHITE) axisYright (top-BLUE) axisYleft (bottom-GREEN) axisZright (front-RED) axisZleft (back-BROWN)
173
174
    mapRight = new Static4D( 0*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
175
    mapLeft  = new Static4D( 1*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
176
    mapTop   = new Static4D( 2*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
177
    mapBottom= new Static4D( 3*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
178
    mapFront = new Static4D( 4*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
179
    mapBack  = new Static4D( 5*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
180
    mapBlack = new Static4D( 6*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
181 b32444ee Leszek Koltunski
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185 411c6285 Leszek Koltunski
  void textureCubitMesh(MeshBase mesh, int x, int y, int z)
186 a10ada2a Leszek Koltunski
    {
187
    Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
188 411c6285 Leszek Koltunski
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
189 a10ada2a Leszek Koltunski
    Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
190
    Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
191 411c6285 Leszek Koltunski
    Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
192
    Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
193
194
    Static4D[] maps = new Static4D[] { tmpRight, tmpLeft, tmpTop, tmpBottom, tmpFront, tmpBack};
195 a10ada2a Leszek Koltunski
196 411c6285 Leszek Koltunski
    mesh.setTextureMap(maps);
197 a10ada2a Leszek Koltunski
    }
198 0c52af30 Leszek Koltunski
}