commit 1c7eebdc63efca01f656c07f8600cf2e5efc079a
Author: leszek <leszek@koltunski.pl>
Date:   Fri Apr 12 22:36:58 2024 +0200

    common code from all activities to one BaseActivity

diff --git a/src/main/java/org/distorted/objectlib/touchcontrol/TouchControlBarrel.java b/src/main/java/org/distorted/objectlib/touchcontrol/TouchControlBarrel.java
index 4f2c72d0..fe5fb044 100644
--- a/src/main/java/org/distorted/objectlib/touchcontrol/TouchControlBarrel.java
+++ b/src/main/java/org/distorted/objectlib/touchcontrol/TouchControlBarrel.java
@@ -32,7 +32,7 @@ public class TouchControlBarrel extends TouchControl
   private final float[][] mCastedRotAxis;
   private float[][] mTouchBorders;
 
-  private float mLongitude, mLatitude;
+  private float mRadius, mHeight;
   private float mX, mY, mZ;
 
   private static final float X = (float)Math.sqrt( (2-SQ2)/(6+SQ2) );
@@ -78,7 +78,7 @@ public class TouchControlBarrel extends TouchControl
     mTouch = new float[3];
 
     int numRotAxis = mRotAxis.length;
-    mEnabledRotAxis = new int[numRotAxis+1];
+    mEnabledRotAxis= new int[numRotAxis+1];
     mCastedRotAxis = new float[numRotAxis][2];
 
     mGhostAxisEnabled = -1;
@@ -155,53 +155,6 @@ public class TouchControlBarrel extends TouchControl
     return len;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Longitude spans from 0 (at Guinea Bay) increasing to the east all the way to 2PI
-// Latitude - from -PI/2 (South Pole) to +PI/2 (North Pole)
-
-  private void computeLongitudeAndLatitude(float A, float B, float C)
-    {
-    float sqrt = (float)Math.sqrt(B*B - 4*A*C);
-    float alpha= (-B+sqrt)/(2*A); // this is the closer point
-
-    float cx = mCamera[0];
-    float cy = mCamera[1];
-    float cz = mCamera[2];
-
-    float vx = mCamera[0]-mPoint[0];
-    float vy = mCamera[1]-mPoint[1];
-    float vz = mCamera[2]-mPoint[2];
-
-    mX = cx + alpha*vx;
-    mY = cy + alpha*vy;
-    mZ = cz + alpha*vz;
-
-    mLongitude = mZ==0 ? 0 : (float)Math.atan(mX/mZ);
-    mLatitude  = (float)Math.asin(2*mY);
-
-    if( mZ<0 ) mLongitude += Math.PI;
-    else if( mX<0 ) mLongitude += 2*Math.PI;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// this is Masterball-specific. See TwistyMasterball.getEnabled()
-
-  private int returnTouchedFace()
-    {
-    float t = (float)(mLongitude + Math.PI/8);
-    if( t>2*Math.PI ) t-=(2*Math.PI);
-    int ret = (int)(t/(Math.PI/4));
-    return ret<8 ? ret : 7;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// this is Masterball-specific. No parts in any faces.
-
-  private int returnTouchedPart()
-    {
-    return 0;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private float computeOffset(int rotIndex)
@@ -216,8 +169,8 @@ public class TouchControlBarrel extends TouchControl
     {
     if( mGhostAxisEnabled<0 )
       {
-      int face = returnTouchedFace();
-      int part = returnTouchedPart();
+      int face = 0;//returnTouchedFace();
+      int part = 0;//returnTouchedPart();
       int num = mEnabled[face][0].length;
       mEnabledRotAxis[0] = num;
       System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
@@ -229,6 +182,27 @@ public class TouchControlBarrel extends TouchControl
       }
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private boolean crossesPipe(float radius, float vx, float vy, float vz)
+    {
+    return false;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private boolean crossesPlane(float nx, float ny, float nz, float d, float vx, float vy, float vz)
+    {
+    return false;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public boolean axisAndFaceAgree(int axisIndex)
+    {
+    return false;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public int getTouchedCubitFace()
@@ -250,6 +224,15 @@ public class TouchControlBarrel extends TouchControl
     return 0;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// simply cast the appropriate rotational axis of the object to the screen surface.
+
+  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
+    {
+    Static3D a = mRotAxis[axisIndex];
+    getCastedRotAxis(output,quat,a.get0(),a.get1(),a.get2(),0);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
@@ -265,15 +248,63 @@ public class TouchControlBarrel extends TouchControl
     float vx = mCamera[0]-mPoint[0];
     float vy = mCamera[1]-mPoint[1];
     float vz = mCamera[2]-mPoint[2];
+    float x1=0,y1=0,z1=0;
+    float x2=0,y2=0,z2=0;
+    float x3=0,y3=0,z3=0;
+    float len1=Float.MAX_VALUE;
+    float len2=Float.MAX_VALUE;
+    float len3=Float.MAX_VALUE;
+
+    if( crossesPipe(mRadius,vx,vy,vz) )
+      {
+      x1 = mX;
+      y1 = mY;
+      z1 = mZ;
+      float dx = mX-mCamera[0];
+      float dy = mY-mCamera[1];
+      float dz = mZ-mCamera[2];
+      len1 = dx*dx + dy*dy + dz*dz;
+      }
+    if( crossesPlane(0,1,0,mHeight,vx,vy,vz) )
+      {
+      x2 = mX;
+      y2 = mY;
+      z2 = mZ;
+      float dx = mX-mCamera[0];
+      float dy = mY-mCamera[1];
+      float dz = mZ-mCamera[2];
+      len2 = dx*dx + dy*dy + dz*dz;
+      }
+    if( crossesPlane(0,-1,0,mHeight,vx,vy,vz) )
+      {
+      x3 = mX;
+      y3 = mY;
+      z3 = mZ;
+      float dx = mX-mCamera[0];
+      float dy = mY-mCamera[1];
+      float dz = mZ-mCamera[2];
+      len3 = dx*dx + dy*dy + dz*dz;
+      }
 
-    float R = 0.5f;
-    float A = vx*vx + vy*vy + vz*vz;
-    float B = 2*(vx*mCamera[0] + vy*mCamera[1] + vz*mCamera[2]);
-    float C = (mCamera[0]*mCamera[0] + mCamera[1]*mCamera[1] + mCamera[2]*mCamera[2]) - R*R;
-
-    if( B*B >= 4*A*C )
+    if( len1<len2 && len1<len3 )
       {
-      computeLongitudeAndLatitude(A,B,C);
+      mX = x1;
+      mY = y1;
+      mZ = z1;
+      return true;
+      }
+    if( len2<len1 && len2<len3 )
+      {
+      mX = x2;
+      mY = y2;
+      mZ = z2;
+      return true;
+      }
+    if( len3<len1 && len3<len2 )
+      {
+      mX = x3;
+      mY = y3;
+      mZ = z3;
       return true;
       }
 
@@ -329,35 +360,4 @@ public class TouchControlBarrel extends TouchControl
     output[0] = rotIndex;
     output[1] = row;
     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// simply cast the appropriate rotational axis of the object to the screen surface.
-
-  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
-    {
-    Static3D a = mRotAxis[axisIndex];
-    Static4D result = QuatHelper.rotateVectorByQuat(a.get0(),a.get1(),a.get2(),0,quat);
-
-    float cx = result.get0();
-    float cy = result.get1();
-    float len= (float)Math.sqrt(cx*cx+cy*cy);
-
-    if( len!=0 )
-      {
-      output[0] = cx/len;
-      output[1] = cy/len;
-      }
-    else
-      {
-      output[0] = 1;
-      output[1] = 0;
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public boolean axisAndFaceAgree(int axisIndex)
-    {
-    return false;
-    }
 }
