Revision 001cc0e4
Added by Leszek Koltunski over 5 years ago
| src/main/java/org/distorted/objects/Cubit.java | ||
|---|---|---|
| 21 | 21 |
|
| 22 | 22 |
import android.content.SharedPreferences; |
| 23 | 23 |
|
| 24 |
import com.google.firebase.crashlytics.FirebaseCrashlytics; |
|
| 25 |
|
|
| 26 | 24 |
import org.distorted.library.effect.MatrixEffectMove; |
| 27 | 25 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
| 28 | 26 |
import org.distorted.library.effect.MatrixEffectRotate; |
| ... | ... | |
| 40 | 38 |
|
| 41 | 39 |
class Cubit |
| 42 | 40 |
{
|
| 41 |
private static final int POST_ROTATION_MILLISEC = 500; |
|
| 43 | 42 |
private static final Static3D matrCenter = new Static3D(0,0,0); |
| 44 | 43 |
|
| 45 | 44 |
private final Static3D mOrigPosition; |
| ... | ... | |
| 50 | 49 |
private MatrixEffectRotate mRotateEffect; |
| 51 | 50 |
private Static3D mCurrentPosition; |
| 52 | 51 |
private int mNumAxis; |
| 52 |
private Dynamic1D mRotationAngle; |
|
| 53 | 53 |
|
| 54 |
Dynamic1D mRotationAngle; |
|
| 55 | 54 |
DistortedNode mNode; |
| 56 | 55 |
DistortedEffects mEffect; |
| 57 | 56 |
Static4D mQuatScramble; |
| ... | ... | |
| 300 | 299 |
|
| 301 | 300 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 302 | 301 |
|
| 303 |
long finishRotationNow(EffectListener listener)
|
|
| 302 |
void removeRotationNow(Static4D quat)
|
|
| 304 | 303 |
{
|
| 305 |
int pointNum = mRotationAngle.getNumPoints(); |
|
| 306 |
|
|
| 307 |
if( pointNum>=1 ) |
|
| 308 |
{
|
|
| 309 |
float startingAngle = mRotationAngle.getPoint(pointNum-1).get0(); |
|
| 310 |
int nearestAngleInDegrees = mParent.computeNearestAngle(startingAngle); |
|
| 311 |
mParent.mRotationAngleStatic.set0(startingAngle); |
|
| 312 |
mParent.mRotationAngleFinal.set0(nearestAngleInDegrees); |
|
| 313 |
mParent.mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f ); |
|
| 314 |
return setUpCallback(listener); |
|
| 315 |
} |
|
| 316 |
else |
|
| 317 |
{
|
|
| 318 |
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
|
| 319 |
crashlytics.setCustomKey("points", "finish, points in RotationAngle: "+pointNum );
|
|
| 320 |
return 0; |
|
| 321 |
} |
|
| 304 |
mRotationAngle.removeAll(); |
|
| 305 |
mQuatScramble.set(RubikSurfaceView.quatMultiply(quat,mQuatScramble)); |
|
| 306 |
normalizeScrambleQuat( mQuatScramble ); |
|
| 307 |
modifyCurrentPosition(quat); |
|
| 322 | 308 |
} |
| 323 | 309 |
|
| 324 | 310 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 325 | 311 |
|
| 326 |
Static4D returnRotationQuat(int axis)
|
|
| 312 |
void beginNewRotation(int axis)
|
|
| 327 | 313 |
{
|
| 328 |
int pointNum = mRotationAngle.getNumPoints(); |
|
| 314 |
mRotationAxis.set( mParent.ROTATION_AXIS[axis] ); |
|
| 315 |
mRotationAngle.add(mParent.mRotationAngleStatic); |
|
| 316 |
} |
|
| 329 | 317 |
|
| 330 |
if( pointNum>=1 ) |
|
| 331 |
{
|
|
| 332 |
float axisX = mParent.ROTATION_AXIS[axis].get0(); |
|
| 333 |
float axisY = mParent.ROTATION_AXIS[axis].get1(); |
|
| 334 |
float axisZ = mParent.ROTATION_AXIS[axis].get2(); |
|
| 335 |
|
|
| 336 |
float startingAngle = mRotationAngle.getPoint(pointNum-1).get0(); |
|
| 337 |
int nearestAngleInDegrees = mParent.computeNearestAngle(startingAngle); |
|
| 338 |
double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180; |
|
| 339 |
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5); |
|
| 340 |
float cosA = (float)Math.cos(nearestAngleInRadians*0.5); |
|
| 341 |
return new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
| 342 |
} |
|
| 343 |
else |
|
| 344 |
{
|
|
| 345 |
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
|
| 346 |
crashlytics.setCustomKey("points", "return, points in RotationAngle: "+pointNum );
|
|
| 347 |
return null; |
|
| 348 |
} |
|
| 318 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 319 |
|
|
| 320 |
void addNewRotation(int axis, long durationMillis, int angle) |
|
| 321 |
{
|
|
| 322 |
mRotationAxis.set( mParent.ROTATION_AXIS[axis] ); |
|
| 323 |
mRotationAngle.setDuration(durationMillis); |
|
| 324 |
mRotationAngle.resetToBeginning(); |
|
| 325 |
mRotationAngle.add(new Static1D(0)); |
|
| 326 |
mRotationAngle.add(new Static1D(angle)); |
|
| 349 | 327 |
} |
| 350 | 328 |
|
| 351 | 329 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 352 | 330 |
|
| 353 |
void removeRotationNow(Static4D quat)
|
|
| 331 |
void resetRotationAngle()
|
|
| 354 | 332 |
{
|
| 333 |
mRotationAngle.setDuration(POST_ROTATION_MILLISEC); |
|
| 334 |
mRotationAngle.resetToBeginning(); |
|
| 355 | 335 |
mRotationAngle.removeAll(); |
| 356 |
mQuatScramble.set(RubikSurfaceView.quatMultiply(quat,mQuatScramble));
|
|
| 357 |
normalizeScrambleQuat( mQuatScramble );
|
|
| 358 |
modifyCurrentPosition(quat);
|
|
| 336 |
mRotationAngle.add(mParent.mRotationAngleStatic);
|
|
| 337 |
mRotationAngle.add(mParent.mRotationAngleMiddle);
|
|
| 338 |
mRotationAngle.add(mParent.mRotationAngleFinal);
|
|
| 359 | 339 |
} |
| 360 | 340 |
|
| 361 | 341 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 378 | 358 |
computeRotationRow(); |
| 379 | 359 |
} |
| 380 | 360 |
|
| 381 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 382 |
|
|
| 383 |
void beginNewRotation(int axis) |
|
| 384 |
{
|
|
| 385 |
mRotationAxis.set( mParent.ROTATION_AXIS[axis] ); |
|
| 386 |
mRotationAngle.add(mParent.mRotationAngleStatic); |
|
| 387 |
} |
|
| 388 |
|
|
| 389 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 390 |
|
|
| 391 |
void addNewRotation(int axis, long durationMillis, int angle) |
|
| 392 |
{
|
|
| 393 |
mRotationAxis.set( mParent.ROTATION_AXIS[axis] ); |
|
| 394 |
mRotationAngle.setDuration(durationMillis); |
|
| 395 |
mRotationAngle.resetToBeginning(); |
|
| 396 |
mRotationAngle.add(new Static1D(0)); |
|
| 397 |
mRotationAngle.add(new Static1D(angle)); |
|
| 398 |
} |
|
| 399 |
|
|
| 400 | 361 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 401 | 362 |
|
| 402 | 363 |
long setUpCallback(EffectListener listener) |
| src/main/java/org/distorted/objects/RubikObject.java | ||
|---|---|---|
| 34 | 34 |
import org.distorted.library.mesh.MeshBase; |
| 35 | 35 |
import org.distorted.library.mesh.MeshRectangles; |
| 36 | 36 |
import org.distorted.library.message.EffectListener; |
| 37 |
import org.distorted.library.type.Dynamic1D; |
|
| 38 | 37 |
import org.distorted.library.type.Static1D; |
| 39 | 38 |
import org.distorted.library.type.Static3D; |
| 40 | 39 |
import org.distorted.library.type.Static4D; |
| ... | ... | |
| 51 | 50 |
|
| 52 | 51 |
static float OBJECT_SCREEN_RATIO; |
| 53 | 52 |
|
| 54 |
private static final int POST_ROTATION_MILLISEC = 500; |
|
| 55 | 53 |
private final int NUM_CUBITS; |
| 56 | 54 |
private int mRotRowBitmap; |
| 57 | 55 |
private int mRotAxis; |
| ... | ... | |
| 205 | 203 |
return diff*diff < MAX_ERROR; |
| 206 | 204 |
} |
| 207 | 205 |
|
| 208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 209 |
|
|
| 210 |
private void resetRotationAngle(Dynamic1D rotationAngle) |
|
| 211 |
{
|
|
| 212 |
rotationAngle.setDuration(POST_ROTATION_MILLISEC); |
|
| 213 |
rotationAngle.resetToBeginning(); |
|
| 214 |
rotationAngle.removeAll(); |
|
| 215 |
rotationAngle.add(mRotationAngleStatic); |
|
| 216 |
rotationAngle.add(mRotationAngleMiddle); |
|
| 217 |
rotationAngle.add(mRotationAngleFinal); |
|
| 218 |
} |
|
| 219 |
|
|
| 220 | 206 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 221 | 207 |
// note the minus in front of the sin() - we rotate counterclockwise |
| 222 | 208 |
// when looking towards the direction where the axis increases in values. |
| ... | ... | |
| 368 | 354 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences); |
| 369 | 355 |
} |
| 370 | 356 |
|
| 371 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 372 |
|
|
| 373 |
public long finishRotationNow(EffectListener listener) |
|
| 374 |
{
|
|
| 375 |
boolean first = true; |
|
| 376 |
long effectID=0; |
|
| 377 |
|
|
| 378 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 379 |
{
|
|
| 380 |
if( belongsToRotation(i,mRotAxis,mRotRowBitmap) ) |
|
| 381 |
{
|
|
| 382 |
if( first ) |
|
| 383 |
{
|
|
| 384 |
first=false; |
|
| 385 |
effectID = mCubits[i].finishRotationNow(listener); |
|
| 386 |
} |
|
| 387 |
resetRotationAngle(mCubits[i].mRotationAngle); |
|
| 388 |
} |
|
| 389 |
} |
|
| 390 |
|
|
| 391 |
return effectID; |
|
| 392 |
} |
|
| 393 |
|
|
| 394 | 357 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 395 | 358 |
|
| 396 | 359 |
public void releaseResources() |
| ... | ... | |
| 490 | 453 |
public long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener ) |
| 491 | 454 |
{
|
| 492 | 455 |
long effectID=0; |
| 493 |
boolean first = true;
|
|
| 456 |
int firstCubit = -1;
|
|
| 494 | 457 |
|
| 495 | 458 |
mRotAxis = axis; |
| 496 | 459 |
mRotRowBitmap = rowBitmap; |
| ... | ... | |
| 501 | 464 |
if( belongsToRotation(i,mRotAxis,mRotRowBitmap) ) |
| 502 | 465 |
{
|
| 503 | 466 |
mCubits[i].addNewRotation(axis,durationMillis,angle); |
| 504 |
|
|
| 505 |
if( first ) |
|
| 506 |
{
|
|
| 507 |
first = false; |
|
| 508 |
effectID = mCubits[i].setUpCallback(listener); |
|
| 509 |
} |
|
| 467 |
if( firstCubit<0 ) firstCubit = i; |
|
| 510 | 468 |
} |
| 511 | 469 |
|
| 470 |
if( firstCubit>=0 ) effectID = mCubits[firstCubit].setUpCallback(listener); |
|
| 471 |
|
|
| 512 | 472 |
return effectID; |
| 513 | 473 |
} |
| 514 | 474 |
|
| 475 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 476 |
|
|
| 477 |
public long finishRotationNow(EffectListener listener) |
|
| 478 |
{
|
|
| 479 |
int firstCubit= -1; |
|
| 480 |
long effectID = 0; |
|
| 481 |
|
|
| 482 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 483 |
{
|
|
| 484 |
if( belongsToRotation(i,mRotAxis,mRotRowBitmap) ) |
|
| 485 |
{
|
|
| 486 |
if( firstCubit<0 ) |
|
| 487 |
{
|
|
| 488 |
firstCubit=i; |
|
| 489 |
|
|
| 490 |
float angle = mRotationAngleStatic.get0(); |
|
| 491 |
int nearestAngleInDegrees = computeNearestAngle(angle); |
|
| 492 |
mRotationAngleStatic.set0(angle); |
|
| 493 |
mRotationAngleFinal.set0(nearestAngleInDegrees); |
|
| 494 |
mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f ); |
|
| 495 |
} |
|
| 496 |
mCubits[i].resetRotationAngle(); |
|
| 497 |
} |
|
| 498 |
} |
|
| 499 |
|
|
| 500 |
if( firstCubit>=0 ) effectID = mCubits[firstCubit].setUpCallback(listener); |
|
| 501 |
|
|
| 502 |
return effectID; |
|
| 503 |
} |
|
| 504 |
|
|
| 515 | 505 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 516 | 506 |
|
| 517 | 507 |
public void removeRotationNow() |
| ... | ... | |
| 525 | 515 |
if( first ) |
| 526 | 516 |
{
|
| 527 | 517 |
first = false; |
| 528 |
quat = mCubits[i].returnRotationQuat(mRotAxis); |
|
| 518 |
|
|
| 519 |
float angle = mRotationAngleFinal.get0(); |
|
| 520 |
int nearestAngleInDegrees = computeNearestAngle(angle); |
|
| 521 |
double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180; |
|
| 522 |
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5); |
|
| 523 |
float cosA = (float)Math.cos(nearestAngleInRadians*0.5); |
|
| 524 |
float axisX = ROTATION_AXIS[mRotAxis].get0(); |
|
| 525 |
float axisY = ROTATION_AXIS[mRotAxis].get1(); |
|
| 526 |
float axisZ = ROTATION_AXIS[mRotAxis].get2(); |
|
| 527 |
quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
| 529 | 528 |
} |
| 530 | 529 |
|
| 531 | 530 |
mCubits[i].removeRotationNow(quat); |
Also available in: Unified diff
Fix a potential crasher ( do not set up a callback for end of rotation effect on the very first cubit that belongs to a rotation if all belonging cubits have not been set up yet )