Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ e69c7a11

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.MeshRectangles;
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 int NUM_FACES;
72
  final int NUM_CUBIT_FACES;
73
  final int NUM_AXIS;
74

    
75
  private static float mInitScreenRatio,mObjectScreenRatio;
76

    
77
  private final int NUM_CUBITS;
78
  private final int mNodeSize;
79
  private int mRotRowBitmap;
80
  private int mRotAxis;
81
  private Static3D[] mOrigPos;
82
  private Static3D mNodeScale;
83
  private Static4D mQuat;
84
  private Cubit[] mCubits;
85
  private int mSize;
86
  private RubikObjectList mList;
87
  private MeshBase mMesh;
88
  private DistortedEffects mEffects;
89
  private VertexEffectRotate mRotateEffect;
90
  private Dynamic1D mRotationAngle;
91
  private Static3D mRotationAxis;
92
  private Static3D mObjectScale;
93

    
94
  float mStart, mStep;
95
  float[] mRowChances;
96

    
97
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
98
  DistortedTexture mTexture;
99

    
100
  MatrixEffectScale mScaleEffect;
101
  MatrixEffectQuaternion mQuatEffect;
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  RubikObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshRectangles nodeMesh,
106
              DistortedEffects nodeEffects, int[][] moves, RubikObjectList list, Resources res, int screenWidth)
107
    {
108
    super(nodeTexture,nodeEffects,nodeMesh);
109

    
110
    mNodeSize = screenWidth;
111

    
112
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
113

    
114
    mList = list;
115
    mOrigPos = getCubitPositions(size);
116

    
117
    QUATS = getQuats();
118
    NUM_CUBITS  = mOrigPos.length;
119
    ROTATION_AXIS = getRotationAxis();
120
    NUM_AXIS = ROTATION_AXIS.length;
121
    mObjectScreenRatio = getScreenRatio();
122
    mInitScreenRatio = mObjectScreenRatio;
123
    NUM_FACES = getNumFaces();
124
    NUM_CUBIT_FACES = getNumCubitFaces();
125

    
126
    mSize = size;
127
    computeStartAndStep(mOrigPos);
128
    mNodeScale= new Static3D(1,NODE_RATIO,1);
129
    mQuat = quat;
130

    
131
    mRowChances = getRowChances();
132

    
133
    mRotationAngle= new Dynamic1D();
134
    mRotationAxis = new Static3D(1,0,0);
135
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
136

    
137
    mRotationAngleStatic = new Static1D(0);
138
    mRotationAngleMiddle = new Static1D(0);
139
    mRotationAngleFinal  = new Static1D(0);
140

    
141
    float scale  = mObjectScreenRatio*mNodeSize/mSize;
142
    mObjectScale = new Static3D(scale,scale,scale);
143
    mScaleEffect = new MatrixEffectScale(mObjectScale);
144
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
145

    
146
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
147
    nodeEffects.apply(nodeScaleEffect);
148

    
149
    createMeshAndCubits(list,res);
150

    
151
    mTexture = new DistortedTexture();
152
    mEffects = new DistortedEffects();
153

    
154
    int num_quats = QUATS.length;
155
    for(int q=0; q<num_quats; q++)
156
      {
157
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
158
      vq.setMeshAssociation(0,q);
159
      mEffects.apply(vq);
160
      }
161

    
162
    mEffects.apply(mRotateEffect);
163
    mEffects.apply(mQuatEffect);
164
    mEffects.apply(mScaleEffect);
165

    
166
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
167

    
168
    setupPosition(moves);
169

    
170
    setProjection(fov, 0.1f);
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private void createMeshAndCubits(RubikObjectList list, Resources res)
176
    {
177
    mCubits = new Cubit[NUM_CUBITS];
178

    
179
    if( mCreateFromDMesh )
180
      {
181
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
182
      int resourceID= list.getResourceIDs()[sizeIndex];
183

    
184
      InputStream is = res.openRawResource(resourceID);
185
      DataInputStream dos = new DataInputStream(is);
186
      mMesh = new MeshFile(dos);
187

    
188
      try
189
        {
190
        is.close();
191
        }
192
      catch(IOException e)
193
        {
194
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
195
        }
196

    
197
      for(int i=0; i<NUM_CUBITS; i++)
198
        {
199
        mCubits[i] = new Cubit(this,mOrigPos[i]);
200
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
201
        }
202
      }
203
    else
204
      {
205
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
206

    
207
      for(int i=0; i<NUM_CUBITS; i++)
208
        {
209
        mCubits[i] = new Cubit(this,mOrigPos[i]);
210
        cubitMesh[i] = createCubitMesh(i);
211
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
212
        cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
213
        }
214

    
215
      mMesh = new MeshJoined(cubitMesh);
216
      resetAllTextureMaps();
217
      }
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public void setObjectRatio(float sizeChange)
223
    {
224
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
225

    
226
    if( mObjectScreenRatio > MAX_SIZE_CHANGE*mInitScreenRatio)
227
      {
228
      mObjectScreenRatio = MAX_SIZE_CHANGE*mInitScreenRatio;
229
      }
230
    if( mObjectScreenRatio < MIN_SIZE_CHANGE*mInitScreenRatio)
231
      {
232
      mObjectScreenRatio = MIN_SIZE_CHANGE*mInitScreenRatio;
233
      }
234

    
235
    float scale = mObjectScreenRatio*mNodeSize/mSize;
236
    mObjectScale.set(scale,scale,scale);
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  static float getObjectRatio()
242
    {
243
    return mObjectScreenRatio;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
248
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
249
// consecutive).
250
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
251
// the Cube and the Pyraminx.
252
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
253
// given Cubit belongs to.
254

    
255
  private void computeStartAndStep(Static3D[] pos)
256
    {
257
    float min = Float.MAX_VALUE;
258
    float max = Float.MIN_VALUE;
259
    float axisX = ROTATION_AXIS[0].get0();
260
    float axisY = ROTATION_AXIS[0].get1();
261
    float axisZ = ROTATION_AXIS[0].get2();
262
    float tmp;
263

    
264
    for(int i=0; i<NUM_CUBITS; i++)
265
      {
266
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
267
      if( tmp<min ) min=tmp;
268
      if( tmp>max ) max=tmp;
269
      }
270

    
271
    mStart = min;
272
    mStep  = (max-min+1.0f)/mSize;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
278
    {
279
    int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
280
    return ((1<<cubitRow)&rowBitmap)!=0;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
// note the minus in front of the sin() - we rotate counterclockwise
285
// when looking towards the direction where the axis increases in values.
286

    
287
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
288
    {
289
    Static3D axis = ROTATION_AXIS[axisIndex];
290

    
291
    while( angleInDegrees<0 ) angleInDegrees += 360;
292
    angleInDegrees %= 360;
293
    
294
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
295
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
296

    
297
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  private synchronized void setupPosition(int[][] moves)
303
    {
304
    if( moves!=null )
305
      {
306
      Static4D quat;
307
      int index, axis, rowBitmap, angle;
308
      int corr = (360/getBasicAngle());
309

    
310
      for(int[] move: moves)
311
        {
312
        axis     = move[0];
313
        rowBitmap= move[1];
314
        angle    = move[2]*corr;
315
        quat     = makeQuaternion(axis,angle);
316

    
317
        for(int j=0; j<NUM_CUBITS; j++)
318
          if( belongsToRotation(j,axis,rowBitmap) )
319
            {
320
            index = mCubits[j].removeRotationNow(quat);
321
            mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
322
            }
323
        }
324
      }
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
329
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
330
// away from the face of the Pyraminx shouldn't be textured.
331

    
332
  boolean isOnFace( int cubit, int axis, int row)
333
    {
334
    final float MAX_ERROR = 0.0001f;
335
    float diff = mCubits[cubit].mRotationRow[axis] - row;
336
    return diff*diff < MAX_ERROR;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  int getCubitFaceColorIndex(int cubit, int face)
342
    {
343
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
344
    return (int)(texMap.get0() / texMap.get2());
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
349

    
350
  void clampPos(Static3D pos)
351
    {
352
    float currError, minError = Float.MAX_VALUE;
353
    int minErrorIndex= -1;
354
    float x = pos.get0();
355
    float y = pos.get1();
356
    float z = pos.get2();
357
    float xo,yo,zo;
358

    
359
    for(int i=0; i<NUM_CUBITS; i++)
360
      {
361
      xo = mOrigPos[i].get0();
362
      yo = mOrigPos[i].get1();
363
      zo = mOrigPos[i].get2();
364

    
365
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
366

    
367
      if( currError<minError )
368
        {
369
        minError = currError;
370
        minErrorIndex = i;
371
        }
372
      }
373

    
374
    pos.set( mOrigPos[minErrorIndex] );
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378
// the getFaceColors + final black in a horizontal strip.
379

    
380
  public void createTexture()
381
    {
382
    Bitmap bitmap;
383

    
384
    Paint paint = new Paint();
385
    bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
386
    Canvas canvas = new Canvas(bitmap);
387

    
388
    paint.setAntiAlias(true);
389
    paint.setTextAlign(Paint.Align.CENTER);
390
    paint.setStyle(Paint.Style.FILL);
391

    
392
    paint.setColor(INTERIOR_COLOR);
393
    canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
394

    
395
    for(int i=0; i<NUM_FACES; i++)
396
      {
397
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
398
      }
399

    
400
    mTexture.setTexture(bitmap);
401
    }
402

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

    
405
  public int getSize()
406
    {
407
    return mSize;
408
    }
409

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

    
412
  public void continueRotation(float angleInDegrees)
413
    {
414
    mRotationAngleStatic.set0(angleInDegrees);
415
    }
416

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

    
419
  public Static4D getRotationQuat()
420
      {
421
      return mQuat;
422
      }
423

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

    
426
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
427
    {
428
    float factor = Math.min(scrWidth,scrHeight);
429
    mNodeScale.set(factor,NODE_RATIO*factor,factor);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  public void savePreferences(SharedPreferences.Editor editor)
435
    {
436
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  public synchronized void restorePreferences(SharedPreferences preferences)
442
    {
443
    for(int i=0; i<NUM_CUBITS; i++)
444
      {
445
      int index = mCubits[i].restorePreferences(preferences);
446
      mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
447
      }
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  public void releaseResources()
453
    {
454
    mTexture.markForDeletion();
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public void apply(Effect effect, int position)
460
    {
461
    mEffects.apply(effect, position);
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public void remove(long effectID)
467
    {
468
    mEffects.abortById(effectID);
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
  public synchronized void solve()
474
    {
475
    for(int i=0; i<NUM_CUBITS; i++)
476
      {
477
      mCubits[i].solve();
478
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
479
      }
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public boolean isSolved()
485
    {
486
    int index = mCubits[0].mQuatIndex;
487

    
488
    for(int i=1; i<NUM_CUBITS; i++)
489
      {
490
      if( !thereIsNoVisibleDifference(mCubits[i], index) ) return false;
491
      }
492

    
493
    return true;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public void resetAllTextureMaps()
499
    {
500
    final float ratio = 1.0f/(NUM_FACES+1);
501
    int color;
502

    
503
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
504
      {
505
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
506

    
507
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
508
        {
509
        color = getFaceColor(cubit,cubitface,mSize);
510
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
511
        }
512

    
513
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
514
      }
515
    }
516

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

    
519
  public void setTextureMap(int cubit, int face, int newColor)
520
    {
521
    final float ratio = 1.0f/(NUM_FACES+1);
522
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
523

    
524
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
525
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public synchronized void beginNewRotation(int axis, int row )
531
    {
532
    if( axis<0 || axis>=ROTATION_AXIS.length )
533
      {
534
      android.util.Log.e("object", "invalid rotation axis: "+axis);
535
      return;
536
      }
537
    if( row<0 || row>=mSize )
538
      {
539
      android.util.Log.e("object", "invalid rotation row: "+row);
540
      return;
541
      }
542

    
543
    mRotAxis     = axis;
544
    mRotRowBitmap= (1<<row);
545
    mRotationAngleStatic.set0(0.0f);
546
    mRotationAxis.set( ROTATION_AXIS[axis] );
547
    mRotationAngle.add(mRotationAngleStatic);
548
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
554
    {
555
    mRotAxis     = axis;
556
    mRotRowBitmap= rowBitmap;
557

    
558
    mRotationAngleStatic.set0(0.0f);
559
    mRotationAxis.set( ROTATION_AXIS[axis] );
560
    mRotationAngle.setDuration(durationMillis);
561
    mRotationAngle.resetToBeginning();
562
    mRotationAngle.add(new Static1D(0));
563
    mRotationAngle.add(new Static1D(angle));
564
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
565
    mRotateEffect.notifyWhenFinished(listener);
566

    
567
    return mRotateEffect.getID();
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
573
    {
574
    float angle = getAngle();
575
    mRotationAngleStatic.set0(angle);
576
    mRotationAngleFinal.set0(nearestAngleInDegrees);
577
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
578

    
579
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
580
    mRotationAngle.resetToBeginning();
581
    mRotationAngle.removeAll();
582
    mRotationAngle.add(mRotationAngleStatic);
583
    mRotationAngle.add(mRotationAngleMiddle);
584
    mRotationAngle.add(mRotationAngleFinal);
585
    mRotateEffect.notifyWhenFinished(listener);
586

    
587
    return mRotateEffect.getID();
588
    }
589

    
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592

    
593
  private float getAngle()
594
    {
595
    int pointNum = mRotationAngle.getNumPoints();
596

    
597
    if( pointNum>=1 )
598
      {
599
      return mRotationAngle.getPoint(pointNum-1).get0();
600
      }
601
    else
602
      {
603
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
604
      crashlytics.log("points in RotationAngle: "+pointNum);
605
      return 0;
606
      }
607
    }
608

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

    
611
  public synchronized void removeRotationNow()
612
    {
613
    float angle = getAngle();
614
    double nearestAngleInRadians = angle*Math.PI/180;
615
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
616
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
617
    float axisX = ROTATION_AXIS[mRotAxis].get0();
618
    float axisY = ROTATION_AXIS[mRotAxis].get1();
619
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
620
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
621

    
622
    mRotationAngle.removeAll();
623
    mRotationAngleStatic.set0(0);
624

    
625
    for(int i=0; i<NUM_CUBITS; i++)
626
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
627
        {
628
        int index = mCubits[i].removeRotationNow(quat);
629
        mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
630
        }
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  public void initializeObject(int[][] moves)
636
    {
637
    solve();
638
    setupPosition(moves);
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642

    
643
  public int getCubit(float[] point3D)
644
    {
645
    float dist, minDist = Float.MAX_VALUE;
646
    int currentBest=-1;
647
    float multiplier = returnMultiplier();
648

    
649
    point3D[0] *= multiplier;
650
    point3D[1] *= multiplier;
651
    point3D[2] *= multiplier;
652

    
653
    for(int i=0; i<NUM_CUBITS; i++)
654
      {
655
      dist = mCubits[i].getDistSquared(point3D);
656
      if( dist<minDist )
657
        {
658
        minDist = dist;
659
        currentBest = i;
660
        }
661
      }
662

    
663
    return currentBest;
664
    }
665

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667

    
668
  public int computeNearestAngle(float angle, float speed)
669
    {
670
    final int NEAREST = 360/getBasicAngle();
671

    
672
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
673
    if( angle< -(NEAREST*0.5) ) tmp-=1;
674

    
675
    if( tmp!=0 ) return NEAREST*tmp;
676

    
677
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
678
    }
679

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

    
682
  public int getNodeSize()
683
    {
684
    return mNodeSize;
685
    }
686

    
687
///////////////////////////////////////////////////////////////////////////////////////////////////
688

    
689
  public RubikObjectList getObjectList()
690
    {
691
    return mList;
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  abstract float getScreenRatio();
697
  abstract Static3D[] getCubitPositions(int size);
698
  abstract Static4D[] getQuats();
699
  abstract int getNumFaces();
700
  abstract int getNumCubitFaces();
701
  abstract MeshBase createCubitMesh(int cubit);
702
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
703
  abstract int getFaceColor(int cubit, int cubitface, int size);
704
  abstract float returnMultiplier();
705
  abstract float[] getRowChances();
706
  abstract boolean thereIsNoVisibleDifference(Cubit cubit, int quatIndex);
707

    
708
  public abstract Static3D[] getRotationAxis();
709
  public abstract int getBasicAngle();
710
  public abstract int computeRowFromOffset(float offset);
711
  public abstract float returnRotationFactor(float offset);
712
  public abstract String retObjectString();
713
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
714
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
715
  }
(8-8/10)