1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted 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
|
// Distorted 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 Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.magic;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.Canvas;
|
24
|
import android.graphics.Paint;
|
25
|
|
26
|
import org.distorted.library.effect.Effect;
|
27
|
import org.distorted.library.effect.EffectType;
|
28
|
import org.distorted.library.effect.MatrixEffectMove;
|
29
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
30
|
import org.distorted.library.effect.MatrixEffectRotate;
|
31
|
import org.distorted.library.effect.MatrixEffectScale;
|
32
|
import org.distorted.library.effect.VertexEffectSink;
|
33
|
import org.distorted.library.effectqueue.EffectQueue;
|
34
|
import org.distorted.library.main.DistortedEffects;
|
35
|
import org.distorted.library.main.DistortedNode;
|
36
|
import org.distorted.library.main.DistortedTexture;
|
37
|
import org.distorted.library.mesh.MeshCubes;
|
38
|
import org.distorted.library.mesh.MeshFlat;
|
39
|
import org.distorted.library.message.EffectListener;
|
40
|
import org.distorted.library.type.Dynamic1D;
|
41
|
import org.distorted.library.type.Static1D;
|
42
|
import org.distorted.library.type.Static3D;
|
43
|
import org.distorted.library.type.Static4D;
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
public class RubikCube extends DistortedNode
|
48
|
{
|
49
|
private static final int POST_ROTATION_MILLISEC = 500;
|
50
|
private static final int TEXTURE_SIZE = 100;
|
51
|
|
52
|
private static final Static3D VectX = new Static3D(1,0,0);
|
53
|
private static final Static3D VectY = new Static3D(0,1,0);
|
54
|
private static final Static3D VectZ = new Static3D(0,0,1);
|
55
|
|
56
|
public static final int VECTX = 0; //
|
57
|
public static final int VECTY = 1; // don't change this
|
58
|
public static final int VECTZ = 2; //
|
59
|
|
60
|
private DistortedNode[][][] mNodes;
|
61
|
private MeshCubes[][][] mCubes;
|
62
|
private DistortedEffects[][][] mEffects;
|
63
|
private Static4D[][][] mQuatScramble;
|
64
|
private Static3D[][][] mRotationAxis;
|
65
|
private Dynamic1D[][][] mRotationAngle;
|
66
|
private Static3D[][][] mCurrentPosition;
|
67
|
private MatrixEffectRotate[][][] mRotateEffect;
|
68
|
private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
|
69
|
private Static3D mMove, mScale, mNodeMove, mNodeScale;
|
70
|
private DistortedTexture mTexture;
|
71
|
|
72
|
private int mRotAxis, mRotRow;
|
73
|
private int mSize;
|
74
|
|
75
|
private DistortedTexture mNodeTexture;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshFlat mesh, DistortedEffects effects)
|
80
|
{
|
81
|
super(texture,effects,mesh);
|
82
|
|
83
|
mNodeTexture = texture;
|
84
|
|
85
|
mSize = size;
|
86
|
|
87
|
mRotationAngleStatic = new Static1D(0);
|
88
|
mRotationAngleMiddle = new Static1D(0);
|
89
|
mRotationAngleFinal = new Static1D(0);
|
90
|
|
91
|
mMove = new Static3D(0,0,0);
|
92
|
mScale = new Static3D(1,1,1);
|
93
|
mNodeMove = new Static3D(0,0,0);
|
94
|
mNodeScale= new Static3D(1,1,1);
|
95
|
|
96
|
mRotAxis = VECTX;
|
97
|
mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
|
98
|
|
99
|
mNodes = new DistortedNode[mSize][mSize][mSize];
|
100
|
mCubes = new MeshCubes[mSize][mSize][mSize];
|
101
|
mEffects = new DistortedEffects[mSize][mSize][mSize];
|
102
|
mQuatScramble = new Static4D[mSize][mSize][mSize];
|
103
|
mRotationAxis = new Static3D[mSize][mSize][mSize];
|
104
|
mRotationAngle = new Dynamic1D[mSize][mSize][mSize];
|
105
|
mCurrentPosition= new Static3D[mSize][mSize][mSize];
|
106
|
mRotateEffect = new MatrixEffectRotate[mSize][mSize][mSize];
|
107
|
|
108
|
Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
|
109
|
|
110
|
Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
|
111
|
Static3D matrCenter = new Static3D(0,0,0);
|
112
|
Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
|
113
|
|
114
|
VertexEffectSink sinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region );
|
115
|
MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
|
116
|
MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
|
117
|
MatrixEffectQuaternion quatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter);
|
118
|
MatrixEffectQuaternion quatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter);
|
119
|
|
120
|
MatrixEffectMove nodeMoveEffect = new MatrixEffectMove(mNodeMove);
|
121
|
MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
|
122
|
|
123
|
effects.apply(nodeScaleEffect);
|
124
|
effects.apply(nodeMoveEffect);
|
125
|
|
126
|
// 3x2 bitmap = 6 squares:
|
127
|
//
|
128
|
// RED GREEN BLUE
|
129
|
// YELLOW WHITE BROWN
|
130
|
|
131
|
final float ze = 0.0f;
|
132
|
final float ot = 1.0f/3.0f;
|
133
|
final float tt = 2.0f/3.0f;
|
134
|
final float oh = 1.0f/2.0f;
|
135
|
final float of = 1.0f/40.0f;
|
136
|
|
137
|
final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
|
138
|
final Static4D mapBack = new Static4D(tt,ze, tt+ot,ze+oh);
|
139
|
final Static4D mapLeft = new Static4D(ot,ze, ot+ot,ze+oh);
|
140
|
final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
|
141
|
final Static4D mapTop = new Static4D(tt,oh, tt+ot,oh+oh);
|
142
|
final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
|
143
|
|
144
|
final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
|
145
|
|
146
|
Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
|
147
|
float nc = 0.5f*mSize;
|
148
|
int vertices = (int)(24.0f/mSize + 2.0f);
|
149
|
|
150
|
for(int x = 0; x< mSize; x++)
|
151
|
for(int y = 0; y< mSize; y++)
|
152
|
for(int z = 0; z< mSize; z++)
|
153
|
{
|
154
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
|
155
|
{
|
156
|
tmpLeft = (x== 0 ? mapLeft :mapBlack);
|
157
|
tmpRight = (x== mSize-1 ? mapRight :mapBlack);
|
158
|
tmpFront = (z== mSize-1 ? mapFront :mapBlack);
|
159
|
tmpBack = (z== 0 ? mapBack :mapBlack);
|
160
|
tmpTop = (y== mSize-1 ? mapTop :mapBlack);
|
161
|
tmpBottom= (y== 0 ? mapBottom:mapBlack);
|
162
|
|
163
|
mCubes[x][y][z] = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
|
164
|
cubeVectors[x][y][z] = new Static3D( TEXTURE_SIZE*(x-nc), TEXTURE_SIZE*(y-nc), TEXTURE_SIZE*(z-nc) );
|
165
|
mQuatScramble[x][y][z] = new Static4D(0,0,0,1);
|
166
|
mRotationAngle[x][y][z] = new Dynamic1D();
|
167
|
mRotationAxis[x][y][z] = new Static3D(1,0,0);
|
168
|
mCurrentPosition[x][y][z] = new Static3D(x,y,z);
|
169
|
mRotateEffect[x][y][z] = new MatrixEffectRotate(mRotationAngle[x][y][z], mRotationAxis[x][y][z], matrCenter);
|
170
|
|
171
|
mEffects[x][y][z] = new DistortedEffects();
|
172
|
mEffects[x][y][z].apply(sinkEffect);
|
173
|
mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
|
174
|
mEffects[x][y][z].apply( new MatrixEffectQuaternion(mQuatScramble[x][y][z], matrCenter));
|
175
|
mEffects[x][y][z].apply(mRotateEffect[x][y][z]);
|
176
|
mEffects[x][y][z].apply(quatAEffect);
|
177
|
mEffects[x][y][z].apply(quatCEffect);
|
178
|
mEffects[x][y][z].apply(scaleEffect);
|
179
|
mEffects[x][y][z].apply(moveEffect);
|
180
|
|
181
|
mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
|
182
|
|
183
|
attach(mNodes[x][y][z]);
|
184
|
}
|
185
|
}
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
boolean isSolved()
|
191
|
{
|
192
|
Static4D q = mQuatScramble[0][0][0];
|
193
|
|
194
|
float x = q.get1();
|
195
|
float y = q.get2();
|
196
|
float z = q.get3();
|
197
|
float w = q.get4();
|
198
|
|
199
|
for(int i = 0; i< mSize; i++)
|
200
|
for(int j = 0; j< mSize; j++)
|
201
|
for(int k = 0; k< mSize; k++)
|
202
|
{
|
203
|
if( i==0 || i==mSize-1 || j==0 || j==mSize-1 || k==0 || k==mSize-1 )
|
204
|
{
|
205
|
q = mQuatScramble[i][j][k];
|
206
|
|
207
|
if( q.get1()!=x || q.get2()!=y || q.get3()!=z || q.get4()!=w )
|
208
|
{
|
209
|
return false;
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
|
214
|
return true;
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
public void apply(Effect effect, int position)
|
220
|
{
|
221
|
for(int x=0; x<mSize; x++)
|
222
|
for(int y=0; y<mSize; y++)
|
223
|
for(int z=0; z<mSize; z++)
|
224
|
{
|
225
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
226
|
{
|
227
|
mEffects[x][y][z].apply(effect, position);
|
228
|
}
|
229
|
}
|
230
|
}
|
231
|
|
232
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
233
|
|
234
|
public void remove(long effectID)
|
235
|
{
|
236
|
for(int x=0; x<mSize; x++)
|
237
|
for(int y=0; y<mSize; y++)
|
238
|
for(int z=0; z<mSize; z++)
|
239
|
{
|
240
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
241
|
{
|
242
|
mEffects[x][y][z].abortById(effectID);
|
243
|
}
|
244
|
}
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
public void solve()
|
250
|
{
|
251
|
for(int x=0; x<mSize; x++)
|
252
|
for(int y=0; y<mSize; y++)
|
253
|
for(int z=0; z<mSize; z++)
|
254
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
255
|
{
|
256
|
mQuatScramble[x][y][z].set(0,0,0,1);
|
257
|
mCurrentPosition[x][y][z].set(x,y,z);
|
258
|
}
|
259
|
}
|
260
|
|
261
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
262
|
|
263
|
public String print_effects()
|
264
|
{
|
265
|
String str="";
|
266
|
|
267
|
EffectQueue[] effects = mEffects[0][0][0].getQueues();
|
268
|
EffectQueue matrix = effects[0];
|
269
|
EffectQueue vertex = effects[1];
|
270
|
EffectQueue fragment = effects[2];
|
271
|
EffectQueue postprocess = effects[3];
|
272
|
|
273
|
str+="MATRIX: ";
|
274
|
int m_len = matrix.getNumEffects();
|
275
|
for(int i=0; i<m_len; i++)
|
276
|
{
|
277
|
str+=(" "+matrix.getEffect(i).getName()+"("+matrix.getEffect(i).getID()+")" );
|
278
|
}
|
279
|
str+='\n';
|
280
|
|
281
|
str+="VERTEX: ";
|
282
|
int v_len = vertex.getNumEffects();
|
283
|
for(int i=0; i<v_len; i++)
|
284
|
{
|
285
|
str+=(" "+vertex.getEffect(i).getName()+"("+matrix.getEffect(i).getID()+")" );
|
286
|
}
|
287
|
str+='\n';
|
288
|
|
289
|
str+="FRAGMENT: ";
|
290
|
int f_len = fragment.getNumEffects();
|
291
|
for(int i=0; i<f_len; i++)
|
292
|
{
|
293
|
str+=(" "+fragment.getEffect(i).getName()+"("+matrix.getEffect(i).getID()+")" );
|
294
|
}
|
295
|
str+='\n';
|
296
|
|
297
|
str+="POSTPROCESS: ";
|
298
|
int p_len = postprocess.getNumEffects();
|
299
|
for(int i=0; i<p_len; i++)
|
300
|
{
|
301
|
str+=(" "+postprocess.getEffect(i).getName()+"("+matrix.getEffect(i).getID()+")" );
|
302
|
}
|
303
|
str+='\n';
|
304
|
|
305
|
return str;
|
306
|
}
|
307
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308
|
|
309
|
public int getNumEffects(EffectType type)
|
310
|
{
|
311
|
return mEffects[0][0][0].getNumEffects(type);
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener )
|
317
|
{
|
318
|
Static3D axis = VectX;
|
319
|
long effectID=0;
|
320
|
boolean first = true;
|
321
|
|
322
|
switch(vector)
|
323
|
{
|
324
|
case VECTX: axis = VectX; break;
|
325
|
case VECTY: axis = VectY; break;
|
326
|
case VECTZ: axis = VectZ; break;
|
327
|
}
|
328
|
|
329
|
mRotAxis = vector;
|
330
|
mRotRow = row;
|
331
|
|
332
|
mRotationAngleStatic.set1(0.0f);
|
333
|
|
334
|
for(int x=0; x<mSize; x++)
|
335
|
for(int y=0; y<mSize; y++)
|
336
|
for(int z=0; z<mSize; z++)
|
337
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
338
|
{
|
339
|
if( belongsToRotation(x,y,z,vector,mRotRow) )
|
340
|
{
|
341
|
mRotationAxis[x][y][z].set(axis);
|
342
|
mRotationAngle[x][y][z].setDuration(durationMillis);
|
343
|
mRotationAngle[x][y][z].resetToBeginning();
|
344
|
mRotationAngle[x][y][z].add(new Static1D(0));
|
345
|
mRotationAngle[x][y][z].add(new Static1D(angle));
|
346
|
|
347
|
if( first )
|
348
|
{
|
349
|
first = false;
|
350
|
effectID = mRotateEffect[x][y][z].getID();
|
351
|
mRotateEffect[x][y][z].notifyWhenFinished(listener);
|
352
|
}
|
353
|
}
|
354
|
}
|
355
|
|
356
|
return effectID;
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
|
361
|
public int getSize()
|
362
|
{
|
363
|
return mSize;
|
364
|
}
|
365
|
|
366
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367
|
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
|
368
|
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
|
369
|
// memory leak.
|
370
|
|
371
|
void releaseResources()
|
372
|
{
|
373
|
mTexture.markForDeletion();
|
374
|
|
375
|
for(int x=0; x<mSize; x++)
|
376
|
for(int y=0; y<mSize; y++)
|
377
|
for(int z=0; z<mSize; z++)
|
378
|
{
|
379
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
380
|
{
|
381
|
mCubes[x][y][z].markForDeletion();
|
382
|
mNodes[x][y][z].markForDeletion();
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
|
387
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
388
|
|
389
|
void addNewRotation(int vector, int row )
|
390
|
{
|
391
|
Static3D axis = VectX;
|
392
|
|
393
|
switch(vector)
|
394
|
{
|
395
|
case VECTX: axis = VectX; break;
|
396
|
case VECTY: axis = VectY; break;
|
397
|
case VECTZ: axis = VectZ; break;
|
398
|
}
|
399
|
|
400
|
mRotAxis = vector;
|
401
|
mRotRow = row;
|
402
|
|
403
|
mRotationAngleStatic.set1(0.0f);
|
404
|
|
405
|
for(int x=0; x<mSize; x++)
|
406
|
for(int y=0; y<mSize; y++)
|
407
|
for(int z=0; z<mSize; z++)
|
408
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
409
|
{
|
410
|
if( belongsToRotation(x,y,z,vector,mRotRow) )
|
411
|
{
|
412
|
mRotationAxis[x][y][z].set(axis);
|
413
|
mRotationAngle[x][y][z].add(mRotationAngleStatic);
|
414
|
}
|
415
|
}
|
416
|
}
|
417
|
|
418
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
419
|
|
420
|
void continueRotation(float angleInDegrees)
|
421
|
{
|
422
|
mRotationAngleStatic.set1(angleInDegrees);
|
423
|
}
|
424
|
|
425
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
426
|
|
427
|
private int computeNearestAngle(float angle)
|
428
|
{
|
429
|
final int NEAREST = 90;
|
430
|
|
431
|
int tmp = (int)((angle+NEAREST/2)/NEAREST);
|
432
|
if( angle< -(NEAREST/2) ) tmp-=1;
|
433
|
|
434
|
return NEAREST*tmp;
|
435
|
}
|
436
|
|
437
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
438
|
|
439
|
long finishRotationNow(EffectListener listener)
|
440
|
{
|
441
|
boolean first = true;
|
442
|
long effectID=0;
|
443
|
|
444
|
for(int x=0; x<mSize; x++)
|
445
|
for(int y=0; y<mSize; y++)
|
446
|
for(int z=0; z<mSize; z++)
|
447
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
448
|
{
|
449
|
if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
|
450
|
{
|
451
|
if( first )
|
452
|
{
|
453
|
first = false;
|
454
|
mRotateEffect[x][y][z].notifyWhenFinished(listener);
|
455
|
effectID = mRotateEffect[x][y][z].getID();
|
456
|
int pointNum = mRotationAngle[x][y][z].getNumPoints();
|
457
|
|
458
|
if( pointNum>=1 )
|
459
|
{
|
460
|
float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get1();
|
461
|
int nearestAngleInDegrees = computeNearestAngle(startingAngle);
|
462
|
mRotationAngleStatic.set1(startingAngle);
|
463
|
mRotationAngleFinal.set1(nearestAngleInDegrees);
|
464
|
mRotationAngleMiddle.set1( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
|
465
|
}
|
466
|
else
|
467
|
{
|
468
|
android.util.Log.e("cube", "ERROR finishing rotation!");
|
469
|
return 0;
|
470
|
}
|
471
|
}
|
472
|
|
473
|
mRotationAngle[x][y][z].setDuration(POST_ROTATION_MILLISEC);
|
474
|
mRotationAngle[x][y][z].resetToBeginning();
|
475
|
mRotationAngle[x][y][z].removeAll();
|
476
|
mRotationAngle[x][y][z].add(mRotationAngleStatic);
|
477
|
mRotationAngle[x][y][z].add(mRotationAngleMiddle);
|
478
|
mRotationAngle[x][y][z].add(mRotationAngleFinal);
|
479
|
}
|
480
|
}
|
481
|
|
482
|
return effectID;
|
483
|
}
|
484
|
|
485
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
486
|
|
487
|
public void removeRotationNow()
|
488
|
{
|
489
|
float qx=0,qy=0,qz=0;
|
490
|
boolean first = true;
|
491
|
Static4D quat = null;
|
492
|
|
493
|
switch(mRotAxis)
|
494
|
{
|
495
|
case VECTX: qx=1; break;
|
496
|
case VECTY: qy=1; break;
|
497
|
case VECTZ: qz=1; break;
|
498
|
}
|
499
|
|
500
|
for(int x=0; x<mSize; x++)
|
501
|
for(int y=0; y<mSize; y++)
|
502
|
for(int z=0; z<mSize; z++)
|
503
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
504
|
{
|
505
|
if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
|
506
|
{
|
507
|
if( first )
|
508
|
{
|
509
|
first = false;
|
510
|
int pointNum = mRotationAngle[x][y][z].getNumPoints();
|
511
|
|
512
|
if( pointNum>=1 )
|
513
|
{
|
514
|
float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get1();
|
515
|
int nearestAngleInDegrees = computeNearestAngle(startingAngle);
|
516
|
double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
|
517
|
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
|
518
|
float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
|
519
|
quat = new Static4D(qx*sinA, qy*sinA, qz*sinA, cosA);
|
520
|
}
|
521
|
else
|
522
|
{
|
523
|
android.util.Log.e("cube", "ERROR removing rotation!");
|
524
|
return;
|
525
|
}
|
526
|
}
|
527
|
|
528
|
mRotationAngle[x][y][z].removeAll();
|
529
|
mQuatScramble[x][y][z].set(RubikSurfaceView.quatMultiply(quat,mQuatScramble[x][y][z]));
|
530
|
normalizeScrambleQuat(x,y,z);
|
531
|
modifyCurrentPosition(x,y,z,quat);
|
532
|
}
|
533
|
}
|
534
|
|
535
|
mRotationAngleStatic.set1(0);
|
536
|
}
|
537
|
|
538
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
539
|
// All legal rotation quats must have all four of their components equal to either
|
540
|
// 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
|
541
|
//
|
542
|
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
|
543
|
// correct the value of the 'scramble' quat to what it should be.
|
544
|
//
|
545
|
// We also have to remember that the group of unit quaternions is a double-cover of rotations
|
546
|
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
|
547
|
|
548
|
private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
|
549
|
private static final float[] LEGAL = { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
|
550
|
|
551
|
private void normalizeScrambleQuat(int i, int j, int k)
|
552
|
{
|
553
|
Static4D quat = mQuatScramble[i][j][k];
|
554
|
|
555
|
float x = quat.get1();
|
556
|
float y = quat.get2();
|
557
|
float z = quat.get3();
|
558
|
float w = quat.get4();
|
559
|
float diff;
|
560
|
|
561
|
for(int legal=0; legal<LEGAL.length; legal++)
|
562
|
{
|
563
|
diff = x-LEGAL[legal];
|
564
|
if( diff*diff<0.01f ) x = LEGAL[legal];
|
565
|
diff = y-LEGAL[legal];
|
566
|
if( diff*diff<0.01f ) y = LEGAL[legal];
|
567
|
diff = z-LEGAL[legal];
|
568
|
if( diff*diff<0.01f ) z = LEGAL[legal];
|
569
|
diff = w-LEGAL[legal];
|
570
|
if( diff*diff<0.01f ) w = LEGAL[legal];
|
571
|
}
|
572
|
|
573
|
if( w<0 )
|
574
|
{
|
575
|
w = -w;
|
576
|
z = -z;
|
577
|
y = -y;
|
578
|
x = -x;
|
579
|
}
|
580
|
else if( w==0 )
|
581
|
{
|
582
|
if( z<0 )
|
583
|
{
|
584
|
z = -z;
|
585
|
y = -y;
|
586
|
x = -x;
|
587
|
}
|
588
|
else if( z==0 )
|
589
|
{
|
590
|
if( y<0 )
|
591
|
{
|
592
|
y = -y;
|
593
|
x = -x;
|
594
|
}
|
595
|
else if( y==0 )
|
596
|
{
|
597
|
if( x<0 )
|
598
|
{
|
599
|
x = -x;
|
600
|
}
|
601
|
}
|
602
|
}
|
603
|
}
|
604
|
|
605
|
mQuatScramble[i][j][k].set(x,y,z,w);
|
606
|
}
|
607
|
|
608
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
609
|
|
610
|
private float getSinkStrength()
|
611
|
{
|
612
|
switch(mSize)
|
613
|
{
|
614
|
case 1 : return 1.1f;
|
615
|
case 2 : return 1.5f;
|
616
|
case 3 : return 1.8f;
|
617
|
case 4 : return 2.0f;
|
618
|
default: return 3.0f - 4.0f/mSize;
|
619
|
}
|
620
|
}
|
621
|
|
622
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
623
|
|
624
|
private boolean belongsToRotation(int x, int y, int z, int vector, int row)
|
625
|
{
|
626
|
switch(vector)
|
627
|
{
|
628
|
case VECTX: return mCurrentPosition[x][y][z].get1()==row;
|
629
|
case VECTY: return mCurrentPosition[x][y][z].get2()==row;
|
630
|
case VECTZ: return mCurrentPosition[x][y][z].get3()==row;
|
631
|
}
|
632
|
|
633
|
return false;
|
634
|
}
|
635
|
|
636
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
637
|
|
638
|
private void modifyCurrentPosition(int x, int y, int z, Static4D quat)
|
639
|
{
|
640
|
Static3D current = mCurrentPosition[x][y][z];
|
641
|
float diff = 0.5f*(mSize-1);
|
642
|
float cubitCenterX = current.get1() - diff;
|
643
|
float cubitCenterY = current.get2() - diff;
|
644
|
float cubitCenterZ = current.get3() - diff;
|
645
|
|
646
|
Static4D cubitCenter = new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
|
647
|
Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
|
648
|
|
649
|
float rotatedX = rotatedCenter.get1() + diff;
|
650
|
float rotatedY = rotatedCenter.get2() + diff;
|
651
|
float rotatedZ = rotatedCenter.get3() + diff;
|
652
|
|
653
|
int roundedX = (int)(rotatedX+0.1f);
|
654
|
int roundedY = (int)(rotatedY+0.1f);
|
655
|
int roundedZ = (int)(rotatedZ+0.1f);
|
656
|
|
657
|
mCurrentPosition[x][y][z].set1(roundedX);
|
658
|
mCurrentPosition[x][y][z].set2(roundedY);
|
659
|
mCurrentPosition[x][y][z].set3(roundedZ);
|
660
|
}
|
661
|
|
662
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
663
|
|
664
|
void createTexture()
|
665
|
{
|
666
|
Bitmap bitmap;
|
667
|
|
668
|
final int S = 128;
|
669
|
final int W = 3*S;
|
670
|
final int H = 2*S;
|
671
|
final int R = S/10;
|
672
|
final int M = S/20;
|
673
|
|
674
|
Paint paint = new Paint();
|
675
|
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
|
676
|
Canvas canvas = new Canvas(bitmap);
|
677
|
|
678
|
paint.setAntiAlias(true);
|
679
|
paint.setTextAlign(Paint.Align.CENTER);
|
680
|
paint.setStyle(Paint.Style.FILL);
|
681
|
|
682
|
// 3x2 bitmap = 6 squares:
|
683
|
//
|
684
|
// RED GREEN BLUE
|
685
|
// YELLOW WHITE BROWN
|
686
|
|
687
|
paint.setColor(0xff000000); // BLACK BACKGROUND
|
688
|
canvas.drawRect(0, 0, W, H, paint); //
|
689
|
|
690
|
paint.setColor(0xffff0000); // RED
|
691
|
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); //
|
692
|
paint.setColor(0xff00ff00); // GREEN
|
693
|
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); //
|
694
|
paint.setColor(0xff0000ff); // BLUE
|
695
|
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); //
|
696
|
paint.setColor(0xffffff00); // YELLOW
|
697
|
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); //
|
698
|
paint.setColor(0xffffffff); // WHITE
|
699
|
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
|
700
|
paint.setColor(0xffb5651d); // BROWN
|
701
|
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
|
702
|
|
703
|
mTexture.setTexture(bitmap);
|
704
|
}
|
705
|
|
706
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
707
|
|
708
|
void recomputeScaleFactor(int screenWidth, int screenHeight, float size)
|
709
|
{
|
710
|
int texW = mNodeTexture.getWidth();
|
711
|
int texH = mNodeTexture.getHeight();
|
712
|
|
713
|
if( (float)texH/texW > (float)screenHeight/screenWidth )
|
714
|
{
|
715
|
int w = (screenHeight*texW)/texH;
|
716
|
float factor = (float)screenHeight/texH;
|
717
|
mNodeMove.set((screenWidth-w)*0.5f ,0, 0);
|
718
|
mNodeScale.set(factor,factor,factor);
|
719
|
}
|
720
|
else
|
721
|
{
|
722
|
int h = (screenWidth*texH)/texW;
|
723
|
float factor = (float)screenWidth/texW;
|
724
|
mNodeMove.set(0,(screenHeight-h)*0.5f,0);
|
725
|
mNodeScale.set(factor,factor,factor);
|
726
|
}
|
727
|
|
728
|
float scaleFactor = (size/(TEXTURE_SIZE*mSize)) * (float)texW/(screenWidth>screenHeight ? screenHeight:screenWidth);
|
729
|
|
730
|
mMove.set( texW*0.5f , texH*0.5f , 0.0f );
|
731
|
mScale.set(scaleFactor,scaleFactor,scaleFactor);
|
732
|
}
|
733
|
}
|