Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 2f5783d4

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

    
78
  private static float mInitScreenRatio,mObjectScreenRatio;
79

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

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

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

    
101
  MatrixEffectScale mScaleEffect;
102
  MatrixEffectQuaternion mQuatEffect;
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

    
111
    mNodeSize = screenWidth;
112

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

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

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

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

    
133
    mRowChances = getRowChances();
134

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

    
139
    mRotationAngleStatic = new Static1D(0);
140
    mRotationAngleMiddle = new Static1D(0);
141
    mRotationAngleFinal  = new Static1D(0);
142

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

    
148
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
149
    nodeEffects.apply(nodeScaleEffect);
150

    
151
    CUBITS = new Cubit[NUM_CUBITS];
152
    createMeshAndCubits(list,res);
153

    
154
    mTexture = new DistortedTexture();
155
    mEffects = new DistortedEffects();
156

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

    
165
    mEffects.apply(mRotateEffect);
166
    mEffects.apply(mQuatEffect);
167
    mEffects.apply(mScaleEffect);
168

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

    
173
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
174

    
175
    setupPosition(moves);
176

    
177
    setProjection(fov, 0.1f);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private void createMeshAndCubits(RubikObjectList list, Resources res)
183
    {
184
    if( mCreateFromDMesh )
185
      {
186
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
187
      int resourceID= list.getResourceIDs()[sizeIndex];
188

    
189
      InputStream is = res.openRawResource(resourceID);
190
      DataInputStream dos = new DataInputStream(is);
191
      mMesh = new MeshFile(dos);
192

    
193
      try
194
        {
195
        is.close();
196
        }
197
      catch(IOException e)
198
        {
199
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
200
        }
201

    
202
      for(int i=0; i<NUM_CUBITS; i++)
203
        {
204
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
205
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
206
        }
207
      }
208
    else
209
      {
210
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
211

    
212
      for(int i=0; i<NUM_CUBITS; i++)
213
        {
214
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
215
        cubitMesh[i] = createCubitMesh(i);
216
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
217
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
218
        }
219

    
220
      mMesh = new MeshJoined(cubitMesh);
221
      resetAllTextureMaps();
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public void setObjectRatio(float sizeChange)
228
    {
229
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
230

    
231
    if( mObjectScreenRatio > MAX_SIZE_CHANGE*mInitScreenRatio)
232
      {
233
      mObjectScreenRatio = MAX_SIZE_CHANGE*mInitScreenRatio;
234
      }
235
    if( mObjectScreenRatio < MIN_SIZE_CHANGE*mInitScreenRatio)
236
      {
237
      mObjectScreenRatio = MIN_SIZE_CHANGE*mInitScreenRatio;
238
      }
239

    
240
    float scale = mObjectScreenRatio*mNodeSize/mSize;
241
    mObjectScale.set(scale,scale,scale);
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  static float getObjectRatio()
247
    {
248
    return mObjectScreenRatio;
249
    }
250

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

    
260
  private void computeStartAndStep(Static3D[] pos)
261
    {
262
    float min = Float.MAX_VALUE;
263
    float max = Float.MIN_VALUE;
264
    float axisX = ROTATION_AXIS[0].get0();
265
    float axisY = ROTATION_AXIS[0].get1();
266
    float axisZ = ROTATION_AXIS[0].get2();
267
    float tmp;
268

    
269
    for(int i=0; i<NUM_CUBITS; i++)
270
      {
271
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
272
      if( tmp<min ) min=tmp;
273
      if( tmp>max ) max=tmp;
274
      }
275

    
276
    mStart = min;
277
    mStep  = (max-min+1.0f)/mSize;
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
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
334
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
335
// away from the face of the Pyraminx shouldn't be textured.
336

    
337
  boolean isOnFace( int cubit, int axis, int row)
338
    {
339
    final float MAX_ERROR = 0.0001f;
340
    float diff = CUBITS[cubit].mRotationRow[axis] - row;
341
    return diff*diff < MAX_ERROR;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  int getCubitFaceColorIndex(int cubit, int face)
347
    {
348
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
349
    return (int)(texMap.get0() / texMap.get2());
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
354

    
355
  void clampPos(Static3D pos)
356
    {
357
    float currError, minError = Float.MAX_VALUE;
358
    int minErrorIndex= -1;
359
    float x = pos.get0();
360
    float y = pos.get1();
361
    float z = pos.get2();
362
    float xo,yo,zo;
363

    
364
    for(int i=0; i<NUM_CUBITS; i++)
365
      {
366
      xo = mOrigPos[i].get0();
367
      yo = mOrigPos[i].get1();
368
      zo = mOrigPos[i].get2();
369

    
370
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
371

    
372
      if( currError<minError )
373
        {
374
        minError = currError;
375
        minErrorIndex = i;
376
        }
377
      }
378

    
379
    pos.set( mOrigPos[minErrorIndex] );
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
// the getFaceColors + final black in a horizontal strip.
384

    
385
  public void createTexture()
386
    {
387
    Bitmap bitmap;
388

    
389
    Paint paint = new Paint();
390
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
391
    Canvas canvas = new Canvas(bitmap);
392

    
393
    paint.setAntiAlias(true);
394
    paint.setTextAlign(Paint.Align.CENTER);
395
    paint.setStyle(Paint.Style.FILL);
396

    
397
    paint.setColor(INTERIOR_COLOR);
398
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
399

    
400
    for(int i=0; i<NUM_TEXTURES; i++)
401
      {
402
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
403
      }
404

    
405
    mTexture.setTexture(bitmap);
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  public int getSize()
411
    {
412
    return mSize;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public void continueRotation(float angleInDegrees)
418
    {
419
    mRotationAngleStatic.set0(angleInDegrees);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  public Static4D getRotationQuat()
425
      {
426
      return mQuat;
427
      }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
432
    {
433
    float factor = Math.min(scrWidth,scrHeight);
434
    mNodeScale.set(factor,NODE_RATIO*factor,factor);
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public void savePreferences(SharedPreferences.Editor editor)
440
    {
441
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public synchronized void restorePreferences(SharedPreferences preferences)
447
    {
448
    for(int i=0; i<NUM_CUBITS; i++)
449
      {
450
      int index = CUBITS[i].restorePreferences(preferences);
451
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
452
      }
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
  public void releaseResources()
458
    {
459
    mTexture.markForDeletion();
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

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

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

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

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

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

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

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

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

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

    
504
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
505
      }
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

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

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

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

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

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

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

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

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

    
558
    return mRotateEffect.getID();
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

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

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

    
578
    return mRotateEffect.getID();
579
    }
580

    
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  private float getAngle()
585
    {
586
    int pointNum = mRotationAngle.getNumPoints();
587

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

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

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

    
613
    mRotationAngle.removeAll();
614
    mRotationAngleStatic.set0(0);
615

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

    
624
///////////////////////////////////////////////////////////////////////////////////////////////////
625

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

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633

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

    
640
    point3D[0] *= multiplier;
641
    point3D[1] *= multiplier;
642
    point3D[2] *= multiplier;
643

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

    
654
    return currentBest;
655
    }
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

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

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

    
666
    if( tmp!=0 ) return NEAREST*tmp;
667

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

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
  public int getNodeSize()
674
    {
675
    return mNodeSize;
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
  public RubikObjectList getObjectList()
681
    {
682
    return mList;
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  abstract float getScreenRatio();
688
  abstract Static3D[] getCubitPositions(int size);
689
  abstract Static4D[] getQuats();
690
  abstract int getNumFaces();
691
  abstract int getNumStickerTypes();
692
  abstract int getNumCubitFaces();
693
  abstract MeshBase createCubitMesh(int cubit);
694
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
695
  abstract int getFaceColor(int cubit, int cubitface, int size);
696
  abstract float returnMultiplier();
697
  abstract float[] getRowChances();
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
  }
(9-9/12)