1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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.objects;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
import android.graphics.Bitmap;
|
24
|
import android.graphics.Canvas;
|
25
|
import android.graphics.Paint;
|
26
|
|
27
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
28
|
|
29
|
import org.distorted.library.effect.Effect;
|
30
|
import org.distorted.library.effect.MatrixEffectMove;
|
31
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
32
|
import org.distorted.library.effect.MatrixEffectScale;
|
33
|
import org.distorted.library.effect.VertexEffectQuaternion;
|
34
|
import org.distorted.library.effect.VertexEffectRotate;
|
35
|
import org.distorted.library.main.DistortedEffects;
|
36
|
import org.distorted.library.main.DistortedNode;
|
37
|
import org.distorted.library.main.DistortedTexture;
|
38
|
import org.distorted.library.mesh.MeshBase;
|
39
|
import org.distorted.library.mesh.MeshJoined;
|
40
|
import org.distorted.library.mesh.MeshRectangles;
|
41
|
import org.distorted.library.message.EffectListener;
|
42
|
import org.distorted.library.type.Dynamic1D;
|
43
|
import org.distorted.library.type.Static1D;
|
44
|
import org.distorted.library.type.Static3D;
|
45
|
import org.distorted.library.type.Static4D;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public abstract class RubikObject extends DistortedNode
|
50
|
{
|
51
|
private static final Static3D CENTER = new Static3D(0,0,0);
|
52
|
static final int INTERIOR_COLOR = 0xff000000;
|
53
|
public static final int NODE_FBO_SIZE = 600;
|
54
|
private static final int POST_ROTATION_MILLISEC = 500;
|
55
|
private static final int TEXTURE_HEIGHT = 128;
|
56
|
|
57
|
final Static3D[] ROTATION_AXIS;
|
58
|
final Static4D[] QUATS;
|
59
|
final int NUM_FACES;
|
60
|
|
61
|
static float OBJECT_SCREEN_RATIO;
|
62
|
|
63
|
private final int NUM_CUBITS;
|
64
|
private int mRotRowBitmap;
|
65
|
private int mRotAxis;
|
66
|
private Static3D[] mOrigPos;
|
67
|
private Static3D mNodeScale;
|
68
|
private Static4D mQuatAccumulated;
|
69
|
private Cubit[] mCubits;
|
70
|
private int mSize;
|
71
|
private RubikObjectList mList;
|
72
|
private MeshBase mMesh;
|
73
|
private DistortedEffects mEffects;
|
74
|
private VertexEffectRotate mRotateEffect;
|
75
|
private Dynamic1D mRotationAngle;
|
76
|
private Static3D mRotationAxis;
|
77
|
|
78
|
float mStart, mStep;
|
79
|
|
80
|
Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
|
81
|
DistortedTexture mTexture;
|
82
|
|
83
|
MatrixEffectScale mScaleEffect;
|
84
|
MatrixEffectQuaternion mQuatCEffect;
|
85
|
MatrixEffectQuaternion mQuatAEffect;
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
RubikObject(int size, int fov, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture,
|
90
|
MeshRectangles nodeMesh, DistortedEffects nodeEffects, int[][] moves, RubikObjectList list)
|
91
|
{
|
92
|
super(nodeTexture,nodeEffects,nodeMesh);
|
93
|
|
94
|
resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
|
95
|
|
96
|
mList = list;
|
97
|
mOrigPos = getCubitPositions(size);
|
98
|
|
99
|
QUATS = getQuats();
|
100
|
NUM_CUBITS = mOrigPos.length;
|
101
|
ROTATION_AXIS = getRotationAxis();
|
102
|
OBJECT_SCREEN_RATIO = getScreenRatio();
|
103
|
NUM_FACES = getNumFaces();
|
104
|
|
105
|
mSize = size;
|
106
|
computeStartAndStep(mOrigPos);
|
107
|
mNodeScale= new Static3D(1,1,1);
|
108
|
mQuatAccumulated = quatAcc;
|
109
|
|
110
|
mRotationAngle= new Dynamic1D();
|
111
|
mRotationAxis = new Static3D(1,0,0);
|
112
|
mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
|
113
|
|
114
|
mRotationAngleStatic = new Static1D(0);
|
115
|
mRotationAngleMiddle = new Static1D(0);
|
116
|
mRotationAngleFinal = new Static1D(0);
|
117
|
|
118
|
float scale = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
|
119
|
mScaleEffect = new MatrixEffectScale(new Static3D(scale,scale,scale));
|
120
|
mQuatCEffect = new MatrixEffectQuaternion(quatCur, CENTER);
|
121
|
mQuatAEffect = new MatrixEffectQuaternion(quatAcc, CENTER);
|
122
|
|
123
|
MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
|
124
|
nodeEffects.apply(nodeScaleEffect);
|
125
|
|
126
|
mCubits = new Cubit[NUM_CUBITS];
|
127
|
mTexture = new DistortedTexture();
|
128
|
MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
|
129
|
|
130
|
for(int i=0; i<NUM_CUBITS; i++)
|
131
|
{
|
132
|
mCubits[i] = new Cubit(this,mOrigPos[i]);
|
133
|
cubitMesh[i] = createCubitMesh(i);
|
134
|
cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
|
135
|
cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
|
136
|
}
|
137
|
|
138
|
mMesh = new MeshJoined(cubitMesh);
|
139
|
|
140
|
resetAllTextureMaps();
|
141
|
|
142
|
mEffects = new DistortedEffects();
|
143
|
|
144
|
int num_quats = QUATS.length;
|
145
|
for(int q=0; q<num_quats; q++)
|
146
|
{
|
147
|
VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
|
148
|
vq.setMeshAssociation(0,q);
|
149
|
mEffects.apply(vq);
|
150
|
}
|
151
|
|
152
|
mEffects.apply(mRotateEffect);
|
153
|
mEffects.apply(mQuatAEffect);
|
154
|
mEffects.apply(mQuatCEffect);
|
155
|
mEffects.apply(mScaleEffect);
|
156
|
|
157
|
attach( new DistortedNode(mTexture,mEffects,mMesh) );
|
158
|
|
159
|
setupPosition(moves);
|
160
|
|
161
|
setProjection(fov, 0.1f);
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
|
166
|
private void textureCubitMesh(MeshBase mesh, int cubit)
|
167
|
{
|
168
|
boolean belongs;
|
169
|
final Static4D[] maps = new Static4D[NUM_FACES];
|
170
|
final float ratio = 1.0f/(NUM_FACES+1);
|
171
|
|
172
|
if( 2*ROTATION_AXIS.length == NUM_FACES ) // i.e. there are faces on both ends of the axis (cube)
|
173
|
{
|
174
|
for(int i=0; i<NUM_FACES; i++)
|
175
|
{
|
176
|
belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
|
177
|
maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
|
178
|
}
|
179
|
}
|
180
|
else if( ROTATION_AXIS.length == NUM_FACES ) // just a single face on the right end of an axis (pyraminx)
|
181
|
{
|
182
|
for(int i=0; i<NUM_FACES; i++)
|
183
|
{
|
184
|
belongs = isOnFace(cubit, i, 0 );
|
185
|
maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
mesh.setTextureMap(maps,NUM_FACES*cubit);
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
|
194
|
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
|
195
|
// consecutive).
|
196
|
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
|
197
|
// the Cube and the Pyraminx.
|
198
|
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
|
199
|
// given Cubit belongs to.
|
200
|
|
201
|
private void computeStartAndStep(Static3D[] pos)
|
202
|
{
|
203
|
float min = Float.MAX_VALUE;
|
204
|
float max = Float.MIN_VALUE;
|
205
|
float axisX = ROTATION_AXIS[0].get0();
|
206
|
float axisY = ROTATION_AXIS[0].get1();
|
207
|
float axisZ = ROTATION_AXIS[0].get2();
|
208
|
float tmp;
|
209
|
|
210
|
for(int i=0; i<NUM_CUBITS; i++)
|
211
|
{
|
212
|
tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
|
213
|
if( tmp<min ) min=tmp;
|
214
|
if( tmp>max ) max=tmp;
|
215
|
}
|
216
|
|
217
|
mStart = min;
|
218
|
mStep = (max-min+1.0f)/mSize;
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
|
224
|
{
|
225
|
int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
|
226
|
return ((1<<cubitRow)&rowBitmap)!=0;
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
|
231
|
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
|
232
|
// away from the face of the Pyraminx shouldn't be textured.
|
233
|
|
234
|
private boolean isOnFace( int cubit, int axis, int row)
|
235
|
{
|
236
|
final float MAX_ERROR = 0.0001f;
|
237
|
float diff = mCubits[cubit].mRotationRow[axis] - row;
|
238
|
return diff*diff < MAX_ERROR;
|
239
|
}
|
240
|
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
// note the minus in front of the sin() - we rotate counterclockwise
|
243
|
// when looking towards the direction where the axis increases in values.
|
244
|
|
245
|
private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
|
246
|
{
|
247
|
Static3D axis = ROTATION_AXIS[axisIndex];
|
248
|
|
249
|
while( angleInDegrees<0 ) angleInDegrees += 360;
|
250
|
angleInDegrees %= 360;
|
251
|
|
252
|
float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
|
253
|
float sinA =-(float)Math.sqrt(1-cosA*cosA);
|
254
|
|
255
|
return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
private void setupPosition(int[][] moves)
|
261
|
{
|
262
|
if( moves!=null )
|
263
|
{
|
264
|
Static4D quat;
|
265
|
int index, axis, rowBitmap, angle;
|
266
|
int corr = (360/getBasicAngle());
|
267
|
|
268
|
for(int[] move: moves)
|
269
|
{
|
270
|
axis = move[0];
|
271
|
rowBitmap= move[1];
|
272
|
angle = move[2]*corr;
|
273
|
quat = makeQuaternion(axis,angle);
|
274
|
|
275
|
for(int j=0; j<NUM_CUBITS; j++)
|
276
|
if( belongsToRotation(j,axis,rowBitmap) )
|
277
|
{
|
278
|
index = mCubits[j].removeRotationNow(quat);
|
279
|
mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
|
280
|
}
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
int getCubitFaceColorIndex(int cubit, int face)
|
288
|
{
|
289
|
Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
|
290
|
return (int)(texMap.get0() / texMap.get2());
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
|
295
|
|
296
|
void clampPos(Static3D pos)
|
297
|
{
|
298
|
float currError, minError = Float.MAX_VALUE;
|
299
|
int minErrorIndex= -1;
|
300
|
float x = pos.get0();
|
301
|
float y = pos.get1();
|
302
|
float z = pos.get2();
|
303
|
float xo,yo,zo;
|
304
|
|
305
|
for(int i=0; i<NUM_CUBITS; i++)
|
306
|
{
|
307
|
xo = mOrigPos[i].get0();
|
308
|
yo = mOrigPos[i].get1();
|
309
|
zo = mOrigPos[i].get2();
|
310
|
|
311
|
currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
|
312
|
|
313
|
if( currError<minError )
|
314
|
{
|
315
|
minError = currError;
|
316
|
minErrorIndex = i;
|
317
|
}
|
318
|
}
|
319
|
|
320
|
pos.set( mOrigPos[minErrorIndex] );
|
321
|
}
|
322
|
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
// the getFaceColors + final black in a horizontal strip.
|
325
|
|
326
|
public void createTexture()
|
327
|
{
|
328
|
Bitmap bitmap;
|
329
|
|
330
|
Paint paint = new Paint();
|
331
|
bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
|
332
|
Canvas canvas = new Canvas(bitmap);
|
333
|
|
334
|
paint.setAntiAlias(true);
|
335
|
paint.setTextAlign(Paint.Align.CENTER);
|
336
|
paint.setStyle(Paint.Style.FILL);
|
337
|
|
338
|
paint.setColor(INTERIOR_COLOR);
|
339
|
canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
|
340
|
|
341
|
for(int i=0; i<NUM_FACES; i++)
|
342
|
{
|
343
|
createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
|
344
|
}
|
345
|
|
346
|
mTexture.setTexture(bitmap);
|
347
|
}
|
348
|
|
349
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350
|
|
351
|
public int getSize()
|
352
|
{
|
353
|
return mSize;
|
354
|
}
|
355
|
|
356
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
357
|
|
358
|
public void continueRotation(float angleInDegrees)
|
359
|
{
|
360
|
mRotationAngleStatic.set0(angleInDegrees);
|
361
|
}
|
362
|
|
363
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
364
|
|
365
|
public Static4D getRotationQuat()
|
366
|
{
|
367
|
return mQuatAccumulated;
|
368
|
}
|
369
|
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
|
372
|
public void recomputeScaleFactor(int scrWidth, int scrHeight)
|
373
|
{
|
374
|
float factor = Math.min(scrWidth,scrHeight);
|
375
|
mNodeScale.set(factor,factor,factor);
|
376
|
}
|
377
|
|
378
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
379
|
|
380
|
public void savePreferences(SharedPreferences.Editor editor)
|
381
|
{
|
382
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
|
383
|
}
|
384
|
|
385
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
386
|
|
387
|
public void restorePreferences(SharedPreferences preferences)
|
388
|
{
|
389
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
|
390
|
}
|
391
|
|
392
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
393
|
|
394
|
public void releaseResources()
|
395
|
{
|
396
|
mTexture.markForDeletion();
|
397
|
}
|
398
|
|
399
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
400
|
|
401
|
public void apply(Effect effect, int position)
|
402
|
{
|
403
|
mEffects.apply(effect, position);
|
404
|
}
|
405
|
|
406
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
407
|
|
408
|
public void remove(long effectID)
|
409
|
{
|
410
|
mEffects.abortById(effectID);
|
411
|
}
|
412
|
|
413
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
414
|
|
415
|
public void solve()
|
416
|
{
|
417
|
for(int i=0; i<NUM_CUBITS; i++)
|
418
|
{
|
419
|
mCubits[i].solve();
|
420
|
mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
|
421
|
}
|
422
|
}
|
423
|
|
424
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
425
|
|
426
|
public boolean isSolved()
|
427
|
{
|
428
|
Static4D q = mCubits[0].mQuatScramble;
|
429
|
|
430
|
for(int i=1; i<NUM_CUBITS; i++)
|
431
|
{
|
432
|
if( !mCubits[i].thereIsNoVisibleDifference(q) ) return false;
|
433
|
}
|
434
|
|
435
|
return true;
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
|
440
|
public void resetAllTextureMaps()
|
441
|
{
|
442
|
for(int i=0; i<NUM_CUBITS; i++)
|
443
|
{
|
444
|
textureCubitMesh( mMesh , i );
|
445
|
}
|
446
|
}
|
447
|
|
448
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
449
|
|
450
|
public void setTextureMap(int cubit, int face, int newColor)
|
451
|
{
|
452
|
final float ratio = 1.0f/(NUM_FACES+1);
|
453
|
final Static4D[] maps = new Static4D[NUM_FACES];
|
454
|
|
455
|
try
|
456
|
{
|
457
|
maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
|
458
|
mMesh.setTextureMap(maps,NUM_FACES*cubit);
|
459
|
}
|
460
|
catch(ArrayIndexOutOfBoundsException ignored)
|
461
|
{
|
462
|
// the object must have changed in between of us touching the screen and getting here
|
463
|
// (which happens after the next render, i.e. about 30 miliseconds later)
|
464
|
// ignore.
|
465
|
}
|
466
|
}
|
467
|
|
468
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
469
|
|
470
|
public void beginNewRotation(int axis, int row )
|
471
|
{
|
472
|
if( axis<0 || axis>=ROTATION_AXIS.length )
|
473
|
{
|
474
|
android.util.Log.e("object", "invalid rotation axis: "+axis);
|
475
|
return;
|
476
|
}
|
477
|
if( row<0 || row>=mSize )
|
478
|
{
|
479
|
android.util.Log.e("object", "invalid rotation row: "+row);
|
480
|
return;
|
481
|
}
|
482
|
|
483
|
mRotAxis = axis;
|
484
|
mRotRowBitmap= (1<<row);
|
485
|
mRotationAngleStatic.set0(0.0f);
|
486
|
mRotationAxis.set( ROTATION_AXIS[axis] );
|
487
|
mRotationAngle.add(mRotationAngleStatic);
|
488
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
|
489
|
}
|
490
|
|
491
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
492
|
|
493
|
public long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
|
494
|
{
|
495
|
mRotAxis = axis;
|
496
|
mRotRowBitmap= rowBitmap;
|
497
|
|
498
|
mRotationAngleStatic.set0(0.0f);
|
499
|
mRotationAxis.set( ROTATION_AXIS[axis] );
|
500
|
mRotationAngle.setDuration(durationMillis);
|
501
|
mRotationAngle.resetToBeginning();
|
502
|
mRotationAngle.add(new Static1D(0));
|
503
|
mRotationAngle.add(new Static1D(angle));
|
504
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
|
505
|
mRotateEffect.notifyWhenFinished(listener);
|
506
|
|
507
|
return mRotateEffect.getID();
|
508
|
}
|
509
|
|
510
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
511
|
|
512
|
public long finishRotationNow(EffectListener listener)
|
513
|
{
|
514
|
float angle = getAngle();
|
515
|
int nearestAngleInDegrees = computeNearestAngle(angle);
|
516
|
mRotationAngleStatic.set0(angle);
|
517
|
mRotationAngleFinal.set0(nearestAngleInDegrees);
|
518
|
mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
|
519
|
|
520
|
mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
|
521
|
mRotationAngle.resetToBeginning();
|
522
|
mRotationAngle.removeAll();
|
523
|
mRotationAngle.add(mRotationAngleStatic);
|
524
|
mRotationAngle.add(mRotationAngleMiddle);
|
525
|
mRotationAngle.add(mRotationAngleFinal);
|
526
|
mRotateEffect.notifyWhenFinished(listener);
|
527
|
|
528
|
return mRotateEffect.getID();
|
529
|
}
|
530
|
|
531
|
|
532
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
533
|
|
534
|
private float getAngle()
|
535
|
{
|
536
|
int pointNum = mRotationAngle.getNumPoints();
|
537
|
|
538
|
if( pointNum>=1 )
|
539
|
{
|
540
|
return mRotationAngle.getPoint(pointNum-1).get0();
|
541
|
}
|
542
|
else
|
543
|
{
|
544
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
545
|
crashlytics.log("points in RotationAngle: "+pointNum);
|
546
|
return 0;
|
547
|
}
|
548
|
}
|
549
|
|
550
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
551
|
|
552
|
public void removeRotationNow()
|
553
|
{
|
554
|
float angle = getAngle();
|
555
|
int nearestAngleInDegrees = computeNearestAngle(angle);
|
556
|
double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
|
557
|
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
|
558
|
float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
|
559
|
float axisX = ROTATION_AXIS[mRotAxis].get0();
|
560
|
float axisY = ROTATION_AXIS[mRotAxis].get1();
|
561
|
float axisZ = ROTATION_AXIS[mRotAxis].get2();
|
562
|
Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
563
|
|
564
|
mRotationAngle.removeAll();
|
565
|
mRotationAngleStatic.set0(0);
|
566
|
|
567
|
for(int i=0; i<NUM_CUBITS; i++)
|
568
|
if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
|
569
|
{
|
570
|
int index = mCubits[i].removeRotationNow(quat);
|
571
|
mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
|
572
|
}
|
573
|
}
|
574
|
|
575
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
576
|
|
577
|
public void initializeObject(int[][] moves)
|
578
|
{
|
579
|
solve();
|
580
|
setupPosition(moves);
|
581
|
}
|
582
|
|
583
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
584
|
|
585
|
public int getCubit(float[] point3D)
|
586
|
{
|
587
|
float dist, minDist = Float. MAX_VALUE;
|
588
|
int currentBest=-1;
|
589
|
float multiplier = returnMultiplier();
|
590
|
|
591
|
point3D[0] *= multiplier;
|
592
|
point3D[1] *= multiplier;
|
593
|
point3D[2] *= multiplier;
|
594
|
|
595
|
for(int i=0; i<NUM_CUBITS; i++)
|
596
|
{
|
597
|
dist = mCubits[i].getDistSquared(point3D);
|
598
|
if( dist<minDist )
|
599
|
{
|
600
|
minDist = dist;
|
601
|
currentBest = i;
|
602
|
}
|
603
|
}
|
604
|
|
605
|
return currentBest;
|
606
|
}
|
607
|
|
608
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
609
|
|
610
|
public int computeNearestAngle(float angle)
|
611
|
{
|
612
|
final int NEAREST = 360/getBasicAngle();
|
613
|
|
614
|
int tmp = (int)((angle+NEAREST/2)/NEAREST);
|
615
|
if( angle< -(NEAREST*0.5) ) tmp-=1;
|
616
|
|
617
|
return NEAREST*tmp;
|
618
|
}
|
619
|
|
620
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
621
|
|
622
|
public RubikObjectList getObjectList()
|
623
|
{
|
624
|
return mList;
|
625
|
}
|
626
|
|
627
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
628
|
|
629
|
abstract float getScreenRatio();
|
630
|
abstract Static3D[] getCubitPositions(int size);
|
631
|
abstract Static4D[] getQuats();
|
632
|
abstract int getNumFaces();
|
633
|
abstract MeshBase createCubitMesh(int cubit);
|
634
|
abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
|
635
|
public abstract Static3D[] getRotationAxis();
|
636
|
public abstract int getBasicAngle();
|
637
|
public abstract float returnMultiplier();
|
638
|
public abstract float returnRotationFactor(float offset);
|
639
|
public abstract String retObjectString();
|
640
|
public abstract float[] getRowChances();
|
641
|
}
|