commit 123d61721c61567ed506d9c2769bb3c6da80412b
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Feb 14 13:30:00 2020 +0000

    Further simplifications for object movement.

diff --git a/src/main/java/org/distorted/magic/RubikRenderer.java b/src/main/java/org/distorted/magic/RubikRenderer.java
index 31532a1c..f3b362d7 100644
--- a/src/main/java/org/distorted/magic/RubikRenderer.java
+++ b/src/main/java/org/distorted/magic/RubikRenderer.java
@@ -38,8 +38,8 @@ import javax.microedition.khronos.opengles.GL10;
 
 public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
 {
-    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
-    public  static final int TEXTURE_SIZE = 600;
+    public static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
+    public static final int TEXTURE_SIZE = 600;
 
     private RubikSurfaceView mView;
     private DistortedScreen mScreen;
@@ -85,14 +85,6 @@ public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
       mNextCubeSize = RubikSize.getSize(mView.getRedButton()).getCubeSize();
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   private float computeFOV(float cameraDistance, int screenHeight)
-     {
-     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
-     return (float)(2*halfFOVInRadians*(180/Math.PI));
-     }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
    private void createCubeNow(int newSize)
@@ -297,18 +289,16 @@ public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
       {
       if( mNewCube!=null ) mNewCube.createTexture();
 
-      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
-      float fovInDegrees   = computeFOV(cameraDistance,height);
-
-      mView.setScreenSize(width,height);
-      mView.setCameraDist(cameraDistance);
+      double halfFOVInRadians = Math.atan( 1.0f/(2*CAMERA_DISTANCE) );
+      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
 
       mScreen.setProjection( fovInDegrees, 0.1f);
       mScreen.resize(width, height);
+      mView.setScreenSize(width,height);
 
       if( mNewCube!=null )
         {
-        mNewCube.recomputeScaleFactor(width, height);
+        mNewCube.recomputeScaleFactor(width,height);
         }
 
       mScreenHeight = height;
diff --git a/src/main/java/org/distorted/magic/RubikSurfaceView.java b/src/main/java/org/distorted/magic/RubikSurfaceView.java
index e43281ed..65f046f0 100644
--- a/src/main/java/org/distorted/magic/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/magic/RubikSurfaceView.java
@@ -61,8 +61,6 @@ public class RubikSurfaceView extends GLSurfaceView
 
     private boolean mDragging, mBeginningRotation, mContinuingRotation;
     private float mX, mY;
-    private int mLastTouchedFace;
-    private float mCameraDistance;
     private int mScreenWidth, mScreenHeight, mScreenMin;
 
     private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
@@ -135,13 +133,6 @@ public class RubikSurfaceView extends GLSurfaceView
       mScreenMin = width<height ? width:height;
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void setCameraDist(float distance)
-      {
-      mCameraDistance = distance;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     RubikRenderer getRenderer()
@@ -359,9 +350,8 @@ public class RubikSurfaceView extends GLSurfaceView
          {
          case MotionEvent.ACTION_DOWN: mX = x;
                                        mY = y;
-                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance/mScreenMin, x, y);
 
-                                       if( mLastTouchedFace != RubikCubeMovement.NONE )
+                                       if( mMovement.faceTouched(mQuatAccumulated, x, y) )
                                          {
                                          mDragging           = false;
                                          mBeginningRotation  = mRenderer.canRotate();
@@ -393,7 +383,7 @@ 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,mLastTouchedFace, x, y);
+                                           Static2D rot = mMovement.newRotation(mQuatAccumulated, x, y);
                                            RubikCube cube = mRenderer.getCube();
 
                                            cube.addNewRotation( (int)rot.get1(), (int)(cube.getSize()*rot.get2()) );
@@ -404,8 +394,7 @@ public class RubikSurfaceView extends GLSurfaceView
                                          }
                                        else if( mContinuingRotation )
                                          {
-                                         float angle = mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x, y);
-
+                                         float angle = mMovement.continueRotation(mQuatAccumulated, x, y);
                                          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 aa7aa011..4d7295cb 100644
--- a/src/main/java/org/distorted/object/RubikCubeMovement.java
+++ b/src/main/java/org/distorted/object/RubikCubeMovement.java
@@ -21,6 +21,7 @@ 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 +39,7 @@ public class RubikCubeMovement
     private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
 
     private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint;
-    private int mRotationVect;
+    private int mRotationVect, mLastTouchedFace;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -139,9 +140,9 @@ public class RubikCubeMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private void convertCameraPointToScreenSpace(Static4D accumulated, float cameraDistance)
+    private void convertCameraPointToScreenSpace(Static4D accumulated)
       {
-      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0);
+      Static4D cameraPoint = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
       Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated);
 
       mCamera[0] = rotatedCamera.get1();
@@ -184,45 +185,46 @@ public class RubikCubeMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y)
+    public boolean faceTouched(Static4D rotQuaternion, float x, float y)
       {
       float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
-      convertTouchPointToScreenSpace(accumulated,x,y);
-      convertCameraPointToScreenSpace(accumulated, cameraDistance);
+      convertTouchPointToScreenSpace(rotQuaternion,x,y);
+      convertCameraPointToScreenSpace(rotQuaternion);
 
-      for(int face=FRONT; face<=BOTTOM; face++)
+      for( mLastTouchedFace=FRONT; mLastTouchedFace<=BOTTOM; mLastTouchedFace++)
         {
-        if( faceIsVisible(face,cubeHalfSize) )
+        if( faceIsVisible(mLastTouchedFace,cubeHalfSize) )
           {
-          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
+          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouchPointCastOntoFace);
 
           float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
           float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
           float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
 
-          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
+          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return true;
           }
         }
 
-      return NONE;
+      mLastTouchedFace = NONE;
+      return false;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public Static2D newRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
+    public Static2D newRotation(Static4D rotQuaternion, float x, float y)
       {
       float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
 
-      convertTouchPointToScreenSpace(accumulated,x,y);
-      castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff);
+      convertTouchPointToScreenSpace(rotQuaternion,x,y);
+      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
 
       mDiff[0] -= mTouchPointCastOntoFace[0];
       mDiff[1] -= mTouchPointCastOntoFace[1];
       mDiff[2] -= mTouchPointCastOntoFace[2];
 
-      int xAxis = retFaceXaxis(lastTouchedFace);
-      int yAxis = retFaceYaxis(lastTouchedFace);
+      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);
 
@@ -235,17 +237,17 @@ public class RubikCubeMovement
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public float continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
+    public float continueRotation(Static4D rotQuaternion, float x, float y)
       {
-      convertTouchPointToScreenSpace(accumulated,x,y);
+      convertTouchPointToScreenSpace(rotQuaternion,x,y);
 
       mDiff[0] = mPoint[0]-mTouchPoint[0];
       mDiff[1] = mPoint[1]-mTouchPoint[1];
       mDiff[2] = mPoint[2]-mTouchPoint[2];
 
-      int xAxis= retFaceXaxis(lastTouchedFace);
-      int yAxis= retFaceYaxis(lastTouchedFace);
-      int sign = retFaceRotationSign(lastTouchedFace);
+      int xAxis= retFaceXaxis(mLastTouchedFace);
+      int yAxis= retFaceYaxis(mLastTouchedFace);
+      int sign = retFaceRotationSign(mLastTouchedFace);
       float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
 
       return sign*angle;
