Project

General

Profile

Download (26.3 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 1d6c1eea

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 TwistyObject extends DistortedNode
57
  {
58
  static final int COLOR_YELLOW = 0xffffff00;
59
  static final int COLOR_WHITE  = 0xffffffff;
60
  static final int COLOR_BLUE   = 0xff0000ff;
61
  static final int COLOR_GREEN  = 0xff00ff00;
62
  static final int COLOR_RED    = 0xffff0000;
63
  static final int COLOR_BROWN  = 0xffb5651d;
64
  static final int COLOR_PINK   = 0xffff90ff;
65
  static final int COLOR_VIOLET = 0xff7700bb;
66
  static final int COLOR_BLACK  = 0xff000000;
67

    
68
  private static final float NODE_RATIO = 1.40f;
69
  private static final float MAX_SIZE_CHANGE = 1.35f;
70
  private static final float MIN_SIZE_CHANGE = 0.8f;
71

    
72
  private static boolean mCreateFromDMesh = true;
73

    
74
  private static final Static3D CENTER = new Static3D(0,0,0);
75
  private static final int POST_ROTATION_MILLISEC = 500;
76
  static final int TEXTURE_HEIGHT = 256;
77

    
78
  final Static3D[] ROTATION_AXIS;
79
  final Static4D[] QUATS;
80
  final Cubit[] CUBITS;
81
  final int NUM_FACES;
82
  final int NUM_TEXTURES;
83
  final int NUM_CUBIT_FACES;
84
  final int NUM_AXIS;
85
  final int NUM_CUBITS;
86
  final float BASIC_STEP;
87

    
88
  private static float mInitScreenRatio;
89
  private static float mObjectScreenRatio = 1.0f;
90

    
91
  private final int mNodeSize;
92
  private int mRotRowBitmap;
93
  private int mRotAxis;
94
  private Static3D[] mOrigPos;
95
  private Static3D mNodeScale;
96
  private Static4D mQuat;
97
  private int mSize;
98
  private ObjectList mList;
99
  private MeshBase mMesh;
100
  private DistortedEffects mEffects;
101
  private VertexEffectRotate mRotateEffect;
102
  private Dynamic1D mRotationAngle;
103
  private Static3D mRotationAxis;
104
  private Static3D mObjectScale;
105

    
106
  float mStart, mStep;
107
  float[] mRowChances;
108

    
109
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
110
  DistortedTexture mTexture;
111

    
112
  MatrixEffectScale mScaleEffect;
113
  MatrixEffectQuaternion mQuatEffect;
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  TwistyObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
118
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
119
    {
120
    super(nodeTexture,nodeEffects,nodeMesh);
121

    
122
    mNodeSize = screenWidth;
123

    
124
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
125

    
126
    mList = list;
127
    mOrigPos = getCubitPositions(size);
128

    
129
    QUATS = getQuats();
130
    NUM_CUBITS  = mOrigPos.length;
131
    ROTATION_AXIS = getRotationAxis();
132
    NUM_AXIS = ROTATION_AXIS.length;
133
    mInitScreenRatio = getScreenRatio();
134
    NUM_FACES = getNumFaces();
135
    NUM_CUBIT_FACES = getNumCubitFaces();
136
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
137
    BASIC_STEP = getBasicStep();
138

    
139
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
140
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
141

    
142
    mSize = size;
143
    computeStartAndStep(mOrigPos);
144
    mNodeScale= new Static3D(1,NODE_RATIO,1);
145
    mQuat = quat;
146

    
147
    mRowChances = getRowChances();
148

    
149
    mRotationAngle= new Dynamic1D();
150
    mRotationAxis = new Static3D(1,0,0);
151
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
152

    
153
    mRotationAngleStatic = new Static1D(0);
154
    mRotationAngleMiddle = new Static1D(0);
155
    mRotationAngleFinal  = new Static1D(0);
156

    
157
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
158
    mObjectScale = new Static3D(scale,scale,scale);
159
    mScaleEffect = new MatrixEffectScale(mObjectScale);
160
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
161

    
162
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
163
    nodeEffects.apply(nodeScaleEffect);
164

    
165
    CUBITS = new Cubit[NUM_CUBITS];
166
    createMeshAndCubits(list,res);
167

    
168
    mTexture = new DistortedTexture();
169
    mEffects = new DistortedEffects();
170

    
171
    int num_quats = QUATS.length;
172
    for(int q=0; q<num_quats; q++)
173
      {
174
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
175
      vq.setMeshAssociation(0,q);
176
      mEffects.apply(vq);
177
      }
178

    
179
    mEffects.apply(mRotateEffect);
180
    mEffects.apply(mQuatEffect);
181
    mEffects.apply(mScaleEffect);
182

    
183
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
184
    // order for the effect to be in front of the object, we need to set the center to be behind it.
185
    getMesh().setComponentCenter(0,0,0,-0.1f);
186

    
187
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
188

    
189
    setupPosition(moves);
190

    
191
    setProjection(fov, 0.1f);
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  private void createMeshAndCubits(ObjectList list, Resources res)
197
    {
198
    if( mCreateFromDMesh )
199
      {
200
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mSize);
201
      int resourceID= list.getResourceIDs()[sizeIndex];
202

    
203
      InputStream is = res.openRawResource(resourceID);
204
      DataInputStream dos = new DataInputStream(is);
205
      mMesh = new MeshFile(dos);
206

    
207
      try
208
        {
209
        is.close();
210
        }
211
      catch(IOException e)
212
        {
213
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
214
        }
215

    
216
      for(int i=0; i<NUM_CUBITS; i++)
217
        {
218
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
219
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
220
        }
221

    
222
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
223
      }
224
    else
225
      {
226
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
227

    
228
      for(int i=0; i<NUM_CUBITS; i++)
229
        {
230
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
231
        cubitMesh[i] = createCubitMesh(i);
232
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
233
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
234
        }
235

    
236
      mMesh = new MeshJoined(cubitMesh);
237
      resetAllTextureMaps();
238
      }
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void setObjectRatio(float sizeChange)
244
    {
245
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
246

    
247
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
248
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
249

    
250
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
251
    mObjectScale.set(scale,scale,scale);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  static float getObjectRatio()
257
    {
258
    return mObjectScreenRatio*mInitScreenRatio;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
263
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
264
// consecutive).
265
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
266
// the Cube and the Pyraminx.
267
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
268
// given Cubit belongs to.
269

    
270
  private void computeStartAndStep(Static3D[] pos)
271
    {
272
    float min = Float.MAX_VALUE;
273
    float max = Float.MIN_VALUE;
274
    float axisX = ROTATION_AXIS[0].get0();
275
    float axisY = ROTATION_AXIS[0].get1();
276
    float axisZ = ROTATION_AXIS[0].get2();
277
    float tmp;
278

    
279
    for(int i=0; i<NUM_CUBITS; i++)
280
      {
281
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
282
      if( tmp<min ) min=tmp;
283
      if( tmp>max ) max=tmp;
284
      }
285

    
286
    mStart = min;
287
    mStep  = (max-min+BASIC_STEP)/mSize;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
293
    {
294
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
295
    return ((1<<cubitRow)&rowBitmap)!=0;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// note the minus in front of the sin() - we rotate counterclockwise
300
// when looking towards the direction where the axis increases in values.
301

    
302
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
303
    {
304
    Static3D axis = ROTATION_AXIS[axisIndex];
305

    
306
    while( angleInDegrees<0 ) angleInDegrees += 360;
307
    angleInDegrees %= 360;
308
    
309
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
310
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
311

    
312
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private synchronized void setupPosition(int[][] moves)
318
    {
319
    if( moves!=null )
320
      {
321
      Static4D quat;
322
      int index, axis, rowBitmap, angle;
323
      int corr = (360/getBasicAngle());
324

    
325
      for(int[] move: moves)
326
        {
327
        axis     = move[0];
328
        rowBitmap= move[1];
329
        angle    = move[2]*corr;
330
        quat     = makeQuaternion(axis,angle);
331

    
332
        for(int j=0; j<NUM_CUBITS; j++)
333
          if( belongsToRotation(j,axis,rowBitmap) )
334
            {
335
            index = CUBITS[j].removeRotationNow(quat);
336
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
337
            }
338
        }
339
      }
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  private float computeAngle(float dx, float dy)
345
    {
346
    float PI = (float)Math.PI;
347
    double angle = Math.atan2(dy,dx);
348
    float ret = (float)(3*PI/2-angle);
349

    
350
    if( ret>2*PI ) ret-= 2*PI;
351

    
352
    return ret;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  private void drawCurrVertex(Canvas canvas, Paint paint, float left, float r, float stroke, float pX,float pY, float cX, float cY, float nX, float nY)
358
    {
359
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
360
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
361
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
362
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
363
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
364
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
365

    
366
    canvas.drawLine(left+pX,pY,left+cX,cY,paint);
367

    
368
    float aX = pX-cX;
369
    float aY = pY-cY;
370
    float bX = cX-nX;
371
    float bY = cY-nY;
372

    
373
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
374
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
375

    
376
    aX /= aLen;
377
    aY /= aLen;
378
    bX /= bLen;
379
    bY /= bLen;
380

    
381
    float sX = (aX-bX)/2;
382
    float sY = (aY-bY)/2;
383
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
384
    sX /= sLen;
385
    sY /= sLen;
386

    
387
    float startAngle = computeAngle(bX,-bY);
388
    float endAngle   = computeAngle(aX,-aY);
389
    float sweepAngle = endAngle-startAngle;
390
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
391

    
392
    float R = r*TEXTURE_HEIGHT+stroke/2;
393

    
394
    float A = (float)(R/(Math.cos(sweepAngle/2)));
395

    
396
    float rX = cX + A*sX;
397
    float rY = cY + A*sY;
398

    
399
    startAngle *= 180/(Math.PI);
400
    sweepAngle *= 180/(Math.PI);
401

    
402
    canvas.drawArc( left+rX-R, rY-R, left+rX+R, rY+R, startAngle, sweepAngle, false, paint);
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, float[] vertices, float stroke, int color, float r)
408
    {
409
    stroke *= TEXTURE_HEIGHT;
410

    
411
    paint.setAntiAlias(true);
412
    paint.setStrokeWidth(stroke);
413
    paint.setColor(color);
414
    paint.setStyle(Paint.Style.FILL);
415

    
416
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
417

    
418
    paint.setColor(COLOR_BLACK);
419
    paint.setStyle(Paint.Style.STROKE);
420

    
421
    int length = vertices.length;
422
    int numVertices = length/2;
423

    
424
    float prevX = vertices[length-2];
425
    float prevY = vertices[length-1];
426
    float currX = vertices[0];
427
    float currY = vertices[1];
428
    float nextX = vertices[2];
429
    float nextY = vertices[3];
430

    
431
    for(int vert=0; vert<numVertices; vert++)
432
      {
433
      drawCurrVertex(canvas, paint, left, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
434

    
435
      prevX = currX;
436
      prevY = currY;
437
      currX = nextX;
438
      currY = nextY;
439

    
440
      if( 2*(vert+2)+1 < length )
441
        {
442
        nextX = vertices[2*(vert+2)  ];
443
        nextY = vertices[2*(vert+2)+1];
444
        }
445
      else
446
        {
447
        nextX = vertices[0];
448
        nextY = vertices[1];
449
        }
450
      }
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  int getCubitFaceColorIndex(int cubit, int face)
456
    {
457
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
458
    return (int)(texMap.get0() / texMap.get2());
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
463

    
464
  void clampPos(Static3D pos)
465
    {
466
    float currError, minError = Float.MAX_VALUE;
467
    int minErrorIndex= -1;
468
    float x = pos.get0();
469
    float y = pos.get1();
470
    float z = pos.get2();
471
    float xo,yo,zo;
472

    
473
    for(int i=0; i<NUM_CUBITS; i++)
474
      {
475
      xo = mOrigPos[i].get0();
476
      yo = mOrigPos[i].get1();
477
      zo = mOrigPos[i].get2();
478

    
479
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
480

    
481
      if( currError<minError )
482
        {
483
        minError = currError;
484
        minErrorIndex = i;
485
        }
486
      }
487

    
488
    pos.set( mOrigPos[minErrorIndex] );
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
// the getFaceColors + final black in a horizontal strip.
493

    
494
  public void createTexture()
495
    {
496
    Bitmap bitmap;
497

    
498
    Paint paint = new Paint();
499
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
500
    Canvas canvas = new Canvas(bitmap);
501

    
502
    paint.setAntiAlias(true);
503
    paint.setTextAlign(Paint.Align.CENTER);
504
    paint.setStyle(Paint.Style.FILL);
505

    
506
    paint.setColor(COLOR_BLACK);
507
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
508

    
509
    for(int i=0; i<NUM_TEXTURES; i++)
510
      {
511
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT);
512
      }
513

    
514
    mTexture.setTexture(bitmap);
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public int getSize()
520
    {
521
    return mSize;
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  public void continueRotation(float angleInDegrees)
527
    {
528
    mRotationAngleStatic.set0(angleInDegrees);
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
  public Static4D getRotationQuat()
534
      {
535
      return mQuat;
536
      }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
  public void recomputeScaleFactor(int scrWidth)
541
    {
542
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
  public void savePreferences(SharedPreferences.Editor editor)
548
    {
549
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
  public synchronized void restorePreferences(SharedPreferences preferences)
555
    {
556
    for(int i=0; i<NUM_CUBITS; i++)
557
      {
558
      int index = CUBITS[i].restorePreferences(preferences);
559

    
560
      if( index<0 )
561
        {
562
        for(int j=0; j<NUM_CUBITS; j++)
563
          {
564
          CUBITS[j].modifyCurrentPosition(QUATS[0]);
565
          mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),0);
566
          }
567
        break;
568
        }
569

    
570
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
571
      }
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

    
576
  public void releaseResources()
577
    {
578
    mTexture.markForDeletion();
579
    }
580

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

    
583
  public void apply(Effect effect, int position)
584
    {
585
    mEffects.apply(effect, position);
586
    }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
  public void remove(long effectID)
591
    {
592
    mEffects.abortById(effectID);
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  public synchronized void solve()
598
    {
599
    for(int i=0; i<NUM_CUBITS; i++)
600
      {
601
      CUBITS[i].solve();
602
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
603
      }
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public void resetAllTextureMaps()
609
    {
610
    final float ratio = 1.0f/(NUM_TEXTURES+1);
611
    int color;
612

    
613
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
614
      {
615
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
616

    
617
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
618
        {
619
        color = getFaceColor(cubit,cubitface,mSize);
620
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
621
        }
622

    
623
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
624
      }
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
  public void setTextureMap(int cubit, int face, int newColor)
630
    {
631
    final float ratio = 1.0f/(NUM_TEXTURES+1);
632
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
633

    
634
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
635
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
636
    }
637

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

    
640
  public synchronized void beginNewRotation(int axis, int row )
641
    {
642
    if( axis<0 || axis>=ROTATION_AXIS.length )
643
      {
644
      android.util.Log.e("object", "invalid rotation axis: "+axis);
645
      return;
646
      }
647
    if( row<0 || row>=mSize )
648
      {
649
      android.util.Log.e("object", "invalid rotation row: "+row);
650
      return;
651
      }
652

    
653
    mRotAxis     = axis;
654
    mRotRowBitmap= (1<<row);
655
    mRotationAngleStatic.set0(0.0f);
656
    mRotationAxis.set( ROTATION_AXIS[axis] );
657
    mRotationAngle.add(mRotationAngleStatic);
658
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
659
    }
660

    
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662

    
663
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
664
    {
665
    mRotAxis     = axis;
666
    mRotRowBitmap= rowBitmap;
667

    
668
    mRotationAngleStatic.set0(0.0f);
669
    mRotationAxis.set( ROTATION_AXIS[axis] );
670
    mRotationAngle.setDuration(durationMillis);
671
    mRotationAngle.resetToBeginning();
672
    mRotationAngle.add(new Static1D(0));
673
    mRotationAngle.add(new Static1D(angle));
674
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
675
    mRotateEffect.notifyWhenFinished(listener);
676

    
677
    return mRotateEffect.getID();
678
    }
679

    
680
///////////////////////////////////////////////////////////////////////////////////////////////////
681

    
682
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
683
    {
684
    float angle = getAngle();
685
    mRotationAngleStatic.set0(angle);
686
    mRotationAngleFinal.set0(nearestAngleInDegrees);
687
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
688

    
689
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
690
    mRotationAngle.resetToBeginning();
691
    mRotationAngle.removeAll();
692
    mRotationAngle.add(mRotationAngleStatic);
693
    mRotationAngle.add(mRotationAngleMiddle);
694
    mRotationAngle.add(mRotationAngleFinal);
695
    mRotateEffect.notifyWhenFinished(listener);
696

    
697
    return mRotateEffect.getID();
698
    }
699

    
700

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702

    
703
  private float getAngle()
704
    {
705
    int pointNum = mRotationAngle.getNumPoints();
706

    
707
    if( pointNum>=1 )
708
      {
709
      return mRotationAngle.getPoint(pointNum-1).get0();
710
      }
711
    else
712
      {
713
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
714
      crashlytics.log("points in RotationAngle: "+pointNum);
715
      return 0;
716
      }
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
  public synchronized void removeRotationNow()
722
    {
723
    float angle = getAngle();
724
    double nearestAngleInRadians = angle*Math.PI/180;
725
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
726
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
727
    float axisX = ROTATION_AXIS[mRotAxis].get0();
728
    float axisY = ROTATION_AXIS[mRotAxis].get1();
729
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
730
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
731

    
732
    mRotationAngle.removeAll();
733
    mRotationAngleStatic.set0(0);
734

    
735
    for(int i=0; i<NUM_CUBITS; i++)
736
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
737
        {
738
        int index = CUBITS[i].removeRotationNow(quat);
739
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
740
        }
741
    }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744

    
745
  public void initializeObject(int[][] moves)
746
    {
747
    solve();
748
    setupPosition(moves);
749
    }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752

    
753
  public int getCubit(float[] point3D)
754
    {
755
    float dist, minDist = Float.MAX_VALUE;
756
    int currentBest=-1;
757
    float multiplier = returnMultiplier();
758

    
759
    point3D[0] *= multiplier;
760
    point3D[1] *= multiplier;
761
    point3D[2] *= multiplier;
762

    
763
    for(int i=0; i<NUM_CUBITS; i++)
764
      {
765
      dist = CUBITS[i].getDistSquared(point3D);
766
      if( dist<minDist )
767
        {
768
        minDist = dist;
769
        currentBest = i;
770
        }
771
      }
772

    
773
    return currentBest;
774
    }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
  public int computeNearestAngle(float angle, float speed)
779
    {
780
    final int NEAREST = 360/getBasicAngle();
781

    
782
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
783
    if( angle< -(NEAREST*0.5) ) tmp-=1;
784

    
785
    if( tmp!=0 ) return NEAREST*tmp;
786

    
787
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
788
    }
789

    
790
///////////////////////////////////////////////////////////////////////////////////////////////////
791

    
792
  public int getNodeSize()
793
    {
794
    return mNodeSize;
795
    }
796

    
797
///////////////////////////////////////////////////////////////////////////////////////////////////
798

    
799
  public ObjectList getObjectList()
800
    {
801
    return mList;
802
    }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805

    
806
  abstract float getScreenRatio();
807
  abstract Static3D[] getCubitPositions(int size);
808
  abstract Static4D[] getQuats();
809
  abstract int getNumFaces();
810
  abstract int getNumStickerTypes();
811
  abstract int getNumCubitFaces();
812
  abstract MeshBase createCubitMesh(int cubit);
813
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left);
814
  abstract int getFaceColor(int cubit, int cubitface, int size);
815
  abstract float returnMultiplier();
816
  abstract float[] getRowChances();
817
  abstract float getBasicStep();
818
  abstract boolean shouldResetTextureMaps();
819

    
820
  public abstract boolean isSolved();
821
  public abstract Static3D[] getRotationAxis();
822
  public abstract int getBasicAngle();
823
  public abstract String retObjectString();
824
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
825
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
826
  }
(17-17/19)