Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 418aa554

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
public abstract class RubikObject extends DistortedNode
56
  {
57
  private static final float NODE_RATIO = 1.32f;
58
  private static final float MAX_SIZE_CHANGE = 1.3f;
59
  private static final float MIN_SIZE_CHANGE = 0.8f;
60

    
61
  private static boolean mCreateFromDMesh = true;
62

    
63
  private static final Static3D CENTER = new Static3D(0,0,0);
64
  static final int INTERIOR_COLOR = 0xff000000;
65
  private static final int POST_ROTATION_MILLISEC = 500;
66
  private static final int TEXTURE_HEIGHT = 128;
67

    
68
  final Static3D[] ROTATION_AXIS;
69
  final Static4D[] QUATS;
70
  final int NUM_FACES;
71

    
72
  private static float mInitScreenRatio,mObjectScreenRatio;
73

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

    
91
  float mStart, mStep;
92

    
93
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
94
  DistortedTexture mTexture;
95

    
96
  MatrixEffectScale mScaleEffect;
97
  MatrixEffectQuaternion mQuatEffect;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  RubikObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshRectangles nodeMesh,
102
              DistortedEffects nodeEffects, int[][] moves, RubikObjectList list, Resources res, int screenWidth)
103
    {
104
    super(nodeTexture,nodeEffects,nodeMesh);
105

    
106
    mNodeSize = screenWidth;
107

    
108
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
109

    
110
    mList = list;
111
    mOrigPos = getCubitPositions(size);
112

    
113
    QUATS = getQuats();
114
    NUM_CUBITS  = mOrigPos.length;
115
    ROTATION_AXIS = getRotationAxis();
116
    mObjectScreenRatio = getScreenRatio();
117
    mInitScreenRatio = mObjectScreenRatio;
118
    NUM_FACES = getNumFaces();
119

    
120
    mSize = size;
121
    computeStartAndStep(mOrigPos);
122
    mNodeScale= new Static3D(1,NODE_RATIO,1);
123
    mQuat = quat;
124

    
125
    mRotationAngle= new Dynamic1D();
126
    mRotationAxis = new Static3D(1,0,0);
127
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
128

    
129
    mRotationAngleStatic = new Static1D(0);
130
    mRotationAngleMiddle = new Static1D(0);
131
    mRotationAngleFinal  = new Static1D(0);
132

    
133
    float scale  = mObjectScreenRatio*mNodeSize/mSize;
134
    mObjectScale = new Static3D(scale,scale,scale);
135
    mScaleEffect = new MatrixEffectScale(mObjectScale);
136
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
137

    
138
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
139
    nodeEffects.apply(nodeScaleEffect);
140

    
141
    createMeshAndCubits(list,res);
142

    
143
    mTexture = new DistortedTexture();
144
    mEffects = new DistortedEffects();
145

    
146
    int num_quats = QUATS.length;
147
    for(int q=0; q<num_quats; q++)
148
      {
149
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
150
      vq.setMeshAssociation(0,q);
151
      mEffects.apply(vq);
152
      }
153

    
154
    mEffects.apply(mRotateEffect);
155
    mEffects.apply(mQuatEffect);
156
    mEffects.apply(mScaleEffect);
157

    
158
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
159

    
160
    setupPosition(moves);
161

    
162
    setProjection(fov, 0.1f);
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private void createMeshAndCubits(RubikObjectList list, Resources res)
168
    {
169
    mCubits = new Cubit[NUM_CUBITS];
170

    
171
    if( mCreateFromDMesh )
172
      {
173
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
174
      int resourceID= list.getResourceIDs()[sizeIndex];
175

    
176
      InputStream is = res.openRawResource(resourceID);
177
      DataInputStream dos = new DataInputStream(is);
178
      mMesh = new MeshFile(dos);
179

    
180
      try
181
        {
182
        is.close();
183
        }
184
      catch(IOException e)
185
        {
186
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
187
        }
188

    
189
      for(int i=0; i<NUM_CUBITS; i++)
190
        {
191
        mCubits[i] = new Cubit(this,mOrigPos[i]);
192
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
193
        }
194
      }
195
    else
196
      {
197
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
198

    
199
      for(int i=0; i<NUM_CUBITS; i++)
200
        {
201
        mCubits[i] = new Cubit(this,mOrigPos[i]);
202
        cubitMesh[i] = createCubitMesh(i);
203
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
204
        cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
205
        }
206

    
207
      mMesh = new MeshJoined(cubitMesh);
208
      resetAllTextureMaps();
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public void setObjectRatio(float sizeChange)
215
    {
216
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
217

    
218
    if( mObjectScreenRatio > MAX_SIZE_CHANGE*mInitScreenRatio)
219
      {
220
      mObjectScreenRatio = MAX_SIZE_CHANGE*mInitScreenRatio;
221
      }
222
    if( mObjectScreenRatio < MIN_SIZE_CHANGE*mInitScreenRatio)
223
      {
224
      mObjectScreenRatio = MIN_SIZE_CHANGE*mInitScreenRatio;
225
      }
226

    
227
    float scale = mObjectScreenRatio*mNodeSize/mSize;
228
    mObjectScale.set(scale,scale,scale);
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  static float getObjectRatio()
234
    {
235
    return mObjectScreenRatio;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
240
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
241
// consecutive).
242
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
243
// the Cube and the Pyraminx.
244
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
245
// given Cubit belongs to.
246

    
247
  private void computeStartAndStep(Static3D[] pos)
248
    {
249
    float min = Float.MAX_VALUE;
250
    float max = Float.MIN_VALUE;
251
    float axisX = ROTATION_AXIS[0].get0();
252
    float axisY = ROTATION_AXIS[0].get1();
253
    float axisZ = ROTATION_AXIS[0].get2();
254
    float tmp;
255

    
256
    for(int i=0; i<NUM_CUBITS; i++)
257
      {
258
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
259
      if( tmp<min ) min=tmp;
260
      if( tmp>max ) max=tmp;
261
      }
262

    
263
    mStart = min;
264
    mStep  = (max-min+1.0f)/mSize;
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
270
    {
271
    int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
272
    return ((1<<cubitRow)&rowBitmap)!=0;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
277
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
278
// away from the face of the Pyraminx shouldn't be textured.
279

    
280
  private boolean isOnFace( int cubit, int axis, int row)
281
    {
282
    final float MAX_ERROR = 0.0001f;
283
    float diff = mCubits[cubit].mRotationRow[axis] - row;
284
    return diff*diff < MAX_ERROR;
285
    }
286

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

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

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

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

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

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

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

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

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

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

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

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

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

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

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

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

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

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

    
384
    paint.setColor(INTERIOR_COLOR);
385
    canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
386

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

    
392
    mTexture.setTexture(bitmap);
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public int getSize()
398
    {
399
    return mSize;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

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

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
419
    {
420
    float factor = Math.min(scrWidth,scrHeight);
421
    mNodeScale.set(factor,NODE_RATIO*factor,factor);
422
    }
423

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

    
426
  public void savePreferences(SharedPreferences.Editor editor)
427
    {
428
    for(int i=0; i<NUM_CUBITS; i++) mCubits[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 = mCubits[i].restorePreferences(preferences);
438
      mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
439
      }
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  public void releaseResources()
445
    {
446
    mTexture.markForDeletion();
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public void apply(Effect effect, int position)
452
    {
453
    mEffects.apply(effect, position);
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  public void remove(long effectID)
459
    {
460
    mEffects.abortById(effectID);
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  public synchronized void solve()
466
    {
467
    for(int i=0; i<NUM_CUBITS; i++)
468
      {
469
      mCubits[i].solve();
470
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
471
      }
472
    }
473

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

    
476
  public boolean isSolved()
477
    {
478
    int index = mCubits[0].mQuatIndex;
479

    
480
    for(int i=1; i<NUM_CUBITS; i++)
481
      {
482
      if( !mCubits[i].thereIsNoVisibleDifference(index) ) return false;
483
      }
484

    
485
    return true;
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public void resetAllTextureMaps()
491
    {
492
    boolean belongs;
493
    final float ratio = 1.0f/(NUM_FACES+1);
494

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

    
499
      if( 2*ROTATION_AXIS.length == NUM_FACES )  // i.e. there are faces on both ends of the axis (cube)
500
        {
501
        for(int i=0; i<NUM_FACES; i++)
502
          {
503
          belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
504
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
505
          }
506
        }
507
      else if( ROTATION_AXIS.length == NUM_FACES )  // just a single face on the right end of an axis (pyraminx)
508
        {
509
        for(int i=0; i<NUM_FACES; i++)
510
          {
511
          belongs = isOnFace(cubit, i, 0 );
512
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
513
          }
514
        }
515

    
516
      mMesh.setTextureMap(maps,NUM_FACES*cubit);
517
      }
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

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

    
527
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
528
    mMesh.setTextureMap(maps,NUM_FACES*cubit);
529
    }
530

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

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

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

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
557
    {
558
    mRotAxis     = axis;
559
    mRotRowBitmap= rowBitmap;
560

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

    
570
    return mRotateEffect.getID();
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

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

    
582
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
583
    mRotationAngle.resetToBeginning();
584
    mRotationAngle.removeAll();
585
    mRotationAngle.add(mRotationAngleStatic);
586
    mRotationAngle.add(mRotationAngleMiddle);
587
    mRotationAngle.add(mRotationAngleFinal);
588
    mRotateEffect.notifyWhenFinished(listener);
589

    
590
    return mRotateEffect.getID();
591
    }
592

    
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595

    
596
  private float getAngle()
597
    {
598
    int pointNum = mRotationAngle.getNumPoints();
599

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

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

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

    
625
    mRotationAngle.removeAll();
626
    mRotationAngleStatic.set0(0);
627

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

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  public void initializeObject(int[][] moves)
639
    {
640
    solve();
641
    setupPosition(moves);
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

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

    
652
    point3D[0] *= multiplier;
653
    point3D[1] *= multiplier;
654
    point3D[2] *= multiplier;
655

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

    
666
    return currentBest;
667
    }
668

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

    
671
  public int computeNearestAngle(float angle, float speed)
672
    {
673
    final int NEAREST = 360/getBasicAngle();
674

    
675
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
676
    if( angle< -(NEAREST*0.5) ) tmp-=1;
677

    
678
    if( tmp!=0 ) return NEAREST*tmp;
679

    
680
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
681
    }
682

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

    
685
  public int getNodeSize()
686
    {
687
    return mNodeSize;
688
    }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
  public RubikObjectList getObjectList()
693
    {
694
    return mList;
695
    }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
  abstract float getScreenRatio();
700
  abstract Static3D[] getCubitPositions(int size);
701
  abstract Static4D[] getQuats();
702
  abstract int getNumFaces();
703
  abstract MeshBase createCubitMesh(int cubit);
704
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
705
  public abstract Static3D[] getRotationAxis();
706
  public abstract int getBasicAngle();
707
  public abstract float returnMultiplier();
708
  public abstract float returnRotationFactor(float offset);
709
  public abstract String retObjectString();
710
  public abstract float[] getRowChances();
711
  }
(6-6/10)