Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / Movement.java @ a76d9cb4

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
  static final float SQ3 = (float)Math.sqrt(3);
31
  static final float SQ6 = (float)Math.sqrt(6);
32

    
33
  private final int mNumFaceAxis;
34
  private final float[] mPoint, mCamera, mTouch;
35
  private final float[] mPoint2D, mMove2D;
36
  private final int[] mEnabledRotAxis;
37
  private final float mDistanceCenterFace3D;
38
  private final Static3D[] mFaceAxis;
39

    
40
  private int mLastTouchedFace;
41
  private float[][][] mCastedRotAxis;
42
  private Static4D[][] mCastedRotAxis4D;
43
  private float[][] mTouchBorders, mA, mB;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  abstract boolean isInsideFace(int face, float[] point);
48
  abstract void computeEnabledAxis(int face, float[] touchPoint, int[] enabledAxis);
49
  public abstract float returnRotationFactor(int numLayers, int row);
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  Movement(Static3D[] rotAxis, Static3D[] faceAxis, float[][] cuts, boolean[][] rotatable, float distance3D, int size)
54
    {
55
    mPoint = new float[3];
56
    mCamera= new float[3];
57
    mTouch = new float[3];
58

    
59
    mPoint2D = new float[2];
60
    mMove2D  = new float[2];
61

    
62
    mFaceAxis   = faceAxis;
63
    mNumFaceAxis= mFaceAxis.length;
64

    
65
    mEnabledRotAxis = new int[rotAxis.length+1];
66

    
67
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
68

    
69
    computeCastedAxis(rotAxis);
70
    computeBorders(cuts,rotatable,size);
71
    computeLinear(distance3D,rotAxis,faceAxis);
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
76
// 1st faceAxis.
77

    
78
  private void computeCastedAxis(Static3D[] rotAxis)
79
    {
80
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
81
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
82

    
83
    float fx,fy,fz,f;
84

    
85
    for( int casted=0; casted<rotAxis.length; casted++)
86
      {
87
      Static3D a = rotAxis[casted];
88
      mPoint[0]= a.get0();
89
      mPoint[1]= a.get1();
90
      mPoint[2]= a.get2();
91

    
92
      for( int face=0; face<mNumFaceAxis; face++)
93
        {
94
        convertTo2Dcoords( mPoint, mFaceAxis[face], mCastedRotAxis[face][casted]);
95
        normalize2D(mCastedRotAxis[face][casted]);
96

    
97
        fx = mFaceAxis[face].get0();
98
        fy = mFaceAxis[face].get1();
99
        fz = mFaceAxis[face].get2();
100
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
101
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
102
        }
103
      }
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void normalize2D(float[] vect)
109
    {
110
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
111
    vect[0] /= len;
112
    vect[1] /= len;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
117

    
118
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
119
    {
120
    float cosAngle, minCosAngle = Float.MAX_VALUE;
121
    int minIndex=0, index;
122
    float m0 = move2D[0];
123
    float m1 = move2D[1];
124
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
125

    
126
    if( len!=0.0f )
127
      {
128
      m0 /= len;
129
      m1 /= len;
130
      }
131
    else
132
      {
133
      m0 = 1.0f;  // arbitrarily
134
      m1 = 0.0f;  //
135
      }
136

    
137
    int numAxis = enabled[0];
138

    
139
    for(int axis=1; axis<=numAxis; axis++)
140
      {
141
      index = enabled[axis];
142
      cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
143
      if( cosAngle<0 ) cosAngle = -cosAngle;
144

    
145
      if( cosAngle<minCosAngle )
146
        {
147
        minCosAngle=cosAngle;
148
        minIndex = index;
149
        }
150
      }
151

    
152
    return minIndex;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// in the center of the face offset is always 0 regardless of the axis
157

    
158
  private float computeOffset(float[] point, float[] axis)
159
    {
160
    return point[0]*axis[0] + point[1]*axis[1];
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private boolean faceIsVisible(Static3D faceAxis)
166
    {
167
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
168
    return castCameraOnAxis > mDistanceCenterFace3D;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
173
// compute point 'output[]' which:
174
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
175
// 2) is co-linear with mCamera and mPoint
176
//
177
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
178

    
179
  private void castTouchPointOntoFace(Static3D faceAxis, float[] output)
180
    {
181
    float d0 = mPoint[0]-mCamera[0];
182
    float d1 = mPoint[1]-mCamera[1];
183
    float d2 = mPoint[2]-mCamera[2];
184
    float a0 = faceAxis.get0();
185
    float a1 = faceAxis.get1();
186
    float a2 = faceAxis.get2();
187

    
188
    float denom = a0*d0 + a1*d1 + a2*d2;
189

    
190
    if( denom != 0.0f )
191
      {
192
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
193
      float alpha = (mDistanceCenterFace3D-axisCam)/denom;
194

    
195
      output[0] = mCamera[0] + d0*alpha;
196
      output[1] = mCamera[1] + d1*alpha;
197
      output[2] = mCamera[2] + d2*alpha;
198
      }
199
    }
200

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

    
211
  private void convertTo2Dcoords(float[] point3D, Static3D faceAxis, float[] output)
212
    {
213
    float y0,y1,y2; // base Y vector of the 2D coord system
214
    float a0 = faceAxis.get0();
215
    float a1 = faceAxis.get1();
216
    float a2 = faceAxis.get2();
217

    
218
    if( a0==0.0f && a2==0.0f )
219
      {
220
      y0=0; y1=0; y2=-a1;
221
      }
222
    else if( a1==0.0f )
223
      {
224
      y0=0; y1=1; y2=0;
225
      }
226
    else
227
      {
228
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
229
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
230
      }
231

    
232
    float x0 = y1*a2 - y2*a1;  //
233
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
234
    float x2 = y0*a1 - y1*a0;  //
235

    
236
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
237

    
238
    float origin0 = originAlpha*a0; // coords of the point where axis
239
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
240
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
241

    
242
    float v0 = point3D[0] - origin0;
243
    float v1 = point3D[1] - origin1;
244
    float v2 = point3D[2] - origin2;
245

    
246
    output[0] = v0*x0 + v1*x1 + v2*x2;
247
    output[1] = v0*y0 + v1*y1 + v2*y2;
248
    }
249

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

    
252
  private float[] computeBorder(float[] cuts, boolean[] rotatable, int size)
253
    {
254
    int len = cuts.length;
255
    float[] border = new float[len];
256

    
257
    for(int i=0; i<len; i++)
258
      {
259
      if( !rotatable[i] )
260
        {
261
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
262
        }
263
      else
264
        {
265
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
266
        else
267
          {
268
          int found = -1;
269

    
270
          for(int j=i+2; j<=len; j++)
271
            {
272
            if( rotatable[j] )
273
              {
274
              found=j;
275
              break;
276
              }
277
            }
278

    
279
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
280
          }
281
        }
282
      }
283

    
284
    return border;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// size, not numLayers (see Master Skewb where size!=numLayers)
289

    
290
  void computeBorders(float[][] cuts, boolean[][] rotatable, int size)
291
    {
292
    int numCuts = cuts.length;
293
    mTouchBorders = new float[numCuts][];
294

    
295
    for(int i=0; i<numCuts; i++)
296
      {
297
      mTouchBorders[i] = computeBorder(cuts[i],rotatable[i],size);
298
      }
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  private int computeSign(Static3D a, Static3D b)
304
    {
305
    float a1 = a.get0();
306
    float a2 = a.get1();
307
    float a3 = a.get2();
308
    float b1 = b.get0();
309
    float b2 = b.get1();
310
    float b3 = b.get2();
311

    
312
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

    
326
    float x1 = a2*b3-a3*b2;
327
    float x2 = a3*b1-a1*b3;
328
    float x3 = a1*b2-a2*b1;
329

    
330
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
331
    }
332

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

    
338
  private void computeLinear(float distance3D, Static3D[] rotAxis, Static3D[] faceAxis)
339
    {
340
    int numFaces = faceAxis.length;
341
    int numRot   = rotAxis.length;
342

    
343
    mA = new float[numFaces][numRot];
344
    mB = new float[numFaces][numRot];
345

    
346
    for(int i=0; i<numFaces; i++)
347
      for(int j=0; j<numRot; j++)
348
        {
349
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
350

    
351
        if( mA[i][j]!=0.0f )
352
          {
353
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
354
          int sign = computeSign(faceAxis[i],rotAxis[j]);
355
          mB[i][j] = sign*distance3D*coeff;
356
          }
357
        else mB[i][j] = 0.0f;
358
        }
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
  private int computeRowFromOffset(int face, int axisIndex, float offset)
364
    {
365
    float[] borders = mTouchBorders[axisIndex];
366
    int len = borders.length;
367
    float A = mA[face][axisIndex];
368

    
369
    if( A!=0.0f )
370
      {
371
      float B = mB[face][axisIndex];
372

    
373
      for(int i=0; i<len; i++)
374
        {
375
        float translated = B + borders[i]/A;
376
        if( offset<translated ) return i;
377
        }
378
      }
379

    
380
    return len;
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384
// PUBLIC API
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera, float objectRatio)
388
    {
389
    mPoint[0]  = rotatedTouchPoint.get0()/objectRatio;
390
    mPoint[1]  = rotatedTouchPoint.get1()/objectRatio;
391
    mPoint[2]  = rotatedTouchPoint.get2()/objectRatio;
392

    
393
    mCamera[0] = rotatedCamera.get0()/objectRatio;
394
    mCamera[1] = rotatedCamera.get1()/objectRatio;
395
    mCamera[2] = rotatedCamera.get2()/objectRatio;
396

    
397
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
398
      {
399
      if( faceIsVisible(mFaceAxis[mLastTouchedFace]) )
400
        {
401
        castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
402
        convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mPoint2D);
403
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
404
        }
405
      }
406

    
407
    return false;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  public Static2D newRotation(Static4D rotatedTouchPoint, float objectRatio)
413
    {
414
    mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
415
    mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
416
    mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
417

    
418
    castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
419
    convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mMove2D);
420

    
421
    mMove2D[0] -= mPoint2D[0];
422
    mMove2D[1] -= mPoint2D[1];
423

    
424
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
425
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
426
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
427
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
428

    
429
    return new Static2D(rotIndex,row);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  public Static4D getCastedRotAxis(int rotIndex)
435
    {
436
    return mCastedRotAxis4D[mLastTouchedFace][rotIndex];
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  public int getTouchedFace()
442
    {
443
    return mLastTouchedFace;
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public float[] getTouchedPoint3D()
449
    {
450
    return mTouch;
451
    }
452
  }
(2-2/48)