Project

General

Profile

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

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

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.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
///////////////////////////////////////////////////////////////////////////////////////////////////
18
// Ball-shaped objects: map the 2D swipes of user's fingers to 3D rotations
19

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

    
25
  private final Static3D[] mRotAxis;
26
  private final float[] mPoint, mCamera, mTouch;
27
  private final int[] mEnabledRotAxis;
28
  private final int[][][] mEnabled;
29
  private final float[] mMove2D;
30
  private final float[][] mCastedRotAxis;
31
  private float[][] mTouchBorders;
32

    
33
  private float mLongitude, mLatitude;
34
  private float mX, mY, mZ;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
  public TouchControlBall(TwistyObject object)
39
    {
40
    super(object.getObjectRatio());
41

    
42
    int[] numLayers       = object.getNumLayers();
43
    float[][] cuts        = object.getCuts(numLayers);
44
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
45
    float size            = object.getSize();
46
    mRotAxis = object.getRotationAxis();
47
    mEnabled = object.getEnabled();
48

    
49
    mMove2D  = new float[2];
50

    
51
    mPoint = new float[3];
52
    mCamera= new float[3];
53
    mTouch = new float[3];
54

    
55
    int numRotAxis = mRotAxis.length;
56
    mEnabledRotAxis = new int[numRotAxis+1];
57
    mCastedRotAxis = new float[numRotAxis][2];
58

    
59
    computeBorders(cuts,rotatable,size);
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
65
    {
66
    if( cuts==null ) return null;
67

    
68
    int len = cuts.length;
69
    float[] border = new float[len];
70

    
71
    for(int i=0; i<len; i++)
72
      {
73
      if( !rotatable[i] )
74
        {
75
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
76
        }
77
      else
78
        {
79
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
80
        else
81
          {
82
          int found = -1;
83

    
84
          for(int j=i+2; j<=len; j++)
85
            {
86
            if( rotatable[j] )
87
              {
88
              found=j;
89
              break;
90
              }
91
            }
92

    
93
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
94
          }
95
        }
96
      }
97

    
98
    return border;
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
103

    
104
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
105
    {
106
    int numCuts = cuts.length;
107
    mTouchBorders = new float[numCuts][];
108

    
109
    for(int axis=0; axis<numCuts; axis++)
110
      {
111
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  private int computeRowFromOffset(int axisIndex, float offset)
118
    {
119
    float[] borders = mTouchBorders[axisIndex];
120
    if( borders==null ) return 0;
121
    int len = borders.length;
122

    
123
    for(int i=0; i<len; i++)
124
      {
125
      if( offset<borders[i] ) return i;
126
      }
127

    
128
    return len;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// Longitude spans from 0 (at Guinea Bay) increasing to the east all the way to 2PI
133
// Latitude - from -PI/2 (South Pole) to +PI/2 (North Pole)
134

    
135
  private void computeLongitudeAndLatitude(float A, float B, float C)
136
    {
137
    float sqrt = (float)Math.sqrt(B*B - 4*A*C);
138
    float alpha= (-B+sqrt)/(2*A); // this is the closer point
139

    
140
    float cx = mCamera[0];
141
    float cy = mCamera[1];
142
    float cz = mCamera[2];
143

    
144
    float vx = mCamera[0]-mPoint[0];
145
    float vy = mCamera[1]-mPoint[1];
146
    float vz = mCamera[2]-mPoint[2];
147

    
148
    mX = cx + alpha*vx;
149
    mY = cy + alpha*vy;
150
    mZ = cz + alpha*vz;
151

    
152
    mLongitude = mZ==0 ? 0 : (float)Math.atan(mX/mZ);
153
    mLatitude  = (float)Math.asin(2*mY);
154

    
155
    if( mZ<0 ) mLongitude += Math.PI;
156
    else if( mX<0 ) mLongitude += 2*Math.PI;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// this is Masterball-specific. See TwistyMasterball.getEnabled()
161

    
162
  private int returnTouchedFace()
163
    {
164
    float t = (float)(mLongitude + Math.PI/8);
165
    if( t>2*Math.PI ) t-=(2*Math.PI);
166
    int ret = (int)(t/(Math.PI/4));
167
    return ret<8 ? ret : 7;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
// this is Masterball-specific. No parts in any faces.
172

    
173
  private int returnTouchedPart()
174
    {
175
    return 0;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private float computeOffset(int rotIndex)
181
    {
182
    Static3D axis = mRotAxis[rotIndex];
183
    return mX*axis.get0() + mY*axis.get1() + mZ*axis.get2();
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void computeEnabledAxis()
189
    {
190
    int face = returnTouchedFace();
191
    int part = returnTouchedPart();
192
    int num = mEnabled[face][0].length;
193
    mEnabledRotAxis[0] = num;
194
    System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public float returnRotationFactor(int[] numLayers, int row)
200
    {
201
    return 1.0f;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public int getTouchedCubitFace()
207
    {
208
    return 0;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public int getTouchedCubit()
214
    {
215
    return 0;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
221
    {
222
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
223
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
224
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
225

    
226
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
227
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
228
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
229

    
230
    float vx = mCamera[0]-mPoint[0];
231
    float vy = mCamera[1]-mPoint[1];
232
    float vz = mCamera[2]-mPoint[2];
233

    
234
    float R = 0.5f;
235
    float A = vx*vx + vy*vy + vz*vz;
236
    float B = 2*(vx*mCamera[0] + vy*mCamera[1] + vz*mCamera[2]);
237
    float C = (mCamera[0]*mCamera[0] + mCamera[1]*mCamera[1] + mCamera[2]*mCamera[2]) - R*R;
238

    
239
    if( B*B >= 4*A*C )
240
      {
241
      computeLongitudeAndLatitude(A,B,C);
242
      return true;
243
      }
244

    
245
    return false;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
251
    {
252
    computeEnabledAxis();
253

    
254
    mTouch[0]  = rotatedTouchPoint.get0()/mObjectRatio;
255
    mTouch[1]  = rotatedTouchPoint.get1()/mObjectRatio;
256
    mTouch[2]  = rotatedTouchPoint.get2()/mObjectRatio;
257

    
258
    float x = mTouch[0]-mPoint[0];
259
    float y = mTouch[1]-mPoint[1];
260
    float z = mTouch[2]-mPoint[2];
261

    
262
    QuatHelper.rotateVectorByQuat(mTmp,x,y,z,0,quat);
263
    mMove2D[0] = mTmp[0];
264
    mMove2D[1] = mTmp[1];
265

    
266
    for(int i=1; i<=mEnabledRotAxis[0]; i++)
267
      {
268
      int enabled = mEnabledRotAxis[i];
269
      Static3D axis = mRotAxis[enabled];
270
      float[] vector = mCastedRotAxis[enabled];
271
      float bx = axis.get0();
272
      float by = axis.get1();
273
      float bz = axis.get2();
274

    
275
      QuatHelper.rotateVectorByQuat(mTmp,bx,by,bz,0,quat);
276
      float len = (float)Math.sqrt(mTmp[0]*mTmp[0] + mTmp[1]*mTmp[1]);
277

    
278
      if( len<MIN_LEN )
279
        {
280
        vector[0] = 1000f;  // switch off the axis because when casted
281
        vector[1] = 1000f;  // onto the screen it is too short
282
        }
283
      else
284
        {
285
        vector[0] = mTmp[0]/len;
286
        vector[1] = mTmp[1]/len;
287
        }
288
      }
289

    
290
    int rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabledRotAxis);
291
    float offset = computeOffset(rotIndex);
292
    int row      = computeRowFromOffset(rotIndex,offset);
293

    
294
    output[0] = rotIndex;
295
    output[1] = row;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// simply cast the appropriate rotational axis of the object to the screen surface.
300

    
301
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
302
    {
303
    Static3D a = mRotAxis[axisIndex];
304
    Static4D result = QuatHelper.rotateVectorByQuat(a.get0(),a.get1(),a.get2(),0,quat);
305

    
306
    float cx = result.get0();
307
    float cy = result.get1();
308
    float len= (float)Math.sqrt(cx*cx+cy*cy);
309

    
310
    if( len!=0 )
311
      {
312
      output[0] = cx/len;
313
      output[1] = cy/len;
314
      }
315
    else
316
      {
317
      output[0] = 1;
318
      output[1] = 0;
319
      }
320
    }
321
}
(2-2/13)