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.Bitmap;
|
23 |
|
|
import android.graphics.Canvas;
|
24 |
|
|
import android.graphics.Paint;
|
25 |
|
|
|
26 |
|
|
import org.distorted.library.main.DistortedEffects;
|
27 |
|
|
import org.distorted.library.main.DistortedTexture;
|
28 |
|
|
import org.distorted.library.mesh.MeshCubes;
|
29 |
97c012ae
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
30 |
0c52af30
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
31 |
|
|
import org.distorted.library.type.Static4D;
|
32 |
|
|
|
33 |
27a70eae
|
Leszek Koltunski
|
import static org.distorted.object.RubikObjectList.VECTX;
|
34 |
|
|
import static org.distorted.object.RubikObjectList.VECTY;
|
35 |
|
|
import static org.distorted.object.RubikObjectList.VECTZ;
|
36 |
|
|
|
37 |
0c52af30
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38 |
|
|
|
39 |
27a70eae
|
Leszek Koltunski
|
class RubikCube extends RubikObject
|
40 |
0c52af30
|
Leszek Koltunski
|
{
|
41 |
a10ada2a
|
Leszek Koltunski
|
private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
|
42 |
0c52af30
|
Leszek Koltunski
|
|
43 |
a10ada2a
|
Leszek Koltunski
|
static
|
44 |
|
|
{
|
45 |
|
|
// 3x2 bitmap = 6 squares:
|
46 |
|
|
//
|
47 |
|
|
// RED GREEN BLUE
|
48 |
|
|
// YELLOW WHITE BROWN
|
49 |
0c52af30
|
Leszek Koltunski
|
|
50 |
a10ada2a
|
Leszek Koltunski
|
final float ze = 0.0f;
|
51 |
|
|
final float ot = 1.0f/3.0f;
|
52 |
|
|
final float tt = 2.0f/3.0f;
|
53 |
|
|
final float oh = 1.0f/2.0f;
|
54 |
|
|
final float of = 1.0f/40.0f;
|
55 |
47ba5ddc
|
Leszek Koltunski
|
|
56 |
a10ada2a
|
Leszek Koltunski
|
mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
|
57 |
|
|
mapBack = new Static4D(tt,ze, tt+ot,ze+oh);
|
58 |
|
|
mapLeft = new Static4D(ot,ze, ot+ot,ze+oh);
|
59 |
|
|
mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
|
60 |
|
|
mapTop = new Static4D(tt,oh, tt+ot,oh+oh);
|
61 |
|
|
mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
|
62 |
9208e27b
|
Leszek Koltunski
|
|
63 |
a10ada2a
|
Leszek Koltunski
|
mapBlack = new Static4D(ze,ze, ze+of,ze+of);
|
64 |
|
|
}
|
65 |
f16ff19d
|
Leszek Koltunski
|
|
66 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67 |
a10ada2a
|
Leszek Koltunski
|
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we dont create or
|
68 |
|
|
// render the inside0
|
69 |
f16ff19d
|
Leszek Koltunski
|
|
70 |
a10ada2a
|
Leszek Koltunski
|
int getNumCubits(int size)
|
71 |
|
|
{
|
72 |
|
|
return 6*size*size - 12*size + 8;
|
73 |
|
|
}
|
74 |
434f2f5a
|
Leszek Koltunski
|
|
75 |
beb325a0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
a10ada2a
|
Leszek Koltunski
|
int[][] getCubitPositions(int size)
|
78 |
|
|
{
|
79 |
|
|
int[][] tmp = new int[getNumCubits(size)][3];
|
80 |
beb325a0
|
Leszek Koltunski
|
|
81 |
a10ada2a
|
Leszek Koltunski
|
int currentPosition = 0;
|
82 |
beb325a0
|
Leszek Koltunski
|
|
83 |
a10ada2a
|
Leszek Koltunski
|
for(int x = 0; x<size; x++)
|
84 |
|
|
for(int y = 0; y<size; y++)
|
85 |
|
|
for(int z = 0; z<size; z++)
|
86 |
|
|
{
|
87 |
|
|
if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 )
|
88 |
beb325a0
|
Leszek Koltunski
|
{
|
89 |
a10ada2a
|
Leszek Koltunski
|
tmp[currentPosition][0] = x;
|
90 |
|
|
tmp[currentPosition][1] = y;
|
91 |
|
|
tmp[currentPosition][2] = z;
|
92 |
0c52af30
|
Leszek Koltunski
|
|
93 |
a10ada2a
|
Leszek Koltunski
|
currentPosition++;
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
47ba5ddc
|
Leszek Koltunski
|
|
97 |
a10ada2a
|
Leszek Koltunski
|
return tmp;
|
98 |
|
|
}
|
99 |
47ba5ddc
|
Leszek Koltunski
|
|
100 |
beb325a0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
a10ada2a
|
Leszek Koltunski
|
// All legal rotation quats of a RubikCube of any size must have all four of their components
|
102 |
|
|
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
|
103 |
beb325a0
|
Leszek Koltunski
|
|
104 |
a10ada2a
|
Leszek Koltunski
|
float[] getLegalQuats()
|
105 |
|
|
{
|
106 |
|
|
final float SQ2 = 0.5f*((float)Math.sqrt(2));
|
107 |
|
|
return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
|
108 |
|
|
}
|
109 |
47ba5ddc
|
Leszek Koltunski
|
|
110 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111 |
|
|
|
112 |
a10ada2a
|
Leszek Koltunski
|
boolean belongsToRotation( Static3D currentPosition, int vector, int row)
|
113 |
|
|
{
|
114 |
|
|
switch(vector)
|
115 |
47ba5ddc
|
Leszek Koltunski
|
{
|
116 |
a10ada2a
|
Leszek Koltunski
|
case VECTX: return currentPosition.get0()==row;
|
117 |
|
|
case VECTY: return currentPosition.get1()==row;
|
118 |
|
|
case VECTZ: return currentPosition.get2()==row;
|
119 |
47ba5ddc
|
Leszek Koltunski
|
}
|
120 |
|
|
|
121 |
a10ada2a
|
Leszek Koltunski
|
return false;
|
122 |
|
|
}
|
123 |
70b76549
|
Leszek Koltunski
|
|
124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
|
126 |
a10ada2a
|
Leszek Koltunski
|
RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
|
127 |
|
|
{
|
128 |
|
|
super(size,quatCur,quatAcc,texture,mesh,effects);
|
129 |
|
|
}
|
130 |
70b76549
|
Leszek Koltunski
|
|
131 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132 |
a10ada2a
|
Leszek Koltunski
|
// PUBLIC API
|
133 |
70b76549
|
Leszek Koltunski
|
|
134 |
a10ada2a
|
Leszek Koltunski
|
public void createTexture()
|
135 |
|
|
{
|
136 |
|
|
Bitmap bitmap;
|
137 |
|
|
|
138 |
|
|
final int S = 128;
|
139 |
|
|
final int W = 3*S;
|
140 |
|
|
final int H = 2*S;
|
141 |
|
|
final int R = S/10;
|
142 |
|
|
final int M = S/20;
|
143 |
|
|
|
144 |
|
|
Paint paint = new Paint();
|
145 |
|
|
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
|
146 |
|
|
Canvas canvas = new Canvas(bitmap);
|
147 |
|
|
|
148 |
|
|
paint.setAntiAlias(true);
|
149 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
150 |
|
|
paint.setStyle(Paint.Style.FILL);
|
151 |
|
|
|
152 |
|
|
// 3x2 bitmap = 6 squares:
|
153 |
|
|
//
|
154 |
|
|
// RED GREEN BLUE
|
155 |
|
|
// YELLOW WHITE BROWN
|
156 |
|
|
|
157 |
|
|
paint.setColor(0xff000000); // BLACK BACKGROUND
|
158 |
|
|
canvas.drawRect(0, 0, W, H, paint); //
|
159 |
|
|
|
160 |
|
|
paint.setColor(0xffff0000); // RED
|
161 |
|
|
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); //
|
162 |
|
|
paint.setColor(0xff00ff00); // GREEN
|
163 |
|
|
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); //
|
164 |
|
|
paint.setColor(0xff0000ff); // BLUE
|
165 |
|
|
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); //
|
166 |
|
|
paint.setColor(0xffffff00); // YELLOW
|
167 |
|
|
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); //
|
168 |
|
|
paint.setColor(0xffffffff); // WHITE
|
169 |
|
|
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
|
170 |
|
|
paint.setColor(0xffb5651d); // BROWN
|
171 |
|
|
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
|
172 |
|
|
|
173 |
|
|
mTexture.setTexture(bitmap);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
|
|
178 |
|
|
MeshCubes createMesh(int vertices,int x, int y, int z)
|
179 |
|
|
{
|
180 |
|
|
Static4D tmpLeft = (x== 0 ? mapLeft :mapBlack);
|
181 |
|
|
Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
|
182 |
|
|
Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
|
183 |
|
|
Static4D tmpBack = (z== 0 ? mapBack :mapBlack);
|
184 |
|
|
Static4D tmpTop = (y== mSize-1 ? mapTop :mapBlack);
|
185 |
|
|
Static4D tmpBottom= (y== 0 ? mapBottom:mapBlack);
|
186 |
|
|
|
187 |
|
|
return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
|
188 |
|
|
}
|
189 |
0c52af30
|
Leszek Koltunski
|
}
|