Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 44f95b2a

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
  final float BASIC_STEP;
78

    
79
  private static float mInitScreenRatio,mObjectScreenRatio;
80

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

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

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

    
102
  MatrixEffectScale mScaleEffect;
103
  MatrixEffectQuaternion mQuatEffect;
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
112
    mNodeSize = screenWidth;
113

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

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

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

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

    
135
    mRowChances = getRowChances();
136

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

    
141
    mRotationAngleStatic = new Static1D(0);
142
    mRotationAngleMiddle = new Static1D(0);
143
    mRotationAngleFinal  = new Static1D(0);
144

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

    
150
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
151
    nodeEffects.apply(nodeScaleEffect);
152

    
153
    CUBITS = new Cubit[NUM_CUBITS];
154
    createMeshAndCubits(list,res);
155

    
156
    mTexture = new DistortedTexture();
157
    mEffects = new DistortedEffects();
158

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

    
167
    mEffects.apply(mRotateEffect);
168
    mEffects.apply(mQuatEffect);
169
    mEffects.apply(mScaleEffect);
170

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

    
175
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
176

    
177
    setupPosition(moves);
178

    
179
    setProjection(fov, 0.1f);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

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

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

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

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

    
222
      mMesh = new MeshJoined(cubitMesh);
223
      resetAllTextureMaps();
224
      }
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

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

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  static float getObjectRatio()
249
    {
250
    return mObjectScreenRatio;
251
    }
252

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

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

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

    
278
    mStart = min;
279
    mStep  = (max-min+BASIC_STEP)/mSize;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

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

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

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

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

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

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

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
336
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
337
// away from the face of the Pyraminx shouldn't be textured.
338

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

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

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

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

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

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

    
372
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
373

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

    
381
    pos.set( mOrigPos[minErrorIndex] );
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
// the getFaceColors + final black in a horizontal strip.
386

    
387
  public void createTexture()
388
    {
389
    Bitmap bitmap;
390

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

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

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

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

    
407
    mTexture.setTexture(bitmap);
408
    }
409

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

    
412
  public int getSize()
413
    {
414
    return mSize;
415
    }
416

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

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

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

    
426
  public Static4D getRotationQuat()
427
      {
428
      return mQuat;
429
      }
430

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

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

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

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

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

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

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

    
459
  public void releaseResources()
460
    {
461
    mTexture.markForDeletion();
462
    }
463

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

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

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

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

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

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

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

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

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

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

    
506
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
507
      }
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

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

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

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

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

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

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

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

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

    
560
    return mRotateEffect.getID();
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

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

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

    
580
    return mRotateEffect.getID();
581
    }
582

    
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
  private float getAngle()
587
    {
588
    int pointNum = mRotationAngle.getNumPoints();
589

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

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

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

    
615
    mRotationAngle.removeAll();
616
    mRotationAngleStatic.set0(0);
617

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

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

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

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635

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

    
642
    point3D[0] *= multiplier;
643
    point3D[1] *= multiplier;
644
    point3D[2] *= multiplier;
645

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

    
656
    return currentBest;
657
    }
658

    
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660

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

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

    
668
    if( tmp!=0 ) return NEAREST*tmp;
669

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

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

    
675
  public int getNodeSize()
676
    {
677
    return mNodeSize;
678
    }
679

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

    
682
  public RubikObjectList getObjectList()
683
    {
684
    return mList;
685
    }
686

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

    
689
  abstract float getScreenRatio();
690
  abstract Static3D[] getCubitPositions(int size);
691
  abstract Static4D[] getQuats();
692
  abstract int getNumFaces();
693
  abstract int getNumStickerTypes();
694
  abstract int getNumCubitFaces();
695
  abstract MeshBase createCubitMesh(int cubit);
696
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
697
  abstract int getFaceColor(int cubit, int cubitface, int size);
698
  abstract float returnMultiplier();
699
  abstract float[] getRowChances();
700
  abstract float getBasicStep();
701

    
702
  public abstract boolean isSolved();
703
  public abstract Static3D[] getRotationAxis();
704
  public abstract int getBasicAngle();
705
  public abstract int computeRowFromOffset(float offset);
706
  public abstract float returnRotationFactor(float offset);
707
  public abstract String retObjectString();
708
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
709
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
710
  }
(11-11/14)