Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// 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
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// 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
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.object;
21

    
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24

    
25
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectRotate;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshBase;
31
import org.distorted.library.mesh.MeshJoined;
32
import org.distorted.library.mesh.MeshRectangles;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class RubikCube extends RubikObject
40
{
41
  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
  int[][] getCubitPositions(int size)
57
    {
58
    int[][] tmp = new int[getNumCubits(size)][3];
59

    
60
    int currentPosition = 0;
61

    
62
    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
            {
68
            tmp[currentPosition][0] = x;
69
            tmp[currentPosition][1] = y;
70
            tmp[currentPosition][2] = z;
71

    
72
            currentPosition++;
73
            }
74
          }
75

    
76
    return tmp;
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// 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

    
92
  float[] getLegalQuats()
93
    {
94
    return LEGAL;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  int getNumFaces()
100
    {
101
    return FACE_COLORS.length;
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  MeshBase createCubitMesh(int vertices)
119
    {
120
    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
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
158
    {
159
    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
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
169

    
170
  static
171
    {
172
    // 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
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  void textureCubitMesh(MeshBase mesh, int x, int y, int z)
186
    {
187
    Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
188
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
189
    Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
190
    Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
191
    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

    
196
    mesh.setTextureMap(maps);
197
    }
198
}
(2-2/6)