Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.helpers.QuatHelper;
13
import org.distorted.library.type.Static3D;
14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.main.TwistyObject;
16

    
17
import static org.distorted.objectlib.main.TwistyObject.SQ2;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20
// Ball-shaped objects: map the 2D swipes of user's fingers to 3D rotations
21

    
22
public class TouchControlBall extends TouchControl
23
{
24
  private static final float MIN_LEN = 0.35f;
25
  private static final float[] mTmp = new float[4];
26

    
27
  private final Static3D[] mRotAxis;
28
  private final float[] mPoint, mCamera, mTouch;
29
  private final int[] mEnabledRotAxis;
30
  private final int[][][] mEnabled;
31
  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

    
38
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public TouchControlBall(TwistyObject object)
64
    {
65
    super(object.getObjectRatio());
66

    
67
    int[] numLayers       = object.getNumLayers();
68
    float[][] cuts        = object.getCuts(numLayers);
69
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
70
    float size            = object.getSize();
71
    mRotAxis = object.getRotationAxis();
72
    mEnabled = object.getEnabled();
73

    
74
    mMove2D  = new float[2];
75

    
76
    mPoint = new float[3];
77
    mCamera= new float[3];
78
    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
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// 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

    
160
  private void computeLongitudeAndLatitude(float A, float B, float C)
161
    {
162
    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
    mX = cx + alpha*vx;
174
    mY = cy + alpha*vy;
175
    mZ = cz + alpha*vz;
176

    
177
    mLongitude = mZ==0 ? 0 : (float)Math.atan(mX/mZ);
178
    mLatitude  = (float)Math.asin(2*mY);
179

    
180
    if( mZ<0 ) mLongitude += Math.PI;
181
    else if( mX<0 ) mLongitude += 2*Math.PI;
182
    }
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

    
198
  private int returnTouchedPart()
199
    {
200
    return 0;
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
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 float[] getTouchedPuzzleCenter()
239
    {
240
    return null;
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  public int getTouchedCubit()
246
    {
247
    return 0;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
253
    {
254
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
255
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
256
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
257

    
258
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
259
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
260
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
261

    
262
    float vx = mCamera[0]-mPoint[0];
263
    float vy = mCamera[1]-mPoint[1];
264
    float vz = mCamera[2]-mPoint[2];
265

    
266
    float R = 0.5f;
267
    float A = vx*vx + vy*vy + vz*vz;
268
    float B = 2*(vx*mCamera[0] + vy*mCamera[1] + vz*mCamera[2]);
269
    float C = (mCamera[0]*mCamera[0] + mCamera[1]*mCamera[1] + mCamera[2]*mCamera[2]) - R*R;
270

    
271
    if( B*B >= 4*A*C )
272
      {
273
      computeLongitudeAndLatitude(A,B,C);
274
      return true;
275
      }
276

    
277
    return false;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
283
    {
284
    computeEnabledAxis();
285

    
286
    mTouch[0]  = rotatedTouchPoint.get0()/mObjectRatio;
287
    mTouch[1]  = rotatedTouchPoint.get1()/mObjectRatio;
288
    mTouch[2]  = rotatedTouchPoint.get2()/mObjectRatio;
289

    
290
    float x = mTouch[0]-mPoint[0];
291
    float y = mTouch[1]-mPoint[1];
292
    float z = mTouch[2]-mPoint[2];
293

    
294
    QuatHelper.rotateVectorByQuat(mTmp,x,y,z,0,quat);
295
    mMove2D[0] = mTmp[0];
296
    mMove2D[1] = mTmp[1];
297

    
298
    for(int i=1; i<=mEnabledRotAxis[0]; i++)
299
      {
300
      int enabled = mEnabledRotAxis[i];
301
      Static3D axis = mRotAxis[enabled];
302
      float[] vector = mCastedRotAxis[enabled];
303
      float bx = axis.get0();
304
      float by = axis.get1();
305
      float bz = axis.get2();
306

    
307
      QuatHelper.rotateVectorByQuat(mTmp,bx,by,bz,0,quat);
308
      float len = (float)Math.sqrt(mTmp[0]*mTmp[0] + mTmp[1]*mTmp[1]);
309

    
310
      if( len<MIN_LEN )
311
        {
312
        vector[0] = 1000f;  // switch off the axis because when casted
313
        vector[1] = 1000f;  // onto the screen it is too short
314
        }
315
      else
316
        {
317
        vector[0] = mTmp[0]/len;
318
        vector[1] = mTmp[1]/len;
319
        }
320
      }
321

    
322
    int rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabledRotAxis);
323
    float offset = computeOffset(rotIndex);
324
    int row      = computeRowFromOffset(rotIndex,offset);
325

    
326
    output[0] = rotIndex;
327
    output[1] = row;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// simply cast the appropriate rotational axis of the object to the screen surface.
332

    
333
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
334
    {
335
    Static3D a = mRotAxis[axisIndex];
336
    Static4D result = QuatHelper.rotateVectorByQuat(a.get0(),a.get1(),a.get2(),0,quat);
337

    
338
    float cx = result.get0();
339
    float cy = result.get1();
340
    float len= (float)Math.sqrt(cx*cx+cy*cy);
341

    
342
    if( len!=0 )
343
      {
344
      output[0] = cx/len;
345
      output[1] = cy/len;
346
      }
347
    else
348
      {
349
      output[0] = 1;
350
      output[1] = 0;
351
      }
352
    }
353
}
(2-2/13)