Project

General

Profile

« Previous | Next » 

Revision 4c864c68

Added by Leszek Koltunski over 4 years ago

Further improve rotations of a Object layer - make them independent of physical screen size (now it depends on the angle of rotation and, if that's 0, on the speed (in inches of second) of the finger swipe done by the user.

View differences:

src/main/java/org/distorted/main/RubikSurfaceView.java
79 79
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
80 80
    private int mCurrentAxis, mCurrentRow;
81 81
    private float mCurrentAngle, mCurrRotSpeed;
82
    private float[] mLastAngles;
83
    private long[] mLastTimestamps;
82
    private float[] mLastX;
83
    private float[] mLastY;
84
    private long[] mLastT;
84 85
    private int mFirstIndex, mLastIndex;
85 86
    private int mDensity;
86 87

  
......
271 272

  
272 273
///////////////////////////////////////////////////////////////////////////////////////////////////
273 274

  
274
    private void addSpeedProbe(float angle)
275
    private void addSpeedProbe(float x, float y)
275 276
      {
276 277
      long currTime = System.currentTimeMillis();
277 278
      boolean theSame = mLastIndex==mFirstIndex;
......
279 280
      mLastIndex++;
280 281
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
281 282

  
282
      mLastTimestamps[mLastIndex] = currTime;
283
      mLastAngles[mLastIndex] = angle;
283
      mLastT[mLastIndex] = currTime;
284
      mLastX[mLastIndex] = x;
285
      mLastY[mLastIndex] = y;
284 286

  
285 287
      if( mLastIndex==mFirstIndex)
286 288
        {
......
290 292

  
291 293
      if( theSame )
292 294
        {
293
        mLastTimestamps[mFirstIndex] = currTime;
294
        mLastAngles[mFirstIndex] = angle;
295
        mLastT[mFirstIndex] = currTime;
296
        mLastX[mFirstIndex] = x;
297
        mLastY[mFirstIndex] = y;
295 298
        }
296 299
      }
297 300

  
298 301
///////////////////////////////////////////////////////////////////////////////////////////////////
299 302

  
300
    private void computeCurrentSpeed()
303
    private void computeCurrentSpeedInInchesPerSecond()
301 304
      {
302
      long firstTime = mLastTimestamps[mFirstIndex];
303
      long lastTime  = mLastTimestamps[mLastIndex];
304
      float firstAngle = mLastAngles[mFirstIndex];
305
      float lastAngle  = mLastAngles[mLastIndex];
305
      long firstTime = mLastT[mFirstIndex];
306
      long lastTime  = mLastT[mLastIndex];
307
      float fX = mLastX[mFirstIndex];
308
      float fY = mLastY[mFirstIndex];
309
      float lX = mLastX[mLastIndex];
310
      float lY = mLastY[mLastIndex];
306 311

  
307 312
      long timeDiff = lastTime-firstTime;
308 313

  
309 314
      mLastIndex = 0;
310 315
      mFirstIndex= 0;
311 316

  
312
      mCurrRotSpeed = timeDiff>0 ? (lastAngle-firstAngle)/timeDiff : 0;
317
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
313 318
      }
314 319

  
315 320
///////////////////////////////////////////////////////////////////////////////////////////////////
......
427 432
              });
428 433
            }
429 434

  
430
          addSpeedProbe(0.0f);
435
          addSpeedProbe(x,y);
431 436

  
432 437
          mBeginningRotation = false;
433 438
          mContinuingRotation= true;
......
439 444
        mCurrentAngle = SWIPING_SENSITIVITY*angle;
440 445
        mPreRender.getObject().continueRotation(mCurrentAngle);
441 446

  
442
        addSpeedProbe(mCurrentAngle);
447
        addSpeedProbe(x,y);
443 448
        }
444 449
      else if( mDragging )
445 450
        {
......
476 481

  
477 482
      if( mContinuingRotation )
478 483
        {
479
        computeCurrentSpeed();
484
        computeCurrentSpeedInInchesPerSecond();
480 485
        int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
481 486
        mPreRender.finishRotation(angle);
482 487

  
......
506 511
        mLastCubitColor = -1;
507 512
        mCurrRotSpeed   = 0.0f;
508 513

  
509
        mLastAngles = new float[NUM_SPEED_PROBES];
510
        mLastTimestamps = new long[NUM_SPEED_PROBES];
514
        mLastX = new float[NUM_SPEED_PROBES];
515
        mLastY = new float[NUM_SPEED_PROBES];
516
        mLastT = new long[NUM_SPEED_PROBES];
511 517
        mFirstIndex =0;
512 518
        mLastIndex  =0;
513 519

  
src/main/java/org/distorted/objects/RubikObject.java
620 620
    {
621 621
    final int NEAREST = 360/getBasicAngle();
622 622

  
623
    float angleAndSpeed = angle + 100*speed;
623
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
624
    if( angle< -(NEAREST*0.5) ) tmp-=1;
624 625

  
625
    int tmp1 = (int)((angle+NEAREST/2)/NEAREST);
626
    if( angle< -(NEAREST*0.5) ) tmp1-=1;
626
    if( tmp!=0 ) return NEAREST*tmp;
627 627

  
628
    int tmp2 = (int)((angleAndSpeed+NEAREST/2)/NEAREST);
629
    if( angleAndSpeed< -(NEAREST*0.5) ) tmp2-=1;
630

  
631
    return NEAREST*(tmp1==0 ? tmp2 : tmp1);
628
    return speed> 1.5f ? NEAREST*(angle>0 ? 1:-1) : 0;
632 629
    }
633 630

  
634 631
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff