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