Project

General

Profile

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

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

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26 27a70eae Leszek Koltunski
27 27e6c301 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
28
29 27a70eae Leszek Koltunski
import org.distorted.library.effect.Effect;
30 470820a7 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
31 27a70eae Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33 10585385 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
34 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
35 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedNode;
37
import org.distorted.library.main.DistortedTexture;
38 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
39 470820a7 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
40 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
41 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
42 27e6c301 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
43 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46 4f9f99a2 Leszek Koltunski
47 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 27a70eae Leszek Koltunski
public abstract class RubikObject extends DistortedNode
50 fdec60a3 Leszek Koltunski
  {
51 8cccfb10 Leszek Koltunski
  private static final Static3D CENTER = new Static3D(0,0,0);
52 14bd7976 Leszek Koltunski
  static final int INTERIOR_COLOR = 0xff000000;
53 fb6a40c8 Leszek Koltunski
  public static final int NODE_FBO_SIZE = 600;
54 27e6c301 Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
55 7289fd6c Leszek Koltunski
  private static final int TEXTURE_HEIGHT = 128;
56 27e6c301 Leszek Koltunski
57 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
58 98904e45 Leszek Koltunski
  final Static4D[] QUATS;
59 470820a7 Leszek Koltunski
  final int NUM_FACES;
60 27a70eae Leszek Koltunski
61 f0fa83ae Leszek Koltunski
  static float OBJECT_SCREEN_RATIO;
62
63 a10ada2a Leszek Koltunski
  private final int NUM_CUBITS;
64 9224ffd2 Leszek Koltunski
  private int mRotRowBitmap;
65 efef689c Leszek Koltunski
  private int mRotAxis;
66 49f67f9b Leszek Koltunski
  private Static3D[] mOrigPos;
67 fb6a40c8 Leszek Koltunski
  private Static3D mNodeScale;
68 27a70eae Leszek Koltunski
  private Static4D mQuatAccumulated;
69 b32444ee Leszek Koltunski
  private Cubit[] mCubits;
70 49f67f9b Leszek Koltunski
  private int mSize;
71 aa171dee Leszek Koltunski
  private RubikObjectList mList;
72 470820a7 Leszek Koltunski
  private MeshBase mMesh;
73
  private DistortedEffects mEffects;
74 27e6c301 Leszek Koltunski
  private VertexEffectRotate mRotateEffect;
75
  private Dynamic1D mRotationAngle;
76
  private Static3D mRotationAxis;
77 27a70eae Leszek Koltunski
78 e844c116 Leszek Koltunski
  float mStart, mStep;
79 fdec60a3 Leszek Koltunski
80 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
81
  DistortedTexture mTexture;
82
83
  MatrixEffectScale mScaleEffect;
84
  MatrixEffectQuaternion mQuatCEffect;
85
  MatrixEffectQuaternion mQuatAEffect;
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88 fdec60a3 Leszek Koltunski
89 aa171dee Leszek Koltunski
  RubikObject(int size, int fov, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture,
90 a31d25de Leszek Koltunski
              MeshRectangles nodeMesh, DistortedEffects nodeEffects, int[][] moves, RubikObjectList list)
91 fdec60a3 Leszek Koltunski
    {
92 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
93 fdec60a3 Leszek Koltunski
94 ba740a0c Leszek Koltunski
    resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
95 d41742f7 Leszek Koltunski
96 aa171dee Leszek Koltunski
    mList = list;
97 49f67f9b Leszek Koltunski
    mOrigPos = getCubitPositions(size);
98 10a2e360 Leszek Koltunski
99 98904e45 Leszek Koltunski
    QUATS = getQuats();
100 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
101 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
102 f0fa83ae Leszek Koltunski
    OBJECT_SCREEN_RATIO = getScreenRatio();
103 470820a7 Leszek Koltunski
    NUM_FACES = getNumFaces();
104 a10ada2a Leszek Koltunski
105 27a70eae Leszek Koltunski
    mSize = size;
106 49f67f9b Leszek Koltunski
    computeStartAndStep(mOrigPos);
107 fb6a40c8 Leszek Koltunski
    mNodeScale= new Static3D(1,1,1);
108
    mQuatAccumulated = quatAcc;
109 e844c116 Leszek Koltunski
110 27e6c301 Leszek Koltunski
    mRotationAngle= new Dynamic1D();
111
    mRotationAxis = new Static3D(1,0,0);
112 8cccfb10 Leszek Koltunski
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
113 27e6c301 Leszek Koltunski
114 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
115
    mRotationAngleMiddle = new Static1D(0);
116
    mRotationAngleFinal  = new Static1D(0);
117
118 fb6a40c8 Leszek Koltunski
    float scale = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
119
    mScaleEffect = new MatrixEffectScale(new Static3D(scale,scale,scale));
120 8cccfb10 Leszek Koltunski
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, CENTER);
121
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, CENTER);
122 27a70eae Leszek Koltunski
123
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
124 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
125 a10ada2a Leszek Koltunski
126
    mCubits = new Cubit[NUM_CUBITS];
127 5974d2ae Leszek Koltunski
    mTexture = new DistortedTexture();
128 470820a7 Leszek Koltunski
    MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
129 a10ada2a Leszek Koltunski
130
    for(int i=0; i<NUM_CUBITS; i++)
131
      {
132 27e6c301 Leszek Koltunski
      mCubits[i] = new Cubit(this,mOrigPos[i]);
133 470820a7 Leszek Koltunski
      cubitMesh[i] = createCubitMesh(i);
134
      cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
135 10585385 Leszek Koltunski
      cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
136 a10ada2a Leszek Koltunski
      }
137 7381193e Leszek Koltunski
138 470820a7 Leszek Koltunski
    mMesh = new MeshJoined(cubitMesh);
139
140
    resetAllTextureMaps();
141
142
    mEffects = new DistortedEffects();
143 10585385 Leszek Koltunski
144 98904e45 Leszek Koltunski
    int num_quats = QUATS.length;
145 10585385 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
146
      {
147 98904e45 Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
148 10585385 Leszek Koltunski
      vq.setMeshAssociation(0,q);
149
      mEffects.apply(vq);
150
      }
151
152 27e6c301 Leszek Koltunski
    mEffects.apply(mRotateEffect);
153 470820a7 Leszek Koltunski
    mEffects.apply(mQuatAEffect);
154
    mEffects.apply(mQuatCEffect);
155
    mEffects.apply(mScaleEffect);
156
157
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
158
159 aa171dee Leszek Koltunski
    setupPosition(moves);
160
161 4888e97c Leszek Koltunski
    setProjection(fov, 0.1f);
162 27a70eae Leszek Koltunski
    }
163
164 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165 97d2f701 Leszek Koltunski
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
166
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
167
// consecutive).
168
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
169
// the Cube and the Pyraminx.
170
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
171
// given Cubit belongs to.
172 e844c116 Leszek Koltunski
173
  private void computeStartAndStep(Static3D[] pos)
174
    {
175
    float min = Float.MAX_VALUE;
176
    float max = Float.MIN_VALUE;
177
    float axisX = ROTATION_AXIS[0].get0();
178
    float axisY = ROTATION_AXIS[0].get1();
179
    float axisZ = ROTATION_AXIS[0].get2();
180
    float tmp;
181
182
    for(int i=0; i<NUM_CUBITS; i++)
183
      {
184
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
185
      if( tmp<min ) min=tmp;
186
      if( tmp>max ) max=tmp;
187
      }
188
189
    mStart = min;
190
    mStep  = (max-min+1.0f)/mSize;
191
    }
192
193 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195 9224ffd2 Leszek Koltunski
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
196 efef689c Leszek Koltunski
    {
197 9224ffd2 Leszek Koltunski
    int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
198
    return ((1<<cubitRow)&rowBitmap)!=0;
199 66cbdd21 Leszek Koltunski
    }
200
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
203
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
204
// away from the face of the Pyraminx shouldn't be textured.
205
206
  private boolean isOnFace( int cubit, int axis, int row)
207
    {
208
    final float MAX_ERROR = 0.0001f;
209
    float diff = mCubits[cubit].mRotationRow[axis] - row;
210
    return diff*diff < MAX_ERROR;
211 efef689c Leszek Koltunski
    }
212
213 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
214 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
215
// when looking towards the direction where the axis increases in values.
216 aa171dee Leszek Koltunski
217 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
218 aa171dee Leszek Koltunski
    {
219 a31d25de Leszek Koltunski
    Static3D axis = ROTATION_AXIS[axisIndex];
220
221
    while( angleInDegrees<0 ) angleInDegrees += 360;
222
    angleInDegrees %= 360;
223
    
224
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
225
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
226
227
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
228
    }
229
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232
  private void setupPosition(int[][] moves)
233
    {
234
    if( moves!=null )
235
      {
236
      Static4D quat;
237 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
238 a31d25de Leszek Koltunski
      int corr = (360/getBasicAngle());
239
240
      for(int[] move: moves)
241
        {
242
        axis     = move[0];
243
        rowBitmap= move[1];
244
        angle    = move[2]*corr;
245
        quat     = makeQuaternion(axis,angle);
246
247
        for(int j=0; j<NUM_CUBITS; j++)
248
          if( belongsToRotation(j,axis,rowBitmap) )
249
            {
250 98904e45 Leszek Koltunski
            index = mCubits[j].removeRotationNow(quat);
251
            mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
252 a31d25de Leszek Koltunski
            }
253
        }
254
      }
255 aa171dee Leszek Koltunski
    }
256
257 fa0f7a56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
  int getCubitFaceColorIndex(int cubit, int face)
260
    {
261 470820a7 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
262
    return (int)(texMap.get0() / texMap.get2());
263 fa0f7a56 Leszek Koltunski
    }
264
265 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
267
268
  void clampPos(Static3D pos)
269
    {
270
    float currError, minError = Float.MAX_VALUE;
271
    int minErrorIndex= -1;
272
    float x = pos.get0();
273
    float y = pos.get1();
274
    float z = pos.get2();
275
    float xo,yo,zo;
276
277
    for(int i=0; i<NUM_CUBITS; i++)
278
      {
279
      xo = mOrigPos[i].get0();
280
      yo = mOrigPos[i].get1();
281
      zo = mOrigPos[i].get2();
282
283
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
284
285
      if( currError<minError )
286
        {
287
        minError = currError;
288
        minErrorIndex = i;
289
        }
290
      }
291
292
    pos.set( mOrigPos[minErrorIndex] );
293
    }
294
295 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
296
// the getFaceColors + final black in a horizontal strip.
297
298
  public void createTexture()
299
    {
300
    Bitmap bitmap;
301
302
    Paint paint = new Paint();
303 470820a7 Leszek Koltunski
    bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
304 411c6285 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
305
306
    paint.setAntiAlias(true);
307
    paint.setTextAlign(Paint.Align.CENTER);
308
    paint.setStyle(Paint.Style.FILL);
309
310 14bd7976 Leszek Koltunski
    paint.setColor(INTERIOR_COLOR);
311 470820a7 Leszek Koltunski
    canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
312 411c6285 Leszek Koltunski
313 470820a7 Leszek Koltunski
    for(int i=0; i<NUM_FACES; i++)
314 411c6285 Leszek Koltunski
      {
315 ca292407 Leszek Koltunski
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
316 411c6285 Leszek Koltunski
      }
317
318
    mTexture.setTexture(bitmap);
319
    }
320
321 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
322
323 27a70eae Leszek Koltunski
  public int getSize()
324 fdec60a3 Leszek Koltunski
    {
325 27a70eae Leszek Koltunski
    return mSize;
326 fdec60a3 Leszek Koltunski
    }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
330 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
331 fdec60a3 Leszek Koltunski
    {
332 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
333 fdec60a3 Leszek Koltunski
    }
334
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336
337 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
338
      {
339
      return mQuatAccumulated;
340
      }
341
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
344 5ba13c05 Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
345 fdec60a3 Leszek Koltunski
    {
346 f0fa83ae Leszek Koltunski
    float factor = Math.min(scrWidth,scrHeight);
347 5ba13c05 Leszek Koltunski
    mNodeScale.set(factor,factor,factor);
348 fdec60a3 Leszek Koltunski
    }
349 27a70eae Leszek Koltunski
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
352 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
353
    {
354
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
355
    }
356 f16ff19d Leszek Koltunski
357 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358 27a70eae Leszek Koltunski
359 a10ada2a Leszek Koltunski
  public void restorePreferences(SharedPreferences preferences)
360
    {
361 2fcad75d Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
362
      {
363
      int index = mCubits[i].restorePreferences(preferences);
364
      mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
365
      }
366 a10ada2a Leszek Koltunski
    }
367 27a70eae Leszek Koltunski
368 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
369
370
  public void releaseResources()
371
    {
372
    mTexture.markForDeletion();
373
    }
374
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377
  public void apply(Effect effect, int position)
378
    {
379 8cccfb10 Leszek Koltunski
    mEffects.apply(effect, position);
380 a10ada2a Leszek Koltunski
    }
381
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
384
  public void remove(long effectID)
385
    {
386 8cccfb10 Leszek Koltunski
    mEffects.abortById(effectID);
387 a10ada2a Leszek Koltunski
    }
388 74686c71 Leszek Koltunski
389 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
390
391
  public void solve()
392
    {
393 98904e45 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
394
      {
395
      mCubits[i].solve();
396
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
397
      }
398 a10ada2a Leszek Koltunski
    }
399
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
402
  public boolean isSolved()
403
    {
404 2fcad75d Leszek Koltunski
    int index = mCubits[0].mQuatIndex;
405 a10ada2a Leszek Koltunski
406 94ad5a8f Leszek Koltunski
    for(int i=1; i<NUM_CUBITS; i++)
407 a10ada2a Leszek Koltunski
      {
408 2fcad75d Leszek Koltunski
      if( !mCubits[i].thereIsNoVisibleDifference(index) ) return false;
409 a10ada2a Leszek Koltunski
      }
410
411
    return true;
412
    }
413
414 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
415
416
  public void resetAllTextureMaps()
417
    {
418 ad73edd5 Leszek Koltunski
    boolean belongs;
419
    final float ratio = 1.0f/(NUM_FACES+1);
420
421
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
422 1f9772f3 Leszek Koltunski
      {
423 ad73edd5 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_FACES];
424
425
      if( 2*ROTATION_AXIS.length == NUM_FACES )  // i.e. there are faces on both ends of the axis (cube)
426
        {
427
        for(int i=0; i<NUM_FACES; i++)
428
          {
429
          belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
430
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
431
          }
432
        }
433
      else if( ROTATION_AXIS.length == NUM_FACES )  // just a single face on the right end of an axis (pyraminx)
434
        {
435
        for(int i=0; i<NUM_FACES; i++)
436
          {
437
          belongs = isOnFace(cubit, i, 0 );
438
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
439
          }
440
        }
441
442
      mMesh.setTextureMap(maps,NUM_FACES*cubit);
443 1f9772f3 Leszek Koltunski
      }
444
    }
445
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
448
  public void setTextureMap(int cubit, int face, int newColor)
449
    {
450 470820a7 Leszek Koltunski
    final float ratio = 1.0f/(NUM_FACES+1);
451
    final Static4D[] maps = new Static4D[NUM_FACES];
452 1f9772f3 Leszek Koltunski
453 d5032ac8 Leszek Koltunski
    try
454
      {
455
      maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
456 470820a7 Leszek Koltunski
      mMesh.setTextureMap(maps,NUM_FACES*cubit);
457 d5032ac8 Leszek Koltunski
      }
458
    catch(ArrayIndexOutOfBoundsException ignored)
459
      {
460
      // the object must have changed in between of us touching the screen and getting here
461
      // (which happens after the next render, i.e. about 30 miliseconds later)
462
      // ignore.
463
      }
464 1f9772f3 Leszek Koltunski
    }
465
466 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
467
468 efef689c Leszek Koltunski
  public void beginNewRotation(int axis, int row )
469 a10ada2a Leszek Koltunski
    {
470 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
471
      {
472
      android.util.Log.e("object", "invalid rotation axis: "+axis);
473
      return;
474
      }
475
    if( row<0 || row>=mSize )
476
      {
477
      android.util.Log.e("object", "invalid rotation row: "+row);
478
      return;
479
      }
480
481 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
482
    mRotRowBitmap= (1<<row);
483 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
484 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
485
    mRotationAngle.add(mRotationAngleStatic);
486 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
487 27e6c301 Leszek Koltunski
    }
488 a10ada2a Leszek Koltunski
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490
491 9224ffd2 Leszek Koltunski
  public long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
492 27e6c301 Leszek Koltunski
    {
493
    mRotAxis     = axis;
494
    mRotRowBitmap= rowBitmap;
495 a10ada2a Leszek Koltunski
496 27e6c301 Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
497
    mRotationAxis.set( ROTATION_AXIS[axis] );
498
    mRotationAngle.setDuration(durationMillis);
499
    mRotationAngle.resetToBeginning();
500
    mRotationAngle.add(new Static1D(0));
501
    mRotationAngle.add(new Static1D(angle));
502 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
503 27e6c301 Leszek Koltunski
    mRotateEffect.notifyWhenFinished(listener);
504
505
    return mRotateEffect.getID();
506
    }
507 a10ada2a Leszek Koltunski
508 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
509 a10ada2a Leszek Koltunski
510 27e6c301 Leszek Koltunski
  public long finishRotationNow(EffectListener listener)
511
    {
512
    float angle = getAngle();
513
    int nearestAngleInDegrees = computeNearestAngle(angle);
514
    mRotationAngleStatic.set0(angle);
515
    mRotationAngleFinal.set0(nearestAngleInDegrees);
516
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
517
518
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
519
    mRotationAngle.resetToBeginning();
520
    mRotationAngle.removeAll();
521
    mRotationAngle.add(mRotationAngleStatic);
522
    mRotationAngle.add(mRotationAngleMiddle);
523
    mRotationAngle.add(mRotationAngleFinal);
524
    mRotateEffect.notifyWhenFinished(listener);
525
526
    return mRotateEffect.getID();
527
    }
528 001cc0e4 Leszek Koltunski
529 a10ada2a Leszek Koltunski
530 001cc0e4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
531
532 27e6c301 Leszek Koltunski
  private float getAngle()
533 001cc0e4 Leszek Koltunski
    {
534 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
535 001cc0e4 Leszek Koltunski
536 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
537 001cc0e4 Leszek Koltunski
      {
538 27e6c301 Leszek Koltunski
      return mRotationAngle.getPoint(pointNum-1).get0();
539
      }
540
    else
541
      {
542
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
543
      crashlytics.log("points in RotationAngle: "+pointNum);
544
      return 0;
545 001cc0e4 Leszek Koltunski
      }
546
    }
547
548 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
549
550
  public void removeRotationNow()
551
     {
552 27e6c301 Leszek Koltunski
     float angle = getAngle();
553
     int nearestAngleInDegrees = computeNearestAngle(angle);
554
     double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
555
     float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
556
     float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
557
     float axisX = ROTATION_AXIS[mRotAxis].get0();
558
     float axisY = ROTATION_AXIS[mRotAxis].get1();
559
     float axisZ = ROTATION_AXIS[mRotAxis].get2();
560
     Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
561
562
     mRotationAngle.removeAll();
563
     mRotationAngleStatic.set0(0);
564 a10ada2a Leszek Koltunski
565
     for(int i=0; i<NUM_CUBITS; i++)
566 9224ffd2 Leszek Koltunski
       if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
567 a10ada2a Leszek Koltunski
         {
568 98904e45 Leszek Koltunski
         int index = mCubits[i].removeRotationNow(quat);
569
         mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
570 a10ada2a Leszek Koltunski
         }
571
     }
572
573 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
574
575 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
576 aa171dee Leszek Koltunski
    {
577
    solve();
578
    setupPosition(moves);
579
    }
580
581 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
582
583
  public int getCubit(float[] point3D)
584
    {
585
    float dist, minDist = Float. MAX_VALUE;
586
    int currentBest=-1;
587
    float multiplier = returnMultiplier();
588
589
    point3D[0] *= multiplier;
590
    point3D[1] *= multiplier;
591
    point3D[2] *= multiplier;
592
593
    for(int i=0; i<NUM_CUBITS; i++)
594
      {
595
      dist = mCubits[i].getDistSquared(point3D);
596
      if( dist<minDist )
597
        {
598
        minDist = dist;
599
        currentBest = i;
600
        }
601
      }
602
603
    return currentBest;
604
    }
605
606 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
607
608
  public int computeNearestAngle(float angle)
609
    {
610
    final int NEAREST = 360/getBasicAngle();
611
612
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
613
    if( angle< -(NEAREST*0.5) ) tmp-=1;
614
615
    return NEAREST*tmp;
616
    }
617
618 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
619
620
  public RubikObjectList getObjectList()
621
    {
622
    return mList;
623
    }
624
625 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
626
627 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
628 10a2e360 Leszek Koltunski
  abstract Static3D[] getCubitPositions(int size);
629 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
630 411c6285 Leszek Koltunski
  abstract int getNumFaces();
631 14bd7976 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit);
632 7289fd6c Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
633 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
634 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
635 9621255f Leszek Koltunski
  public abstract float returnMultiplier();
636 e46e17fb Leszek Koltunski
  public abstract float returnRotationFactor(float offset);
637 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
638 5cf34c5f Leszek Koltunski
  public abstract float[] getRowChances();
639 fdec60a3 Leszek Koltunski
  }