Project

General

Profile

« Previous | Next » 

Revision e6cf7283

Added by Leszek Koltunski about 3 years ago

Change the Cubit center from a Static3D to a float[].
The point: now we can have more than one center, and bandaged objects need more than one, because in this way they are going to fill up their RotationRow bitmaps.

View differences:

src/main/java/org/distorted/objects/TwistyObject.java
98 98

  
99 99
  private static float mInitScreenRatio;
100 100
  private static float mObjectScreenRatio = 1.0f;
101
  private static final float[] mTmp1 = new float[4];
102
  private static final float[] mTmp2 = new float[4];
101 103

  
102 104
  private final int mNodeSize;
103
  private final Static3D[] mOrigPos;
105
  private final float[][] mOrigPos;
104 106
  private final Static3D mNodeScale;
105 107
  private final Static4D mQuat;
106 108
  private final int mNumLayers, mRealSize;
......
214 216
    setProjection( fov, 0.1f);
215 217
    }
216 218

  
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

  
221
  private Static3D getPos(float[] origPos)
222
    {
223
    int len = origPos.length/3;
224
    float sumX = 0.0f;
225
    float sumY = 0.0f;
226
    float sumZ = 0.0f;
227

  
228
    for(int i=0; i<len; i++)
229
      {
230
      sumX += origPos[3*i  ];
231
      sumY += origPos[3*i+1];
232
      sumZ += origPos[3*i+2];
233
      }
234

  
235
    sumX /= len;
236
    sumY /= len;
237
    sumZ /= len;
238

  
239
    return new Static3D(sumX,sumY,sumZ);
240
    }
241

  
217 242
///////////////////////////////////////////////////////////////////////////////////////////////////
218 243

  
219 244
  private void createMeshAndCubits(ObjectList list, Resources res)
......
252 277
        {
253 278
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
254 279
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
255
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
280
        Static3D pos = getPos(mOrigPos[i]);
281
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
256 282
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
257 283
        }
258 284

  
......
283 309

  
284 310
///////////////////////////////////////////////////////////////////////////////////////////////////
285 311

  
286
  int computeRow(float x, float y, float z, int rotIndex)
312
  int computeRow(float[] pos, int rotIndex)
287 313
    {
314
    int ret=0;
315
    int len = pos.length / 3;
288 316
    Static3D axis = ROTATION_AXIS[rotIndex];
289
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
317
    float axisX = axis.get0();
318
    float axisY = axis.get1();
319
    float axisZ = axis.get2();
320

  
321
    for(int i=0; i<len; i++)
322
      {
323
      ret |= computeRow(pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ);
324
      }
325

  
326
    return ret;
327
    }
328

  
329
///////////////////////////////////////////////////////////////////////////////////////////////////
290 330

  
331
  private int computeRow(float casted)
332
    {
291 333
    for(int i=0; i<NUM_CUTS; i++)
292 334
      {
293
      if( tmp<CUTS[i] ) return (1<<i);
335
      if( casted<CUTS[i] ) return (1<<i);
294 336
      }
295 337

  
296 338
    return (1<<NUM_CUTS);
......
369 411
///////////////////////////////////////////////////////////////////////////////////////////////////
370 412
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
371 413

  
372
  void clampPos(Static3D pos)
414
  void clampPos(float[] pos, int offset)
373 415
    {
374 416
    float currError, minError = Float.MAX_VALUE;
375
    int minErrorIndex= -1;
376
    float x = pos.get0();
377
    float y = pos.get1();
378
    float z = pos.get2();
417
    int minErrorIndex1 = -1;
418
    int minErrorIndex2 = -1;
419

  
420
    float x = pos[offset  ];
421
    float y = pos[offset+1];
422
    float z = pos[offset+2];
423

  
379 424
    float xo,yo,zo;
380 425

  
381 426
    for(int i=0; i<NUM_CUBITS; i++)
382 427
      {
383
      xo = mOrigPos[i].get0();
384
      yo = mOrigPos[i].get1();
385
      zo = mOrigPos[i].get2();
428
      int len = mOrigPos[i].length / 3;
386 429

  
387
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
388

  
389
      if( currError<minError )
430
      for(int j=0; j<len; j++)
390 431
        {
391
        minError = currError;
392
        minErrorIndex = i;
432
        xo = mOrigPos[i][3*j  ];
433
        yo = mOrigPos[i][3*j+1];
434
        zo = mOrigPos[i][3*j+2];
435

  
436
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
437

  
438
        if( currError<minError )
439
          {
440
          minError = currError;
441
          minErrorIndex1 = i;
442
          minErrorIndex2 = j;
443
          }
393 444
        }
394 445
      }
395 446

  
396
    pos.set( mOrigPos[minErrorIndex] );
447
    pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
448
    pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
449
    pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
397 450
    }
398 451

  
399 452
///////////////////////////////////////////////////////////////////////////////////////////////////
......
422 475
      {
423 476
      case 0 : return false;  // 'inside' cubit that does not lie on any face
424 477
      case 1 :                // cubit that lies inside one of the faces
425
               Static3D orig = cubit.getOrigPosition();
478
               float[] orig   = cubit.getOrigPosition();
426 479
               Static4D quat1 = QUATS[quatIndex];
427 480
               Static4D quat2 = QUATS[cubit.mQuatIndex];
428 481

  
429
               Static4D cubitCenter = new Static4D( orig.get0(), orig.get1(), orig.get2(), 0);
430
               Static4D rotated1 = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat1 );
482
               Static4D cubitCenter = new Static4D( orig[0], orig[1], orig[2], 0);              // not used for bandaged objects,
483
               Static4D rotated1 = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat1 );   // only check the first position
431 484
               Static4D rotated2 = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat2 );
432 485

  
433
               int row1, row2;
434
               float x1 = rotated1.get0();
435
               float y1 = rotated1.get1();
436
               float z1 = rotated1.get2();
437
               float x2 = rotated2.get0();
438
               float y2 = rotated2.get1();
439
               float z2 = rotated2.get2();
486
               rotated1.get(mTmp1, 0, 0, 0);
487
               rotated2.get(mTmp2, 0, 0, 0);
440 488

  
441 489
               for(int i=0; i<NUM_AXIS; i++)
442 490
                 {
443
                 row1 = computeRow(x1,y1,z1,i);
444
                 row2 = computeRow(x2,y2,z2,i);
445

  
446
                 if( ((row1 & row2) & bitmap) != 0 ) return false;
491
                 if( (computeRow(mTmp1,i) & computeRow(mTmp2,i) & bitmap) != 0 ) return false;
447 492
                 }
448 493
               return true;
449 494

  
......
843 888
///////////////////////////////////////////////////////////////////////////////////////////////////
844 889

  
845 890
  abstract float getScreenRatio();
846
  abstract Static3D[] getCubitPositions(int numLayers);
891
  abstract float[][] getCubitPositions(int numLayers);
847 892
  abstract Static4D[] getQuats();
848 893
  abstract int getNumFaces();
849 894
  abstract int getNumStickerTypes(int numLayers);

Also available in: Unified diff