Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ b89898c5

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
  static final int TEXTURE_HEIGHT = 256;
69

    
70
  static final float SQ2 = (float)Math.sqrt(2);
71
  static final float SQ3 = (float)Math.sqrt(3);
72
  static final float SQ6 = (float)Math.sqrt(6);
73

    
74
  private static final float NODE_RATIO = 1.40f;
75
  private static final float MAX_SIZE_CHANGE = 1.35f;
76
  private static final float MIN_SIZE_CHANGE = 0.8f;
77

    
78
  private static boolean mCreateFromDMesh = false;
79

    
80
  private static final Static3D CENTER = new Static3D(0,0,0);
81
  private static final int POST_ROTATION_MILLISEC = 500;
82

    
83
  final Static3D[] ROTATION_AXIS;
84
  final Static4D[] QUATS;
85
  final Cubit[] CUBITS;
86
  final int NUM_FACES;
87
  final int NUM_TEXTURES;
88
  final int NUM_CUBIT_FACES;
89
  final int NUM_AXIS;
90
  final int NUM_CUBITS;
91
  final float[] CUTS;
92
  final int NUM_CUTS;
93

    
94
  private static float mInitScreenRatio;
95
  private static float mObjectScreenRatio = 1.0f;
96

    
97
  private final int mNodeSize;
98
  private int mRotRowBitmap;
99
  private int mRotAxis;
100
  private Static3D[] mOrigPos;
101
  private Static3D mNodeScale;
102
  private Static4D mQuat;
103
  private int mNumLayers, mRealSize;
104
  private ObjectList mList;
105
  private MeshBase mMesh;
106
  private DistortedEffects mEffects;
107
  private VertexEffectRotate mRotateEffect;
108
  private Dynamic1D mRotationAngle;
109
  private Static3D mRotationAxis;
110
  private Static3D mObjectScale;
111

    
112
  float[] mRowChances;
113
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
114
  DistortedTexture mTexture;
115
  MatrixEffectScale mScaleEffect;
116
  MatrixEffectQuaternion mQuatEffect;
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  TwistyObject(int numLayers, int realSize, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
121
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
122
    {
123
    super(nodeTexture,nodeEffects,nodeMesh);
124

    
125
    mNodeSize = screenWidth;
126

    
127
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
128

    
129
    mNumLayers = numLayers;
130
    mRealSize = realSize;
131
    mList = list;
132
    mOrigPos = getCubitPositions(mNumLayers);
133

    
134
    QUATS = getQuats();
135
    NUM_CUBITS  = mOrigPos.length;
136
    ROTATION_AXIS = getRotationAxis();
137
    NUM_AXIS = ROTATION_AXIS.length;
138
    mInitScreenRatio = getScreenRatio();
139
    NUM_FACES = getNumFaces();
140
    NUM_CUBIT_FACES = getNumCubitFaces();
141
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
142
    CUTS = getCuts(mNumLayers);
143
    NUM_CUTS = CUTS.length;
144

    
145
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
146
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
147

    
148
    mNodeScale= new Static3D(1,NODE_RATIO,1);
149
    mQuat = quat;
150

    
151
    mRowChances = getRowChances();
152

    
153
    mRotationAngle= new Dynamic1D();
154
    mRotationAxis = new Static3D(1,0,0);
155
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
156

    
157
    mRotationAngleStatic = new Static1D(0);
158
    mRotationAngleMiddle = new Static1D(0);
159
    mRotationAngleFinal  = new Static1D(0);
160

    
161
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
162
    mObjectScale = new Static3D(scale,scale,scale);
163
    mScaleEffect = new MatrixEffectScale(mObjectScale);
164
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
165

    
166
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
167
    nodeEffects.apply(nodeScaleEffect);
168

    
169
    CUBITS = new Cubit[NUM_CUBITS];
170
    createMeshAndCubits(list,res);
171

    
172
    mTexture = new DistortedTexture();
173
    mEffects = new DistortedEffects();
174

    
175
    int num_quats = QUATS.length;
176
    for(int q=0; q<num_quats; q++)
177
      {
178
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
179
      vq.setMeshAssociation(0,q);
180
      mEffects.apply(vq);
181
      }
182

    
183
    mEffects.apply(mRotateEffect);
184
    mEffects.apply(mQuatEffect);
185
    mEffects.apply(mScaleEffect);
186

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

    
191
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
192

    
193
    setupPosition(moves);
194

    
195
    setProjection(fov, 0.1f);
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void createMeshAndCubits(ObjectList list, Resources res)
201
    {
202
    if( mCreateFromDMesh )
203
      {
204
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
205
      int resourceID= list.getResourceIDs()[sizeIndex];
206

    
207
      InputStream is = res.openRawResource(resourceID);
208
      DataInputStream dos = new DataInputStream(is);
209
      mMesh = new MeshFile(dos);
210

    
211
      try
212
        {
213
        is.close();
214
        }
215
      catch(IOException e)
216
        {
217
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
218
        }
219

    
220
      for(int i=0; i<NUM_CUBITS; i++)
221
        {
222
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
223
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
224
        }
225

    
226
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
227
      }
228
    else
229
      {
230
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
231

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

    
240
      mMesh = new MeshJoined(cubitMesh);
241
      resetAllTextureMaps();
242
      }
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public void setObjectRatio(float sizeChange)
248
    {
249
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
250

    
251
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
252
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
253

    
254
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
255
    mObjectScale.set(scale,scale,scale);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  static float getObjectRatio()
261
    {
262
    return mObjectScreenRatio*mInitScreenRatio;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  int computeRow(float x, float y, float z, int rotIndex)
268
    {
269
    Static3D axis = ROTATION_AXIS[rotIndex];
270
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
271

    
272
    for(int i=0; i<NUM_CUTS; i++)
273
      {
274
      if( tmp<CUTS[i] ) return i;
275
      }
276

    
277
    return NUM_CUTS;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
283
    {
284
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
285
    return ((1<<cubitRow)&rowBitmap)!=0;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
// note the minus in front of the sin() - we rotate counterclockwise
290
// when looking towards the direction where the axis increases in values.
291

    
292
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
293
    {
294
    Static3D axis = ROTATION_AXIS[axisIndex];
295

    
296
    while( angleInDegrees<0 ) angleInDegrees += 360;
297
    angleInDegrees %= 360;
298
    
299
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
300
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
301

    
302
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private synchronized void setupPosition(int[][] moves)
308
    {
309
    if( moves!=null )
310
      {
311
      Static4D quat;
312
      int index, axis, rowBitmap, angle;
313
      int corr = (360/getBasicAngle());
314

    
315
      for(int[] move: moves)
316
        {
317
        axis     = move[0];
318
        rowBitmap= move[1];
319
        angle    = move[2]*corr;
320
        quat     = makeQuaternion(axis,angle);
321

    
322
        for(int j=0; j<NUM_CUBITS; j++)
323
          if( belongsToRotation(j,axis,rowBitmap) )
324
            {
325
            index = CUBITS[j].removeRotationNow(quat);
326
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
327
            }
328
        }
329
      }
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  int getCubitFaceColorIndex(int cubit, int face)
335
    {
336
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
337
    return (int)(texMap.get0() / texMap.get2());
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
342

    
343
  void clampPos(Static3D pos)
344
    {
345
    float currError, minError = Float.MAX_VALUE;
346
    int minErrorIndex= -1;
347
    float x = pos.get0();
348
    float y = pos.get1();
349
    float z = pos.get2();
350
    float xo,yo,zo;
351

    
352
    for(int i=0; i<NUM_CUBITS; i++)
353
      {
354
      xo = mOrigPos[i].get0();
355
      yo = mOrigPos[i].get1();
356
      zo = mOrigPos[i].get2();
357

    
358
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
359

    
360
      if( currError<minError )
361
        {
362
        minError = currError;
363
        minErrorIndex = i;
364
        }
365
      }
366

    
367
    pos.set( mOrigPos[minErrorIndex] );
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
// the getFaceColors + final black in a horizontal strip.
372

    
373
  public void createTexture()
374
    {
375
    Bitmap bitmap;
376

    
377
    Paint paint = new Paint();
378
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
379
    Canvas canvas = new Canvas(bitmap);
380

    
381
    paint.setAntiAlias(true);
382
    paint.setTextAlign(Paint.Align.CENTER);
383
    paint.setStyle(Paint.Style.FILL);
384

    
385
    paint.setColor(COLOR_BLACK);
386
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
387

    
388
    for(int i=0; i<NUM_TEXTURES; i++)
389
      {
390
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT);
391
      }
392

    
393
    mTexture.setTexture(bitmap);
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public int getNumLayers()
399
    {
400
    return mNumLayers;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  public void continueRotation(float angleInDegrees)
406
    {
407
    mRotationAngleStatic.set0(angleInDegrees);
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  public Static4D getRotationQuat()
413
      {
414
      return mQuat;
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  public void recomputeScaleFactor(int scrWidth)
420
    {
421
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void savePreferences(SharedPreferences.Editor editor)
427
    {
428
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  public synchronized void restorePreferences(SharedPreferences preferences)
434
    {
435
    for(int i=0; i<NUM_CUBITS; i++)
436
      {
437
      int index = CUBITS[i].restorePreferences(preferences);
438

    
439
      if( index<0 )
440
        {
441
        for(int j=0; j<NUM_CUBITS; j++)
442
          {
443
          CUBITS[j].modifyCurrentPosition(QUATS[0]);
444
          mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),0);
445
          }
446
        break;
447
        }
448

    
449
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
450
      }
451
    }
452

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

    
455
  public void releaseResources()
456
    {
457
    mTexture.markForDeletion();
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  public void apply(Effect effect, int position)
463
    {
464
    mEffects.apply(effect, position);
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  public void remove(long effectID)
470
    {
471
    mEffects.abortById(effectID);
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475

    
476
  public synchronized void solve()
477
    {
478
    for(int i=0; i<NUM_CUBITS; i++)
479
      {
480
      CUBITS[i].solve();
481
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
482
      }
483
    }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

    
487
  public void resetAllTextureMaps()
488
    {
489
    final float ratio = 1.0f/(NUM_TEXTURES+1);
490
    int color;
491

    
492
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
493
      {
494
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
495

    
496
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
497
        {
498
        color = getFaceColor(cubit,cubitface,mNumLayers);
499
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
500
        }
501

    
502
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
503
      }
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  public void setTextureMap(int cubit, int face, int newColor)
509
    {
510
    final float ratio = 1.0f/(NUM_TEXTURES+1);
511
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
512

    
513
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
514
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
515
    }
516

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

    
519
  public synchronized void beginNewRotation(int axis, int row )
520
    {
521
    if( axis<0 || axis>=ROTATION_AXIS.length )
522
      {
523
      android.util.Log.e("object", "invalid rotation axis: "+axis);
524
      return;
525
      }
526
    if( row<0 || row>=mNumLayers )
527
      {
528
      android.util.Log.e("object", "invalid rotation row: "+row);
529
      return;
530
      }
531

    
532
    mRotAxis     = axis;
533
    mRotRowBitmap= (1<<row);
534
    mRotationAngleStatic.set0(0.0f);
535
    mRotationAxis.set( ROTATION_AXIS[axis] );
536
    mRotationAngle.add(mRotationAngleStatic);
537
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
543
    {
544
    mRotAxis     = axis;
545
    mRotRowBitmap= rowBitmap;
546

    
547
    mRotationAngleStatic.set0(0.0f);
548
    mRotationAxis.set( ROTATION_AXIS[axis] );
549
    mRotationAngle.setDuration(durationMillis);
550
    mRotationAngle.resetToBeginning();
551
    mRotationAngle.add(new Static1D(0));
552
    mRotationAngle.add(new Static1D(angle));
553
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
554
    mRotateEffect.notifyWhenFinished(listener);
555

    
556
    return mRotateEffect.getID();
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
562
    {
563
    float angle = getAngle();
564
    mRotationAngleStatic.set0(angle);
565
    mRotationAngleFinal.set0(nearestAngleInDegrees);
566
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
567

    
568
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
569
    mRotationAngle.resetToBeginning();
570
    mRotationAngle.removeAll();
571
    mRotationAngle.add(mRotationAngleStatic);
572
    mRotationAngle.add(mRotationAngleMiddle);
573
    mRotationAngle.add(mRotationAngleFinal);
574
    mRotateEffect.notifyWhenFinished(listener);
575

    
576
    return mRotateEffect.getID();
577
    }
578

    
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
  private float getAngle()
583
    {
584
    int pointNum = mRotationAngle.getNumPoints();
585

    
586
    if( pointNum>=1 )
587
      {
588
      return mRotationAngle.getPoint(pointNum-1).get0();
589
      }
590
    else
591
      {
592
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
593
      crashlytics.log("points in RotationAngle: "+pointNum);
594
      return 0;
595
      }
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599

    
600
  public synchronized void removeRotationNow()
601
    {
602
    float angle = getAngle();
603
    double nearestAngleInRadians = angle*Math.PI/180;
604
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
605
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
606
    float axisX = ROTATION_AXIS[mRotAxis].get0();
607
    float axisY = ROTATION_AXIS[mRotAxis].get1();
608
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
609
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
610

    
611
    mRotationAngle.removeAll();
612
    mRotationAngleStatic.set0(0);
613

    
614
    for(int i=0; i<NUM_CUBITS; i++)
615
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
616
        {
617
        int index = CUBITS[i].removeRotationNow(quat);
618
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
619
        }
620
    }
621

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623

    
624
  public void initializeObject(int[][] moves)
625
    {
626
    solve();
627
    setupPosition(moves);
628
    }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631

    
632
  public int getCubit(float[] point3D)
633
    {
634
    float dist, minDist = Float.MAX_VALUE;
635
    int currentBest=-1;
636
    float multiplier = returnMultiplier();
637

    
638
    point3D[0] *= multiplier;
639
    point3D[1] *= multiplier;
640
    point3D[2] *= multiplier;
641

    
642
    for(int i=0; i<NUM_CUBITS; i++)
643
      {
644
      dist = CUBITS[i].getDistSquared(point3D);
645
      if( dist<minDist )
646
        {
647
        minDist = dist;
648
        currentBest = i;
649
        }
650
      }
651

    
652
    return currentBest;
653
    }
654

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
  public int computeNearestAngle(float angle, float speed)
658
    {
659
    final int NEAREST = 360/getBasicAngle();
660

    
661
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
662
    if( angle< -(NEAREST*0.5) ) tmp-=1;
663

    
664
    if( tmp!=0 ) return NEAREST*tmp;
665

    
666
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
667
    }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
  public int getNodeSize()
672
    {
673
    return mNodeSize;
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677

    
678
  public ObjectList getObjectList()
679
    {
680
    return mList;
681
    }
682

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

    
685
  abstract float getScreenRatio();
686
  abstract Static3D[] getCubitPositions(int size);
687
  abstract Static4D[] getQuats();
688
  abstract int getNumFaces();
689
  abstract int getNumStickerTypes();
690
  abstract int getNumCubitFaces();
691
  abstract MeshBase createCubitMesh(int cubit);
692
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left);
693
  abstract int getFaceColor(int cubit, int cubitface, int size);
694
  abstract float returnMultiplier();
695
  abstract float[] getRowChances();
696
  abstract float[] getCuts(int size);
697
  abstract boolean shouldResetTextureMaps();
698

    
699
  public abstract boolean isSolved();
700
  public abstract Static3D[] getRotationAxis();
701
  public abstract int getBasicAngle();
702
  public abstract String retObjectString();
703
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
704
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
705
  }
(19-19/22)