Revision 168b6b56
Added by Leszek Koltunski almost 4 years ago
src/main/java/org/distorted/main/RubikSurfaceView.java | ||
---|---|---|
40 | 40 |
|
41 | 41 |
public class RubikSurfaceView extends GLSurfaceView |
42 | 42 |
{ |
43 |
private static final int NUM_SPEED_PROBES = 10; |
|
44 |
|
|
43 | 45 |
public static final int MODE_ROTATE = 0; |
44 | 46 |
public static final int MODE_DRAG = 1; |
45 | 47 |
public static final int MODE_REPLACE = 2; |
... | ... | |
75 | 77 |
private float mRotationFactor; |
76 | 78 |
private int mLastCubitColor, mLastCubitFace, mLastCubit; |
77 | 79 |
private int mCurrentAxis, mCurrentRow; |
78 |
private float mCurrentAngle; |
|
80 |
private float mCurrentAngle, mCurrRotSpeed; |
|
81 |
private float[] mLastAngles; |
|
82 |
private long[] mLastTimestamps; |
|
83 |
private int mFirstIndex, mLastIndex; |
|
79 | 84 |
|
80 | 85 |
private static Static4D mQuatCurrent = new Static4D(0,0,0,1); |
81 | 86 |
private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); |
... | ... | |
315 | 320 |
return quatMultiply(tmp,quat); |
316 | 321 |
} |
317 | 322 |
|
323 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
324 |
|
|
325 |
private void addSpeedProbe(float angle) |
|
326 |
{ |
|
327 |
long currTime = System.currentTimeMillis(); |
|
328 |
boolean theSame = mLastIndex==mFirstIndex; |
|
329 |
|
|
330 |
mLastIndex++; |
|
331 |
if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0; |
|
332 |
|
|
333 |
mLastTimestamps[mLastIndex] = currTime; |
|
334 |
mLastAngles[mLastIndex] = angle; |
|
335 |
|
|
336 |
if( mLastIndex==mFirstIndex) |
|
337 |
{ |
|
338 |
mFirstIndex++; |
|
339 |
if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0; |
|
340 |
} |
|
341 |
|
|
342 |
if( theSame ) |
|
343 |
{ |
|
344 |
mLastTimestamps[mFirstIndex] = currTime; |
|
345 |
mLastAngles[mFirstIndex] = angle; |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
350 |
|
|
351 |
private void computeCurrentSpeed() |
|
352 |
{ |
|
353 |
long firstTime = mLastTimestamps[mFirstIndex]; |
|
354 |
long lastTime = mLastTimestamps[mLastIndex]; |
|
355 |
float firstAngle = mLastAngles[mFirstIndex]; |
|
356 |
float lastAngle = mLastAngles[mLastIndex]; |
|
357 |
|
|
358 |
long timeDiff = lastTime-firstTime; |
|
359 |
|
|
360 |
mLastIndex = 0; |
|
361 |
mFirstIndex= 0; |
|
362 |
|
|
363 |
mCurrRotSpeed = timeDiff>0 ? (lastAngle-firstAngle)/timeDiff : 0; |
|
364 |
} |
|
365 |
|
|
318 | 366 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
319 | 367 |
// PUBLIC API |
320 | 368 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
326 | 374 |
if(!isInEditMode()) |
327 | 375 |
{ |
328 | 376 |
mLastCubitColor = -1; |
377 |
mCurrRotSpeed = 0.0f; |
|
378 |
|
|
379 |
mLastAngles = new float[NUM_SPEED_PROBES]; |
|
380 |
mLastTimestamps = new long[NUM_SPEED_PROBES]; |
|
381 |
mFirstIndex =0; |
|
382 |
mLastIndex =0; |
|
329 | 383 |
|
330 | 384 |
mRenderer = new RubikRenderer(this); |
331 | 385 |
mPreRender = new RubikPreRender(this); |
... | ... | |
390 | 444 |
}); |
391 | 445 |
} |
392 | 446 |
|
447 |
addSpeedProbe(0.0f); |
|
448 |
|
|
393 | 449 |
mBeginningRotation = false; |
394 | 450 |
mContinuingRotation= true; |
395 | 451 |
} |
... | ... | |
399 | 455 |
float angle = continueRotation(x-mStartRotX,y-mStartRotY); |
400 | 456 |
mCurrentAngle = SWIPING_SENSITIVITY*angle; |
401 | 457 |
mPreRender.getObject().continueRotation(mCurrentAngle); |
458 |
|
|
459 |
addSpeedProbe(mCurrentAngle); |
|
402 | 460 |
} |
403 | 461 |
else if( mDragging ) |
404 | 462 |
{ |
... | ... | |
430 | 488 |
|
431 | 489 |
if( mContinuingRotation ) |
432 | 490 |
{ |
433 |
mPreRender.finishRotation(); |
|
491 |
computeCurrentSpeed(); |
|
492 |
int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed); |
|
493 |
mPreRender.finishRotation(angle); |
|
434 | 494 |
|
435 | 495 |
if( RubikState.getCurrentState()==RubikState.SOLV ) |
436 | 496 |
{ |
437 | 497 |
RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass(); |
438 | 498 |
|
439 |
int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle); |
|
440 |
|
|
441 | 499 |
if( angle!=0 ) |
442 | 500 |
solving.addMove(mCurrentAxis, mCurrentRow, angle); |
443 | 501 |
} |
Also available in: Unified diff
Improvements for the way we rotate layers of Objects. (take the speed into account - so even if we rotated a layer of a Cube to less than 45 degrees, but we did it fast, do a 90 degree rotation!)