Revision efef689c
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/object/RubikCube.java | ||
---|---|---|
38 | 38 |
|
39 | 39 |
class RubikCube extends RubikObject |
40 | 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 }; |
|
41 |
// the three rotation axis of a RubikCube |
|
42 |
private static final Static3D[] AXIS = new Static3D[] |
|
43 |
{ |
|
44 |
new Static3D(1,0,0), |
|
45 |
new Static3D(0,1,0), |
|
46 |
new Static3D(0,0,1) |
|
47 |
}; |
|
48 |
|
|
49 |
private static final int[] FACE_COLORS = new int[] |
|
50 |
{ |
|
51 |
0xffffff00, 0xffffffff, // AXIS[0]right (right-YELLOW) AXIS[0]left (left -WHITE) |
|
52 |
0xff0000ff, 0xff00ff00, // AXIS[1]right (top -BLUE ) AXIS[1]left (bottom-GREEN) |
|
53 |
0xffff0000, 0xffb5651d // AXIS[2]right (front-RED ) AXIS[2]left (back -BROWN) |
|
54 |
}; |
|
55 |
|
|
56 |
// All legal rotation quats of a RubikCube of any size must have all four of their components |
|
57 |
// equal to either 0, +-1, +-0.5 or +-sqrt(2)/2. |
|
58 |
// Here's how to compute this: |
|
59 |
// 1) compute how many rotations there are (RubikCube of any size = 24) |
|
60 |
// 2) take the AXIS, angles of rotation (90 in RubikCube's case) compute the basic quaternions |
|
61 |
// (i.e. rotations of 1 basic angle along each of the axis) and from there start semi-randomly |
|
62 |
// multiplying them and eventually you'll find all (24) legal rotations. |
|
63 |
// 3) linear scan through those shows that the only floats in those 24 quats are those 7 given |
|
64 |
// below. |
|
65 |
private static final float[] LEGALQUATS = new float[] |
|
66 |
{ |
|
67 |
0.0f , |
|
68 |
0.5f , |
|
69 |
-0.5f , |
|
70 |
1.0f , |
|
71 |
-1.0f , |
|
72 |
0.5f*((float)Math.sqrt(2)) , |
|
73 |
-0.5f*((float)Math.sqrt(2)) |
|
74 |
}; |
|
46 | 75 |
|
47 | 76 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
48 | 77 |
|
... | ... | |
86 | 115 |
} |
87 | 116 |
|
88 | 117 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
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 | 118 |
|
92 | 119 |
float[] getLegalQuats() |
93 | 120 |
{ |
94 |
return LEGAL; |
|
121 |
return LEGALQUATS;
|
|
95 | 122 |
} |
96 | 123 |
|
97 | 124 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
101 | 128 |
return FACE_COLORS.length; |
102 | 129 |
} |
103 | 130 |
|
131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
132 |
|
|
133 |
Static3D[] getRotationAxis() |
|
134 |
{ |
|
135 |
return AXIS; |
|
136 |
} |
|
137 |
|
|
104 | 138 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
105 | 139 |
|
106 | 140 |
void createFaceTexture(Canvas canvas, Paint paint, int face) |
... | ... | |
151 | 185 |
|
152 | 186 |
return new MeshJoined(meshes); |
153 | 187 |
} |
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 | 188 |
} |
Also available in: Unified diff
Progress towards generalizing belongsToRotation()