commit 93594b9508cf5f9b80ee095304df80ca8c17a63c
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Mar 10 22:17:04 2020 +0000

    Improve RubikCubeMovement

diff --git a/src/main/java/org/distorted/object/RubikCubeMovement.java b/src/main/java/org/distorted/object/RubikCubeMovement.java
index be09c97c..08341442 100644
--- a/src/main/java/org/distorted/object/RubikCubeMovement.java
+++ b/src/main/java/org/distorted/object/RubikCubeMovement.java
@@ -26,20 +26,17 @@ import org.distorted.library.type.Static4D;
 
 class RubikCubeMovement extends RubikObjectMovement
 {
-    private final static int NONE   =-1;
-    private final static int FRONT  = 0;  //
-    private final static int BACK   = 1;  //
-    private final static int LEFT   = 2;  // has to be 6 consecutive ints
-    private final static int RIGHT  = 3;  // FRONT ... BOTTOM
-    private final static int TOP    = 4;  //
-    private final static int BOTTOM = 5;  //
+    private final static int LEFT   = 0;  // axisX left
+    private final static int RIGHT  = 1;  // axisX right
+    private final static int BOTTOM = 2;  // axisY left
+    private final static int TOP    = 3;  // axisY right
+    private final static int BACK   = 4;  // axisZ left
+    private final static int FRONT  = 5;  // axisZ right
 
     private static final int VECTX  = 0;  //
     private static final int VECTY  = 1;  // don't change this
     private static final int VECTZ  = 2;  //
 
-    private static final int[] VECT = {VECTX,VECTY,VECTZ};
-
     private float[] mPoint, mCamera, mDiff, mTouch;
     private int mRotationVect, mLastTouchedFace;
 
@@ -71,39 +68,19 @@ class RubikCubeMovement extends RubikObjectMovement
 
     private int retFaceXaxis(int face)
       {
-      switch(face)
-        {
-        case FRONT :
-        case BACK  : return VECTX;
-        case LEFT  :
-        case RIGHT : return VECTZ;
-        case TOP   :
-        case BOTTOM: return VECTX;
-        }
-
-      return -1;
+      return face==LEFT || face==RIGHT ? VECTZ : VECTX;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     private int retFaceYaxis(int face)
       {
-      switch(face)
-        {
-        case FRONT :
-        case BACK  : return VECTY;
-        case LEFT  :
-        case RIGHT : return VECTY;
-        case TOP   :
-        case BOTTOM: return VECTZ;
-        }
-
-      return -1;
+      return face==TOP || face==BOTTOM ? VECTZ : VECTY;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private int retFaceZaxis(int face)
+    private int retFaceAxis(int face)
       {
       switch(face)
         {
@@ -120,12 +97,12 @@ class RubikCubeMovement extends RubikObjectMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private boolean faceIsVisible(int face, float cubeHalfSize)
+    private boolean faceIsVisible(int face)
       {
       int sign = retFaceSign(face);
-      int zAxis= retFaceZaxis(face);
+      int zAxis= retFaceAxis(face);
 
-      return sign*mCamera[zAxis] > cubeHalfSize;
+      return sign*mCamera[zAxis] > 0.5f;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -133,13 +110,13 @@ class RubikCubeMovement extends RubikObjectMovement
 // cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
 // Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
 
-    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
+    private void castTouchPointOntoFace(int face, float[] output)
       {
       int sign = retFaceSign(face);
-      int zAxis= retFaceZaxis(face);
+      int zAxis= retFaceAxis(face);
       float diff = mPoint[zAxis]-mCamera[zAxis];
 
-      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
+      float ratio =  diff!=0.0f ? (sign*0.5f-mCamera[zAxis])/diff : 0.0f;
 
       output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
       output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
@@ -162,31 +139,26 @@ class RubikCubeMovement extends RubikObjectMovement
 
     public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
       {
-      float cubeHalfSize= RubikObject.OBJECT_SCREEN_RATIO*0.5f;
-
-      mPoint[0]  = rotatedTouchPoint.get0();
-      mPoint[1]  = rotatedTouchPoint.get1();
-      mPoint[2]  = rotatedTouchPoint.get2();
+      mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
+      mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
+      mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
 
-      mCamera[0] = rotatedCamera.get0();
-      mCamera[1] = rotatedCamera.get1();
-      mCamera[2] = rotatedCamera.get2();
+      mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
+      mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
+      mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
 
-      for( mLastTouchedFace=FRONT; mLastTouchedFace<=BOTTOM; mLastTouchedFace++)
+      for( mLastTouchedFace=LEFT; mLastTouchedFace<=FRONT; mLastTouchedFace++)
         {
-        if( faceIsVisible(mLastTouchedFace,cubeHalfSize) )
+        if( faceIsVisible(mLastTouchedFace) )
           {
-          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouch);
+          castTouchPointOntoFace(mLastTouchedFace, mTouch);
 
-          float qX= (mTouch[0]+cubeHalfSize) / (2*cubeHalfSize);
-          float qY= (mTouch[1]+cubeHalfSize) / (2*cubeHalfSize);
-          float qZ= (mTouch[2]+cubeHalfSize) / (2*cubeHalfSize);
-
-          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return true;
+          if( mTouch[0]<=0.5f && mTouch[0]>=-0.5f &&
+              mTouch[1]<=0.5f && mTouch[1]>=-0.5f &&
+              mTouch[2]<=0.5f && mTouch[2]>=-0.5f  ) return true;
           }
         }
 
-      mLastTouchedFace = NONE;
       return false;
       }
 
@@ -194,13 +166,11 @@ class RubikCubeMovement extends RubikObjectMovement
 
     public Static2D newRotation(Static4D rotatedTouchPoint)
       {
-      float cubeHalfSize= RubikObject.OBJECT_SCREEN_RATIO*0.5f;
-
-      mPoint[0] = rotatedTouchPoint.get0();
-      mPoint[1] = rotatedTouchPoint.get1();
-      mPoint[2] = rotatedTouchPoint.get2();
+      mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
+      mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
+      mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
 
-      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
+      castTouchPointOntoFace(mLastTouchedFace,mDiff);
 
       mDiff[0] -= mTouch[0];
       mDiff[1] -= mTouch[1];
@@ -208,8 +178,8 @@ class RubikCubeMovement extends RubikObjectMovement
 
       int xAxis = retFaceXaxis(mLastTouchedFace);
       int yAxis = retFaceYaxis(mLastTouchedFace);
-      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
-      float offset= (mTouch[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
+      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
+      float offset= mTouch[mRotationVect]+0.5f;
 
       mTouch[0] = mPoint[0];
       mTouch[1] = mPoint[1];
@@ -222,15 +192,15 @@ class RubikCubeMovement extends RubikObjectMovement
 
     public float continueRotation(Static4D rotatedTouchPoint)
       {
-      mDiff[0] = rotatedTouchPoint.get0()-mTouch[0];
-      mDiff[1] = rotatedTouchPoint.get1()-mTouch[1];
-      mDiff[2] = rotatedTouchPoint.get2()-mTouch[2];
+      mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[0];
+      mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[1];
+      mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[2];
 
       int xAxis= retFaceXaxis(mLastTouchedFace);
       int yAxis= retFaceYaxis(mLastTouchedFace);
       int sign = retFaceRotationSign(mLastTouchedFace);
       float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
 
-      return sign*angle;
+      return sign*angle*0.5f;
       }
 }
