Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlBarrel.java @ 09d5cf2b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 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 static org.distorted.objectlib.main.TwistyObject.SQ2;
13

    
14
import org.distorted.library.helpers.QuatHelper;
15
import org.distorted.library.type.Static3D;
16
import org.distorted.library.type.Static4D;
17
import org.distorted.objectlib.main.TwistyObject;
18

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

    
22
public class TouchControlBarrel 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 TouchControlBarrel(TwistyObject object)
64
    {
65
    super(object);
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
    mGhostAxisEnabled = -1;
85

    
86
    computeBorders(cuts,rotatable,size);
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
92
    {
93
    if( cuts==null ) return null;
94

    
95
    int len = cuts.length;
96
    float[] border = new float[len];
97

    
98
    for(int i=0; i<len; i++)
99
      {
100
      if( !rotatable[i] )
101
        {
102
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
103
        }
104
      else
105
        {
106
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
107
        else
108
          {
109
          int found = -1;
110

    
111
          for(int j=i+2; j<=len; j++)
112
            {
113
            if( rotatable[j] )
114
              {
115
              found=j;
116
              break;
117
              }
118
            }
119

    
120
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
121
          }
122
        }
123
      }
124

    
125
    return border;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
130

    
131
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
132
    {
133
    int numCuts = cuts.length;
134
    mTouchBorders = new float[numCuts][];
135

    
136
    for(int axis=0; axis<numCuts; axis++)
137
      {
138
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private int computeRowFromOffset(int axisIndex, float offset)
145
    {
146
    float[] borders = mTouchBorders[axisIndex];
147
    if( borders==null ) return 0;
148
    int len = borders.length;
149

    
150
    for(int i=0; i<len; i++)
151
      {
152
      if( offset<borders[i] ) return i;
153
      }
154

    
155
    return len;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// Longitude spans from 0 (at Guinea Bay) increasing to the east all the way to 2PI
160
// Latitude - from -PI/2 (South Pole) to +PI/2 (North Pole)
161

    
162
  private void computeLongitudeAndLatitude(float A, float B, float C)
163
    {
164
    float sqrt = (float)Math.sqrt(B*B - 4*A*C);
165
    float alpha= (-B+sqrt)/(2*A); // this is the closer point
166

    
167
    float cx = mCamera[0];
168
    float cy = mCamera[1];
169
    float cz = mCamera[2];
170

    
171
    float vx = mCamera[0]-mPoint[0];
172
    float vy = mCamera[1]-mPoint[1];
173
    float vz = mCamera[2]-mPoint[2];
174

    
175
    mX = cx + alpha*vx;
176
    mY = cy + alpha*vy;
177
    mZ = cz + alpha*vz;
178

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

    
182
    if( mZ<0 ) mLongitude += Math.PI;
183
    else if( mX<0 ) mLongitude += 2*Math.PI;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// this is Masterball-specific. See TwistyMasterball.getEnabled()
188

    
189
  private int returnTouchedFace()
190
    {
191
    float t = (float)(mLongitude + Math.PI/8);
192
    if( t>2*Math.PI ) t-=(2*Math.PI);
193
    int ret = (int)(t/(Math.PI/4));
194
    return ret<8 ? ret : 7;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
// this is Masterball-specific. No parts in any faces.
199

    
200
  private int returnTouchedPart()
201
    {
202
    return 0;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private float computeOffset(int rotIndex)
208
    {
209
    Static3D axis = mRotAxis[rotIndex];
210
    return mX*axis.get0() + mY*axis.get1() + mZ*axis.get2();
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  private void computeEnabledAxis()
216
    {
217
    if( mGhostAxisEnabled<0 )
218
      {
219
      int face = returnTouchedFace();
220
      int part = returnTouchedPart();
221
      int num = mEnabled[face][0].length;
222
      mEnabledRotAxis[0] = num;
223
      System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
224
      }
225
    else
226
      {
227
      mEnabledRotAxis[0] = 1;  // if in 'ghost' mode, only one axis is enabled.
228
      mEnabledRotAxis[1] = mGhostAxisEnabled;
229
      }
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public int getTouchedCubitFace()
235
    {
236
    return 0;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public float[] getTouchedPuzzleCenter()
242
    {
243
    return null;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public int getTouchedCubit()
249
    {
250
    return 0;
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
261
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
262
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
263
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
264

    
265
    float vx = mCamera[0]-mPoint[0];
266
    float vy = mCamera[1]-mPoint[1];
267
    float vz = mCamera[2]-mPoint[2];
268

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

    
274
    if( B*B >= 4*A*C )
275
      {
276
      computeLongitudeAndLatitude(A,B,C);
277
      return true;
278
      }
279

    
280
    return false;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
286
    {
287
    computeEnabledAxis();
288

    
289
    mTouch[0]  = rotatedTouchPoint.get0()/mObjectRatio;
290
    mTouch[1]  = rotatedTouchPoint.get1()/mObjectRatio;
291
    mTouch[2]  = rotatedTouchPoint.get2()/mObjectRatio;
292

    
293
    float x = mTouch[0]-mPoint[0];
294
    float y = mTouch[1]-mPoint[1];
295
    float z = mTouch[2]-mPoint[2];
296

    
297
    QuatHelper.rotateVectorByQuat(mTmp,x,y,z,0,quat);
298
    mMove2D[0] = mTmp[0];
299
    mMove2D[1] = mTmp[1];
300

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

    
310
      QuatHelper.rotateVectorByQuat(mTmp,bx,by,bz,0,quat);
311
      float len = (float)Math.sqrt(mTmp[0]*mTmp[0] + mTmp[1]*mTmp[1]);
312

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

    
325
    int rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabledRotAxis);
326
    float offset = computeOffset(rotIndex);
327
    int row      = computeRowFromOffset(rotIndex,offset);
328

    
329
    output[0] = rotIndex;
330
    output[1] = row;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// simply cast the appropriate rotational axis of the object to the screen surface.
335

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

    
341
    float cx = result.get0();
342
    float cy = result.get1();
343
    float len= (float)Math.sqrt(cx*cx+cy*cy);
344

    
345
    if( len!=0 )
346
      {
347
      output[0] = cx/len;
348
      output[1] = cy/len;
349
      }
350
    else
351
      {
352
      output[0] = 1;
353
      output[1] = 0;
354
      }
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  public boolean axisAndFaceAgree(int axisIndex)
360
    {
361
    return false;
362
    }
363
}
(3-3/14)