commit 775e675d1da37d84439b3466f4b92e5426646c89
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Feb 14 13:05:43 2020 +0000

    Further simplifications for object movement - remove from it a reference to the Object altogether.

diff --git a/src/main/java/org/distorted/magic/RubikRenderer.java b/src/main/java/org/distorted/magic/RubikRenderer.java
index a24bb36a..31532a1c 100644
--- a/src/main/java/org/distorted/magic/RubikRenderer.java
+++ b/src/main/java/org/distorted/magic/RubikRenderer.java
@@ -30,7 +30,6 @@ import org.distorted.library.main.DistortedTexture;
 import org.distorted.library.mesh.MeshFlat;
 import org.distorted.library.message.EffectListener;
 import org.distorted.object.RubikCube;
-import org.distorted.object.RubikCubeMovement;
 
 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;
@@ -112,9 +111,6 @@ public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
        mNewCube.recomputeScaleFactor(mScreenWidth, mScreenHeight);
        }
 
-     RubikCubeMovement movement = new RubikCubeMovement(mNewCube);
-     mView.setMovement(movement);
-
      mIsSolved = true;
      }
 
diff --git a/src/main/java/org/distorted/magic/RubikSurfaceView.java b/src/main/java/org/distorted/magic/RubikSurfaceView.java
index 4e52a23f..e43281ed 100644
--- a/src/main/java/org/distorted/magic/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/magic/RubikSurfaceView.java
@@ -34,13 +34,19 @@ import android.widget.Button;
 import android.widget.ImageButton;
 import android.widget.LinearLayout;
 
+import org.distorted.library.type.Static2D;
 import org.distorted.library.type.Static4D;
+import org.distorted.object.RubikCube;
 import org.distorted.object.RubikCubeMovement;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 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;
 
@@ -307,7 +313,7 @@ public class RubikSurfaceView extends GLSurfaceView
         axisY /= axisL;
         axisZ /= axisL;
 
-        float ratio = axisL/mScreenMin;
+        float ratio = axisL;
         ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
 
         float cosA = (float)Math.cos(Math.PI*ratio);
@@ -319,13 +325,6 @@ public class RubikSurfaceView extends GLSurfaceView
       return new Static4D(0f, 0f, 0f, 1f);
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void setMovement(RubikCubeMovement movement)
-      {
-      mMovement = movement;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -338,6 +337,7 @@ public class RubikSurfaceView extends GLSurfaceView
         {
         mInScrambleMode = false;
         mRenderer = new RubikRenderer(this);
+        mMovement = new RubikCubeMovement();
 
         final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
         final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
@@ -352,14 +352,14 @@ public class RubikSurfaceView extends GLSurfaceView
     public boolean onTouchEvent(MotionEvent event)
       {
       int action = event.getAction();
-      float x = event.getX() - mScreenWidth*0.5f;
-      float y = mScreenHeight*0.5f -event.getY();
+      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
+      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
 
       switch(action)
          {
          case MotionEvent.ACTION_DOWN: mX = x;
                                        mY = y;
-                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance, x, y);
+                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance/mScreenMin, x, y);
 
                                        if( mLastTouchedFace != RubikCubeMovement.NONE )
                                          {
@@ -379,9 +379,7 @@ public class RubikSurfaceView extends GLSurfaceView
                                          mTempCurrent.set(quatFromDrag(mX-x,y-mY));
                                          mRenderer.setQuatCurrentOnNextRender();
 
-                                         int minimumDist = (mScreenMin*mScreenMin)/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY);
-
-                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > minimumDist )
+                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
                                            {
                                            mX = x;
                                            mY = y;
@@ -393,18 +391,22 @@ public class RubikSurfaceView extends GLSurfaceView
                                          }
                                        if( mBeginningRotation )
                                          {
-                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
-
-                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
+                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
                                            {
-                                           mMovement.addNewRotation(mQuatAccumulated,mLastTouchedFace, x, y);
+                                           Static2D rot = mMovement.newRotation(mQuatAccumulated,mLastTouchedFace, x, y);
+                                           RubikCube cube = mRenderer.getCube();
+
+                                           cube.addNewRotation( (int)rot.get1(), (int)(cube.getSize()*rot.get2()) );
+
                                            mBeginningRotation = false;
                                            mContinuingRotation= true;
                                            }
                                          }
                                        else if( mContinuingRotation )
                                          {
-                                         mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x, y, mScreenMin);
+                                         float angle = mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x, y);
+
+                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
                                          }
                                        break;
          case MotionEvent.ACTION_UP  : if( mDragging )
diff --git a/src/main/java/org/distorted/object/RubikCube.java b/src/main/java/org/distorted/object/RubikCube.java
index 78ad9747..d1343d00 100644
--- a/src/main/java/org/distorted/object/RubikCube.java
+++ b/src/main/java/org/distorted/object/RubikCube.java
@@ -45,7 +45,7 @@ import org.distorted.magic.RubikSurfaceView;
 
 public class RubikCube extends DistortedNode
 {
-    private static final float CUBE_SCREEN_RATIO = 0.5f;
+            static final float CUBE_SCREEN_RATIO = 0.5f;
     private static final int POST_ROTATION_MILLISEC = 500;
     private static final int TEXTURE_SIZE = 100;
 
@@ -74,7 +74,6 @@ public class RubikCube extends DistortedNode
     private int mSize;
 
     private DistortedTexture mNodeTexture;
-    private float mCubeSizeInScreenSpace;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -212,51 +211,6 @@ public class RubikCube extends DistortedNode
       mCurrentPosition[x][y][z].set3(roundedZ);
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void addNewRotation(int vector, int row )
-      {
-      Static3D axis = VectX;
-
-      switch(vector)
-        {
-        case VECTX: axis = VectX; break;
-        case VECTY: axis = VectY; break;
-        case VECTZ: axis = VectZ; break;
-        }
-
-      mRotAxis = vector;
-      mRotRow  = row;
-
-      mRotationAngleStatic.set1(0.0f);
-
-      for(int x=0; x<mSize; x++)
-        for(int y=0; y<mSize; y++)
-          for(int z=0; z<mSize; z++)
-            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
-              {
-              if( belongsToRotation(x,y,z,vector,mRotRow) )
-                {
-                mRotationAxis[x][y][z].set(axis);
-                mRotationAngle[x][y][z].add(mRotationAngleStatic);
-                }
-              }
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void continueRotation(float angleInDegrees)
-      {
-      mRotationAngleStatic.set1(angleInDegrees);
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    float returnCubeSizeInScreenSpace()
-      {
-      return mCubeSizeInScreenSpace;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -372,6 +326,44 @@ public class RubikCube extends DistortedNode
             }
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void addNewRotation(int vector, int row )
+      {
+      Static3D axis = VectX;
+
+      switch(vector)
+        {
+        case VECTX: axis = VectX; break;
+        case VECTY: axis = VectY; break;
+        case VECTZ: axis = VectZ; break;
+        }
+
+      mRotAxis = vector;
+      mRotRow  = row;
+
+      mRotationAngleStatic.set1(0.0f);
+
+      for(int x=0; x<mSize; x++)
+        for(int y=0; y<mSize; y++)
+          for(int z=0; z<mSize; z++)
+            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
+              {
+              if( belongsToRotation(x,y,z,vector,mRotRow) )
+                {
+                mRotationAxis[x][y][z].set(axis);
+                mRotationAngle[x][y][z].add(mRotationAngleStatic);
+                }
+              }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void continueRotation(float angleInDegrees)
+      {
+      mRotationAngleStatic.set1(angleInDegrees);
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     public Static4D getRotationQuat()
@@ -496,8 +488,6 @@ public class RubikCube extends DistortedNode
 
     public void recomputeScaleFactor(int screenWidth, int screenHeight)
       {
-      mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
-
       int texW = mNodeTexture.getWidth();
       int texH = mNodeTexture.getHeight();
 
@@ -516,7 +506,7 @@ public class RubikCube extends DistortedNode
         mNodeScale.set(factor,factor,factor);
         }
 
-      float scaleFactor = (mCubeSizeInScreenSpace/(TEXTURE_SIZE*mSize)) * (float)texW/(screenWidth>screenHeight ? screenHeight:screenWidth);
+      float scaleFactor = (CUBE_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize));
 
       mMove.set( texW*0.5f , texH*0.5f , 0.0f );
       mScale.set(scaleFactor,scaleFactor,scaleFactor);
diff --git a/src/main/java/org/distorted/object/RubikCubeMovement.java b/src/main/java/org/distorted/object/RubikCubeMovement.java
index 791f37c5..aa7aa011 100644
--- a/src/main/java/org/distorted/object/RubikCubeMovement.java
+++ b/src/main/java/org/distorted/object/RubikCubeMovement.java
@@ -19,6 +19,7 @@
 
 package org.distorted.object;
 
+import org.distorted.library.type.Static2D;
 import org.distorted.library.type.Static4D;
 import org.distorted.magic.RubikSurfaceView;
 
@@ -36,12 +37,7 @@ public class RubikCubeMovement
 
     private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
 
-    // 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;
-
-    private RubikCube mCube;
-    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
+    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint;
     private int mRotationVect;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -51,58 +47,6 @@ public class RubikCubeMovement
       return (y>x) ? (y>=-x) : (y< -x);
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private boolean faceIsVisible(int face, float cubeHalfSize)
-      {
-      int sign = retFaceSign(face);
-      int zAxis= retFaceZaxis(face);
-
-      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, float cameraDistance)
-      {
-      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 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'.
-// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
-
-    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
-      {
-      int sign = retFaceSign(face);
-      int zAxis= retFaceZaxis(face);
-      float diff = mPoint[zAxis]-mCamera[zAxis];
-
-      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
-
-      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
-      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
-      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     private int retFaceSign(int face)
@@ -172,13 +116,63 @@ public class RubikCubeMovement
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// PUBLIC API
+
+    private boolean faceIsVisible(int face, float cubeHalfSize)
+      {
+      int sign = retFaceSign(face);
+      int zAxis= retFaceZaxis(face);
+
+      return sign*mCamera[zAxis] > cubeHalfSize;
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public RubikCubeMovement(RubikCube cube)
+    private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
       {
-      mCube = cube;
+      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, float cameraDistance)
+      {
+      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 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'.
+// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
+
+    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
+      {
+      int sign = retFaceSign(face);
+      int zAxis= retFaceZaxis(face);
+      float diff = mPoint[zAxis]-mCamera[zAxis];
+
+      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
+
+      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
+      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
+      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// PUBLIC API
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public RubikCubeMovement()
+      {
       mRotationVect = VECT[0];
 
       mPoint = new float[3];
@@ -192,7 +186,7 @@ public class RubikCubeMovement
 
     public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y)
       {
-      float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
+      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
       convertTouchPointToScreenSpace(accumulated,x,y);
       convertCameraPointToScreenSpace(accumulated, cameraDistance);
@@ -216,9 +210,9 @@ public class RubikCubeMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public void addNewRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
+    public Static2D newRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
       {
-      float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
+      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
       convertTouchPointToScreenSpace(accumulated,x,y);
       castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff);
@@ -236,12 +230,12 @@ public class RubikCubeMovement
       mTouchPoint[1] = mPoint[1];
       mTouchPoint[2] = mPoint[2];
 
-      mCube.addNewRotation(mRotationVect, (int)(mCube.getSize()*offset) );
+      return new Static2D(mRotationVect,offset);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public void continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y, int scrMin)
+    public float continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
       {
       convertTouchPointToScreenSpace(accumulated,x,y);
 
@@ -254,6 +248,6 @@ public class RubikCubeMovement
       int sign = retFaceRotationSign(lastTouchedFace);
       float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
 
-      mCube.continueRotation(SWIPING_SENSITIVITY*sign*angle/scrMin);
+      return sign*angle;
       }
 }
