Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlBall.java @ a7c72edb

1 5caf2641 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.touchcontrol;
11
12
import org.distorted.library.main.QuatHelper;
13
import org.distorted.library.type.Static3D;
14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.main.TwistyObject;
16
17 b63235a0 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.SQ2;
18
19 5caf2641 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
// Ball-shaped objects: map the 2D swipes of user's fingers to 3D rotations
21
22
public class TouchControlBall extends TouchControl
23
{
24 bfdb9aa5 Leszek Koltunski
  private static final float MIN_LEN = 0.35f;
25
  private static final float[] mTmp = new float[4];
26
27 5caf2641 Leszek Koltunski
  private final Static3D[] mRotAxis;
28 bfdb9aa5 Leszek Koltunski
  private final float[] mPoint, mCamera, mTouch;
29 b31249d6 Leszek Koltunski
  private final int[] mEnabledRotAxis;
30
  private final int[][][] mEnabled;
31 bfdb9aa5 Leszek Koltunski
  private final float[] mMove2D;
32
  private final float[][] mCastedRotAxis;
33
  private float[][] mTouchBorders;
34
35
  private float mLongitude, mLatitude;
36
  private float mX, mY, mZ;
37 5caf2641 Leszek Koltunski
38 b63235a0 Leszek Koltunski
  private static final float X = (float)Math.sqrt( (2-SQ2)/(6+SQ2) );
39
  private static final float Y = (float)Math.sqrt( (2+SQ2)/(6+SQ2) );
40
41
  public static final Static3D[] FACE_AXIS = new Static3D[]
42
        {
43
          new Static3D( X, Y, Y),
44
          new Static3D( X,-Y, Y),
45
          new Static3D( Y, Y, X),
46
          new Static3D( Y,-Y, X),
47
          new Static3D( Y, Y,-X),
48
          new Static3D( Y,-Y,-X),
49
          new Static3D( X, Y,-Y),
50
          new Static3D( X,-Y,-Y),
51
          new Static3D(-X, Y,-Y),
52
          new Static3D(-X,-Y,-Y),
53
          new Static3D(-Y, Y,-X),
54
          new Static3D(-Y,-Y,-X),
55
          new Static3D(-Y, Y, X),
56
          new Static3D(-Y,-Y, X),
57
          new Static3D(-X, Y, Y),
58
          new Static3D(-X,-Y, Y),
59
        };
60
61 5caf2641 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63
  public TouchControlBall(TwistyObject object)
64
    {
65
    super(object.getObjectRatio());
66 a8efe86f Leszek Koltunski
67 bfdb9aa5 Leszek Koltunski
    int[] numLayers       = object.getNumLayers();
68
    float[][] cuts        = object.getCuts(numLayers);
69
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
70
    float size            = object.getSize();
71 5caf2641 Leszek Koltunski
    mRotAxis = object.getRotationAxis();
72 b31249d6 Leszek Koltunski
    mEnabled = object.getEnabled();
73
74 bfdb9aa5 Leszek Koltunski
    mMove2D  = new float[2];
75
76 a8efe86f Leszek Koltunski
    mPoint = new float[3];
77
    mCamera= new float[3];
78 bfdb9aa5 Leszek Koltunski
    mTouch = new float[3];
79
80
    int numRotAxis = mRotAxis.length;
81
    mEnabledRotAxis = new int[numRotAxis+1];
82
    mCastedRotAxis = new float[numRotAxis][2];
83
84
    computeBorders(cuts,rotatable,size);
85
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
90
    {
91
    if( cuts==null ) return null;
92
93
    int len = cuts.length;
94
    float[] border = new float[len];
95
96
    for(int i=0; i<len; i++)
97
      {
98
      if( !rotatable[i] )
99
        {
100
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
101
        }
102
      else
103
        {
104
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
105
        else
106
          {
107
          int found = -1;
108
109
          for(int j=i+2; j<=len; j++)
110
            {
111
            if( rotatable[j] )
112
              {
113
              found=j;
114
              break;
115
              }
116
            }
117
118
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
119
          }
120
        }
121
      }
122
123
    return border;
124
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
128
129
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
130
    {
131
    int numCuts = cuts.length;
132
    mTouchBorders = new float[numCuts][];
133
134
    for(int axis=0; axis<numCuts; axis++)
135
      {
136
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
137
      }
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  private int computeRowFromOffset(int axisIndex, float offset)
143
    {
144
    float[] borders = mTouchBorders[axisIndex];
145
    if( borders==null ) return 0;
146
    int len = borders.length;
147
148
    for(int i=0; i<len; i++)
149
      {
150
      if( offset<borders[i] ) return i;
151
      }
152
153
    return len;
154 5caf2641 Leszek Koltunski
    }
155
156 54588439 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157 b31249d6 Leszek Koltunski
// Longitude spans from 0 (at Guinea Bay) increasing to the east all the way to 2PI
158
// Latitude - from -PI/2 (South Pole) to +PI/2 (North Pole)
159 54588439 Leszek Koltunski
160
  private void computeLongitudeAndLatitude(float A, float B, float C)
161
    {
162 b31249d6 Leszek Koltunski
    float sqrt = (float)Math.sqrt(B*B - 4*A*C);
163
    float alpha= (-B+sqrt)/(2*A); // this is the closer point
164
165
    float cx = mCamera[0];
166
    float cy = mCamera[1];
167
    float cz = mCamera[2];
168
169
    float vx = mCamera[0]-mPoint[0];
170
    float vy = mCamera[1]-mPoint[1];
171
    float vz = mCamera[2]-mPoint[2];
172
173 bfdb9aa5 Leszek Koltunski
    mX = cx + alpha*vx;
174
    mY = cy + alpha*vy;
175
    mZ = cz + alpha*vz;
176 b31249d6 Leszek Koltunski
177 bfdb9aa5 Leszek Koltunski
    mLongitude = mZ==0 ? 0 : (float)Math.atan(mX/mZ);
178
    mLatitude  = (float)Math.asin(2*mY);
179 b31249d6 Leszek Koltunski
180 bfdb9aa5 Leszek Koltunski
    if( mZ<0 ) mLongitude += Math.PI;
181
    else if( mX<0 ) mLongitude += 2*Math.PI;
182 b31249d6 Leszek Koltunski
    }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// this is Masterball-specific. See TwistyMasterball.getEnabled()
186
187
  private int returnTouchedFace()
188
    {
189
    float t = (float)(mLongitude + Math.PI/8);
190
    if( t>2*Math.PI ) t-=(2*Math.PI);
191
    int ret = (int)(t/(Math.PI/4));
192
    return ret<8 ? ret : 7;
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
// this is Masterball-specific. No parts in any faces.
197 54588439 Leszek Koltunski
198 b31249d6 Leszek Koltunski
  private int returnTouchedPart()
199
    {
200
    return 0;
201
    }
202
203 bfdb9aa5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
  private float computeOffset(int rotIndex)
206
    {
207
    Static3D axis = mRotAxis[rotIndex];
208
    return mX*axis.get0() + mY*axis.get1() + mZ*axis.get2();
209
    }
210
211 b31249d6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
  private void computeEnabledAxis()
214
    {
215
    int face = returnTouchedFace();
216
    int part = returnTouchedPart();
217
    int num = mEnabled[face][0].length;
218
    mEnabledRotAxis[0] = num;
219
    System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
220 54588439 Leszek Koltunski
    }
221
222 5caf2641 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224
  public float returnRotationFactor(int[] numLayers, int row)
225
    {
226
    return 1.0f;
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  public int getTouchedCubitFace()
232
    {
233
    return 0;
234
    }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
  public int getTouchedCubit()
239
    {
240
    return 0;
241
    }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
246
    {
247 a8efe86f Leszek Koltunski
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
248
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
249
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
250
251
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
252
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
253
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
254
255 b31249d6 Leszek Koltunski
    float vx = mCamera[0]-mPoint[0];
256
    float vy = mCamera[1]-mPoint[1];
257
    float vz = mCamera[2]-mPoint[2];
258 a8efe86f Leszek Koltunski
259 b31249d6 Leszek Koltunski
    float R = 0.5f;
260
    float A = vx*vx + vy*vy + vz*vz;
261
    float B = 2*(vx*mCamera[0] + vy*mCamera[1] + vz*mCamera[2]);
262
    float C = (mCamera[0]*mCamera[0] + mCamera[1]*mCamera[1] + mCamera[2]*mCamera[2]) - R*R;
263 54588439 Leszek Koltunski
264 f2259427 Leszek Koltunski
    if( B*B >= 4*A*C )
265 54588439 Leszek Koltunski
      {
266
      computeLongitudeAndLatitude(A,B,C);
267
      return true;
268
      }
269 a8efe86f Leszek Koltunski
270 54588439 Leszek Koltunski
    return false;
271 5caf2641 Leszek Koltunski
    }
272
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
276
    {
277 b31249d6 Leszek Koltunski
    computeEnabledAxis();
278 bfdb9aa5 Leszek Koltunski
279
    mTouch[0]  = rotatedTouchPoint.get0()/mObjectRatio;
280
    mTouch[1]  = rotatedTouchPoint.get1()/mObjectRatio;
281
    mTouch[2]  = rotatedTouchPoint.get2()/mObjectRatio;
282
283
    float x = mTouch[0]-mPoint[0];
284
    float y = mTouch[1]-mPoint[1];
285
    float z = mTouch[2]-mPoint[2];
286
287
    QuatHelper.rotateVectorByQuat(mTmp,x,y,z,0,quat);
288
    mMove2D[0] = mTmp[0];
289
    mMove2D[1] = mTmp[1];
290
291
    for(int i=1; i<=mEnabledRotAxis[0]; i++)
292
      {
293
      int enabled = mEnabledRotAxis[i];
294
      Static3D axis = mRotAxis[enabled];
295
      float[] vector = mCastedRotAxis[enabled];
296
      float bx = axis.get0();
297
      float by = axis.get1();
298
      float bz = axis.get2();
299
300
      QuatHelper.rotateVectorByQuat(mTmp,bx,by,bz,0,quat);
301
      float len = (float)Math.sqrt(mTmp[0]*mTmp[0] + mTmp[1]*mTmp[1]);
302
303
      if( len<MIN_LEN )
304
        {
305
        vector[0] = 1000f;  // switch off the axis because when casted
306
        vector[1] = 1000f;  // onto the screen it is too short
307
        }
308
      else
309
        {
310
        vector[0] = mTmp[0]/len;
311
        vector[1] = mTmp[1]/len;
312
        }
313
      }
314
315
    int rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabledRotAxis);
316
    float offset = computeOffset(rotIndex);
317
    int row      = computeRowFromOffset(rotIndex,offset);
318 5caf2641 Leszek Koltunski
319 a8efe86f Leszek Koltunski
    output[0] = rotIndex;
320
    output[1] = row;
321 5caf2641 Leszek Koltunski
    }
322
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
// simply cast the appropriate rotational axis of the object to the screen surface.
325
326
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
327
    {
328
    Static3D a = mRotAxis[axisIndex];
329
    Static4D result = QuatHelper.rotateVectorByQuat(a.get0(),a.get1(),a.get2(),0,quat);
330
331
    float cx = result.get0();
332
    float cy = result.get1();
333
    float len= (float)Math.sqrt(cx*cx+cy*cy);
334
335
    if( len!=0 )
336
      {
337
      output[0] = cx/len;
338
      output[1] = cy/len;
339
      }
340
    else
341
      {
342
      output[0] = 1;
343
      output[1] = 0;
344
      }
345
    }
346
}