commit 86c73a6918f7eeb8f4ce6ea5dd87c7d43459976f
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Feb 14 23:04:14 2020 +0000

    Movement.

diff --git a/src/main/java/org/distorted/magic/RubikSurfaceView.java b/src/main/java/org/distorted/magic/RubikSurfaceView.java
index b8fc8e70..10273c0c 100644
--- a/src/main/java/org/distorted/magic/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/magic/RubikSurfaceView.java
@@ -46,13 +46,13 @@ public class RubikSurfaceView extends GLSurfaceView
     // Moving the finger from the middle of the vertical screen to the right edge will rotate a
     // given face by SWIPING_SENSITIVITY/2 degrees.
     private final static int SWIPING_SENSITIVITY  = 240;
-
     // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
     private final static int ROTATION_SENSITIVITY =  12;
-
     // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
     private final static int DIRECTION_SENSITIVITY=  12;
 
+    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
+
     private RubikRenderer mRenderer;
     private RubikCubeMovement mMovement;
 
@@ -351,7 +351,11 @@ public class RubikSurfaceView extends GLSurfaceView
          case MotionEvent.ACTION_DOWN: mX = x;
                                        mY = y;
 
-                                       if( mMovement.faceTouched(mQuatAccumulated, x, y) )
+                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
+                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
+                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
+
+                                       if( mMovement.faceTouched(rotatedTouchPoint1, rotatedCamera) )
                                          {
                                          mDragging           = false;
                                          mBeginningRotation  = mRenderer.canRotate();
@@ -383,7 +387,10 @@ public class RubikSurfaceView extends GLSurfaceView
                                          {
                                          if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
                                            {
-                                           Static2D rot = mMovement.newRotation(mQuatAccumulated, x, y);
+                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
+                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
+
+                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
                                            RubikCube cube = mRenderer.getCube();
 
                                            cube.addNewRotation( (int)rot.get1(), (int)(cube.getSize()*rot.get2()) );
@@ -394,7 +401,10 @@ public class RubikSurfaceView extends GLSurfaceView
                                          }
                                        else if( mContinuingRotation )
                                          {
-                                         float angle = mMovement.continueRotation(mQuatAccumulated, x, y);
+                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
+                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
+
+                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
                                          mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
                                          }
                                        break;
diff --git a/src/main/java/org/distorted/object/RubikCubeMovement.java b/src/main/java/org/distorted/object/RubikCubeMovement.java
index f45bd2a7..5ef411a2 100644
--- a/src/main/java/org/distorted/object/RubikCubeMovement.java
+++ b/src/main/java/org/distorted/object/RubikCubeMovement.java
@@ -21,8 +21,6 @@ package org.distorted.object;
 
 import org.distorted.library.type.Static2D;
 import org.distorted.library.type.Static4D;
-import org.distorted.magic.RubikRenderer;
-import org.distorted.magic.RubikSurfaceView;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -38,7 +36,7 @@ public class RubikCubeMovement
 
     private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
 
-    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint;
+    private float[] mPoint, mCamera, mDiff, mTouch;
     private int mRotationVect, mLastTouchedFace;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -126,30 +124,6 @@ public class RubikCubeMovement
       return sign*mCamera[zAxis] > cubeHalfSize;
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
-      {
-      Static4D touchPoint = new Static4D(x, y, 0, 0);
-      Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated);
-
-      mPoint[0] = rotatedTouchPoint.get1();
-      mPoint[1] = rotatedTouchPoint.get2();
-      mPoint[2] = rotatedTouchPoint.get3();
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private void convertCameraPointToScreenSpace(Static4D accumulated)
-      {
-      Static4D cameraPoint = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
-      Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated);
-
-      mCamera[0] = rotatedCamera.get1();
-      mCamera[1] = rotatedCamera.get2();
-      mCamera[2] = rotatedCamera.get3();
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
 // cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
@@ -174,33 +148,35 @@ public class RubikCubeMovement
 
     public RubikCubeMovement()
       {
-      mRotationVect = VECT[0];
-
       mPoint = new float[3];
       mCamera= new float[3];
       mDiff  = new float[3];
-      mTouchPoint = new float[3];
-      mTouchPointCastOntoFace = new float[3];
+      mTouch = new float[3];
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public boolean faceTouched(Static4D rotQuaternion, float x, float y)
+    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
       {
       float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
-      convertTouchPointToScreenSpace(rotQuaternion,x,y);
-      convertCameraPointToScreenSpace(rotQuaternion);
+      mPoint[0]  = rotatedTouchPoint.get1();
+      mPoint[1]  = rotatedTouchPoint.get2();
+      mPoint[2]  = rotatedTouchPoint.get3();
+
+      mCamera[0] = rotatedCamera.get1();
+      mCamera[1] = rotatedCamera.get2();
+      mCamera[2] = rotatedCamera.get3();
 
       for( mLastTouchedFace=FRONT; mLastTouchedFace<=BOTTOM; mLastTouchedFace++)
         {
         if( faceIsVisible(mLastTouchedFace,cubeHalfSize) )
           {
-          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouchPointCastOntoFace);
+          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouch);
 
-          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
-          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
-          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
+          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;
           }
@@ -212,38 +188,39 @@ public class RubikCubeMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public Static2D newRotation(Static4D rotQuaternion, float x, float y)
+    public Static2D newRotation(Static4D rotatedTouchPoint)
       {
       float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
-      convertTouchPointToScreenSpace(rotQuaternion,x,y);
+      mPoint[0] = rotatedTouchPoint.get1();
+      mPoint[1] = rotatedTouchPoint.get2();
+      mPoint[2] = rotatedTouchPoint.get3();
+
       castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
 
-      mDiff[0] -= mTouchPointCastOntoFace[0];
-      mDiff[1] -= mTouchPointCastOntoFace[1];
-      mDiff[2] -= mTouchPointCastOntoFace[2];
+      mDiff[0] -= mTouch[0];
+      mDiff[1] -= mTouch[1];
+      mDiff[2] -= mTouch[2];
 
       int xAxis = retFaceXaxis(mLastTouchedFace);
       int yAxis = retFaceYaxis(mLastTouchedFace);
       mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
-      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
+      float offset= (mTouch[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
 
-      mTouchPoint[0] = mPoint[0];
-      mTouchPoint[1] = mPoint[1];
-      mTouchPoint[2] = mPoint[2];
+      mTouch[0] = mPoint[0];
+      mTouch[1] = mPoint[1];
+      mTouch[2] = mPoint[2];
 
       return new Static2D(mRotationVect,offset);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public float continueRotation(Static4D rotQuaternion, float x, float y)
+    public float continueRotation(Static4D rotatedTouchPoint)
       {
-      convertTouchPointToScreenSpace(rotQuaternion,x,y);
-
-      mDiff[0] = mPoint[0]-mTouchPoint[0];
-      mDiff[1] = mPoint[1]-mTouchPoint[1];
-      mDiff[2] = mPoint[2]-mTouchPoint[2];
+      mDiff[0] = rotatedTouchPoint.get1()-mTouch[0];
+      mDiff[1] = rotatedTouchPoint.get2()-mTouch[1];
+      mDiff[2] = rotatedTouchPoint.get3()-mTouch[2];
 
       int xAxis= retFaceXaxis(mLastTouchedFace);
       int yAxis= retFaceYaxis(mLastTouchedFace);
