Project

General

Profile

Download (16.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / Movement.java @ 967c1d17

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import org.distorted.library.type.Static2D;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public abstract class Movement
29
  {
30
  // it doesn't matter where we touch a face - the list of enabled rotAxis will always be the same
31
  static final int TYPE_NOT_SPLIT    = 0;
32
  // each face is split into several parts by lines coming from its center to the midpoints of each edge
33
  static final int TYPE_SPLIT_EDGE   = 1;
34
  // each face is split into several parts by lines coming from its center to the vertices
35
  static final int TYPE_SPLIT_CORNER = 2;
36

    
37
  static final float SQ3 = (float)Math.sqrt(3);
38
  static final float SQ6 = (float)Math.sqrt(6);
39

    
40
  private final int mNumFaceAxis;
41
  private final float[] mPoint, mCamera, mTouch;
42
  private final float[] mPoint2D, mMove2D;
43
  private final int[] mEnabledRotAxis;
44
  private final float mDistanceCenterFace3D;
45
  private final Static3D[] mFaceAxis;
46

    
47
  private int mLastTouchedFace;
48
  private float[][][] mCastedRotAxis;
49
  private Static4D[][] mCastedRotAxis4D;
50
  private float[][] mTouchBorders, mA, mB;
51

    
52
  private final int mType;
53
  private final int[] mNumEnabled;
54
  private final int[][][] mEnabled;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  abstract int returnPart(int type, int face, float[] touchPoint);
59
  abstract boolean isInsideFace(int face, float[] point);
60
  public abstract float returnRotationFactor(int numLayers, int row);
61

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

    
64
  Movement(Static3D[] rotAxis, Static3D[] faceAxis, float[][] cuts, boolean[][] rotatable,
65
           float distance3D, int size, int type, int[] numEnabled, int[][][] enabled)
66
    {
67
    mPoint = new float[3];
68
    mCamera= new float[3];
69
    mTouch = new float[3];
70

    
71
    mPoint2D = new float[2];
72
    mMove2D  = new float[2];
73

    
74
    mType = type;
75
    mNumEnabled = numEnabled;
76
    mEnabled = enabled;
77

    
78
    mFaceAxis   = faceAxis;
79
    mNumFaceAxis= mFaceAxis.length;
80

    
81
    mEnabledRotAxis = new int[rotAxis.length+1];
82

    
83
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
84

    
85
    computeCastedAxis(rotAxis);
86
    computeBorders(cuts,rotatable,size);
87
    computeLinear(distance3D,rotAxis,faceAxis);
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
92
// 1st faceAxis.
93

    
94
  private void computeCastedAxis(Static3D[] rotAxis)
95
    {
96
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
97
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
98

    
99
    float fx,fy,fz,f;
100

    
101
    for( int casted=0; casted<rotAxis.length; casted++)
102
      {
103
      Static3D a = rotAxis[casted];
104
      mPoint[0]= a.get0();
105
      mPoint[1]= a.get1();
106
      mPoint[2]= a.get2();
107

    
108
      for( int face=0; face<mNumFaceAxis; face++)
109
        {
110
        convertTo2Dcoords( mPoint, mFaceAxis[face], mCastedRotAxis[face][casted]);
111
        normalize2D(mCastedRotAxis[face][casted]);
112

    
113
        fx = mFaceAxis[face].get0();
114
        fy = mFaceAxis[face].get1();
115
        fz = mFaceAxis[face].get2();
116
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
117
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
118
        }
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void normalize2D(float[] vect)
125
    {
126
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
127
    vect[0] /= len;
128
    vect[1] /= len;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
133

    
134
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
135
    {
136
    float cosAngle, minCosAngle = Float.MAX_VALUE;
137
    int minIndex=0, index;
138
    float m0 = move2D[0];
139
    float m1 = move2D[1];
140
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
141

    
142
    if( len!=0.0f )
143
      {
144
      m0 /= len;
145
      m1 /= len;
146
      }
147
    else
148
      {
149
      m0 = 1.0f;  // arbitrarily
150
      m1 = 0.0f;  //
151
      }
152

    
153
    int numAxis = enabled[0];
154

    
155
    for(int axis=1; axis<=numAxis; axis++)
156
      {
157
      index = enabled[axis];
158
      cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
159
      if( cosAngle<0 ) cosAngle = -cosAngle;
160

    
161
      if( cosAngle<minCosAngle )
162
        {
163
        minCosAngle=cosAngle;
164
        minIndex = index;
165
        }
166
      }
167

    
168
    return minIndex;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// in the center of the face offset is always 0 regardless of the axis
173

    
174
  private float computeOffset(float[] point, float[] axis)
175
    {
176
    return point[0]*axis[0] + point[1]*axis[1];
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private boolean faceIsVisible(Static3D faceAxis)
182
    {
183
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
184
    return castCameraOnAxis > mDistanceCenterFace3D;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
189
// compute point 'output[]' which:
190
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
191
// 2) is co-linear with mCamera and mPoint
192
//
193
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
194

    
195
  private void castTouchPointOntoFace(Static3D faceAxis, float[] output)
196
    {
197
    float d0 = mPoint[0]-mCamera[0];
198
    float d1 = mPoint[1]-mCamera[1];
199
    float d2 = mPoint[2]-mCamera[2];
200
    float a0 = faceAxis.get0();
201
    float a1 = faceAxis.get1();
202
    float a2 = faceAxis.get2();
203

    
204
    float denom = a0*d0 + a1*d1 + a2*d2;
205

    
206
    if( denom != 0.0f )
207
      {
208
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
209
      float alpha = (mDistanceCenterFace3D-axisCam)/denom;
210

    
211
      output[0] = mCamera[0] + d0*alpha;
212
      output[1] = mCamera[1] + d1*alpha;
213
      output[2] = mCamera[2] + d2*alpha;
214
      }
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
219
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
220
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
221
// original 3D Y axis and our 2D in-plane origin.
222
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
223
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
224
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
225
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
226

    
227
  private void convertTo2Dcoords(float[] point3D, Static3D faceAxis, float[] output)
228
    {
229
    float y0,y1,y2; // base Y vector of the 2D coord system
230
    float a0 = faceAxis.get0();
231
    float a1 = faceAxis.get1();
232
    float a2 = faceAxis.get2();
233

    
234
    if( a0==0.0f && a2==0.0f )
235
      {
236
      y0=0; y1=0; y2=-a1;
237
      }
238
    else if( a1==0.0f )
239
      {
240
      y0=0; y1=1; y2=0;
241
      }
242
    else
243
      {
244
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
245
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
246
      }
247

    
248
    float x0 = y1*a2 - y2*a1;  //
249
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
250
    float x2 = y0*a1 - y1*a0;  //
251

    
252
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
253

    
254
    float origin0 = originAlpha*a0; // coords of the point where axis
255
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
256
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
257

    
258
    float v0 = point3D[0] - origin0;
259
    float v1 = point3D[1] - origin1;
260
    float v2 = point3D[2] - origin2;
261

    
262
    output[0] = v0*x0 + v1*x1 + v2*x2;
263
    output[1] = v0*y0 + v1*y1 + v2*y2;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  private float[] computeBorder(float[] cuts, boolean[] rotatable, int size)
269
    {
270
    int len = cuts.length;
271
    float[] border = new float[len];
272

    
273
    for(int i=0; i<len; i++)
274
      {
275
      if( !rotatable[i] )
276
        {
277
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
278
        }
279
      else
280
        {
281
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
282
        else
283
          {
284
          int found = -1;
285

    
286
          for(int j=i+2; j<=len; j++)
287
            {
288
            if( rotatable[j] )
289
              {
290
              found=j;
291
              break;
292
              }
293
            }
294

    
295
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
296
          }
297
        }
298
      }
299

    
300
    return border;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
// size, not numLayers (see Master Skewb where size!=numLayers)
305

    
306
  void computeBorders(float[][] cuts, boolean[][] rotatable, int size)
307
    {
308
    int numCuts = cuts.length;
309
    mTouchBorders = new float[numCuts][];
310

    
311
    for(int i=0; i<numCuts; i++)
312
      {
313
      mTouchBorders[i] = computeBorder(cuts[i],rotatable[i],size);
314
      }
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  private int computeSign(Static3D a, Static3D b)
320
    {
321
    float a1 = a.get0();
322
    float a2 = a.get1();
323
    float a3 = a.get2();
324
    float b1 = b.get0();
325
    float b2 = b.get1();
326
    float b3 = b.get2();
327

    
328
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  private float crossProductLen(Static3D a, Static3D b)
334
    {
335
    float a1 = a.get0();
336
    float a2 = a.get1();
337
    float a3 = a.get2();
338
    float b1 = b.get0();
339
    float b2 = b.get1();
340
    float b3 = b.get2();
341

    
342
    float x1 = a2*b3-a3*b2;
343
    float x2 = a3*b1-a1*b3;
344
    float x3 = a1*b2-a2*b1;
345

    
346
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
// compute the array of 'A' and 'B' coeffs of the Ax+B linear function by which we need to multiply
351
// the 3D 'cuts' to translate it from 3D (i.e. with respect to the rotAxis) to 2D in-face (i.e. with
352
// respect to the 2D rotAxis cast into a particular face)
353

    
354
  private void computeLinear(float distance3D, Static3D[] rotAxis, Static3D[] faceAxis)
355
    {
356
    int numFaces = faceAxis.length;
357
    int numRot   = rotAxis.length;
358

    
359
    mA = new float[numFaces][numRot];
360
    mB = new float[numFaces][numRot];
361

    
362
    for(int i=0; i<numFaces; i++)
363
      for(int j=0; j<numRot; j++)
364
        {
365
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
366

    
367
        if( mA[i][j]!=0.0f )
368
          {
369
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
370
          int sign = computeSign(faceAxis[i],rotAxis[j]);
371
          mB[i][j] = sign*distance3D*coeff;
372
          }
373
        else mB[i][j] = 0.0f;
374
        }
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  private int computeRowFromOffset(int face, int axisIndex, float offset)
380
    {
381
    float[] borders = mTouchBorders[axisIndex];
382
    int len = borders.length;
383
    float A = mA[face][axisIndex];
384

    
385
    if( A!=0.0f )
386
      {
387
      float B = mB[face][axisIndex];
388

    
389
      for(int i=0; i<len; i++)
390
        {
391
        float translated = B + borders[i]/A;
392
        if( offset<translated ) return i;
393
        }
394
      }
395

    
396
    return len;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
402
    {
403
    int part = returnPart(mType,face,touchPoint);
404

    
405
    int num = mNumEnabled[face];
406
    enabled[0] = num;
407
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
// PUBLIC API
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera, float objectRatio)
415
    {
416
    mPoint[0]  = rotatedTouchPoint.get0()/objectRatio;
417
    mPoint[1]  = rotatedTouchPoint.get1()/objectRatio;
418
    mPoint[2]  = rotatedTouchPoint.get2()/objectRatio;
419

    
420
    mCamera[0] = rotatedCamera.get0()/objectRatio;
421
    mCamera[1] = rotatedCamera.get1()/objectRatio;
422
    mCamera[2] = rotatedCamera.get2()/objectRatio;
423

    
424
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
425
      {
426
      if( faceIsVisible(mFaceAxis[mLastTouchedFace]) )
427
        {
428
        castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
429
        convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mPoint2D);
430
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
431
        }
432
      }
433

    
434
    return false;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public Static2D newRotation(Static4D rotatedTouchPoint, float objectRatio)
440
    {
441
    mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
442
    mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
443
    mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
444

    
445
    castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
446
    convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mMove2D);
447

    
448
    mMove2D[0] -= mPoint2D[0];
449
    mMove2D[1] -= mPoint2D[1];
450

    
451
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
452
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
453
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
454
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
455

    
456
    return new Static2D(rotIndex,row);
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  public Static4D getCastedRotAxis(int rotIndex)
462
    {
463
    return mCastedRotAxis4D[mLastTouchedFace][rotIndex];
464
    }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
  public int getTouchedFace()
469
    {
470
    return mLastTouchedFace;
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
  public float[] getTouchedPoint3D()
476
    {
477
    return mTouch;
478
    }
479
  }
(2-2/38)