Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObjectMovement.java @ 39e74052

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.object;
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 RubikObjectMovement
29
  {
30
  int mRotationVect, mLastTouchedAxis;
31

    
32
  private float[] mPoint, mCamera, mDiff, mTouch;
33
  private float[] mPoint2D, mMove2D;
34
  private float[][][] mCastAxis;
35
  private int mLastTouchedLR;
36
  private int mNumAxis, mNumFacesPerAxis;
37
  private int[] mPossible;
38
  private float mDistanceCenterFace3D, mDistanceCenterFace2D;
39
  private Static3D[] mAxis;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  abstract boolean isInsideFace(float[] point);
44
  abstract float returnAngle(float[] vect, int[] possible);
45
  abstract void fillPossibleRotations(int axis, int[] output);
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  RubikObjectMovement(Static3D[] axis, int numFacesPerAxis, float distance3D, float distance2D)
50
    {
51
    mPoint = new float[3];
52
    mCamera= new float[3];
53
    mDiff  = new float[3];
54
    mTouch = new float[3];
55

    
56
    mPoint2D = new float[2];
57
    mMove2D  = new float[2];
58

    
59
    mAxis = axis;
60
    mNumAxis = mAxis.length;
61
    mNumFacesPerAxis = numFacesPerAxis;
62
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
63
    mDistanceCenterFace2D = distance2D; // distance from the center of a face to its edge
64
    mPossible = new int[mNumAxis-1];
65

    
66
    // mCastAxis[1][2]{0,1} are the 2D coords of the 2nd axis cast onto the face defined by the
67
    // 1st pair (axis,lr)
68
    mCastAxis = new float[mNumAxis*mNumFacesPerAxis][mNumAxis][2];
69

    
70
    for( int casted=0; casted<mNumAxis; casted++)
71
      {
72
      Static3D a = mAxis[casted];
73
      mPoint[0]= a.get0();
74
      mPoint[1]= a.get1();
75
      mPoint[2]= a.get2();
76

    
77
      for( int surface=0; surface<mNumAxis; surface++)
78
        for(int lr=0; lr<mNumFacesPerAxis; lr++)
79
          {
80
          int index = surface*mNumFacesPerAxis + lr;
81

    
82
          if( casted!=surface )
83
            {
84
            convertTo2Dcoords( mPoint, mAxis[surface], lr, mPoint2D);
85
            mCastAxis[index][casted][0] = mPoint2D[0];
86
            mCastAxis[index][casted][1] = mPoint2D[1];
87
            normalize2D(mCastAxis[index][casted]);
88
            }
89
          else
90
            {
91
            mCastAxis[index][casted][0] = 0;
92
            mCastAxis[index][casted][1] = 0;
93
            }
94
          }
95
      }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void normalize2D(float[] vect)
101
    {
102
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
103
    vect[0] /= len;
104
    vect[1] /= len;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
109

    
110
  private int computeRotationVect(int axis, int lr, float[] move2D)
111
    {
112
    float cosAngle, minCosAngle = Float.MAX_VALUE;
113
    int minIndex=-1;
114
    int index = axis*mNumFacesPerAxis + lr;
115
    float m0 = move2D[0];
116
    float m1 = move2D[1];
117
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
118
    m0 /= len;
119
    m1 /= len;
120

    
121
    for(int i=0; i<mNumAxis; i++)
122
      {
123
      if( axis != i )
124
        {
125
        cosAngle = m0*mCastAxis[index][i][0] +  m1*mCastAxis[index][i][1];
126
        if( cosAngle<0 ) cosAngle = -cosAngle;
127

    
128
        if( cosAngle<minCosAngle )
129
          {
130
          minCosAngle=cosAngle;
131
          minIndex = i;
132
          }
133
        }
134
      }
135

    
136
    return minIndex;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private float computeOffset(float[] point, float[] axis)
142
    {
143
    return point[0]*axis[0] + point[1]*axis[1] + mDistanceCenterFace2D;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private boolean faceIsVisible(Static3D axis, int lr)
149
    {
150
    float castCameraOnAxis = mCamera[0]*axis.get0() + mCamera[1]*axis.get1() + mCamera[2]*axis.get2();
151
    return (2*lr-1)*castCameraOnAxis > mDistanceCenterFace3D;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
156
// compute point 'output[]' which:
157
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0)) [and this
158
//    distance is +-mDistanceCenterFace, depending if it is the face on the left or the right end of
159
//    the axis] (lr=0 or 1, so (2lr-1)*mDistanceCenterFace)
160
// 2) is co-linear with mCamera and mPoint
161
//
162
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
163

    
164
  private void castTouchPointOntoFace(Static3D axis, int lr, float[] output)
165
    {
166
    float d0 = mPoint[0]-mCamera[0];
167
    float d1 = mPoint[1]-mCamera[1];
168
    float d2 = mPoint[2]-mCamera[2];
169
    float a0 = axis.get0();
170
    float a1 = axis.get1();
171
    float a2 = axis.get2();
172

    
173
    float denom = a0*d0 + a1*d1 + a2*d2;
174

    
175
    if( denom != 0.0f )
176
      {
177
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
178
      float distance = (2*lr-1)*mDistanceCenterFace3D;
179
      float alpha = (distance-axisCam)/denom;
180

    
181
      output[0] = mCamera[0] + d0*alpha;
182
      output[1] = mCamera[1] + d1*alpha;
183
      output[2] = mCamera[2] + d2*alpha;
184
      }
185
    }
186

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

    
197
  private void convertTo2Dcoords(float[] point3D, Static3D axis, int lr, float[] output)
198
    {
199
    float y0,y1,y2; // base Y vector of the 2D coord system
200
    float a0 = axis.get0();
201
    float a1 = axis.get1();
202
    float a2 = axis.get2();
203

    
204
    if( lr==0 )
205
      {
206
      a0=-a0; a1=-a1; a2=-a2;
207
      }
208

    
209
    if( a0==0.0f && a2==0.0f )
210
      {
211
      y0=0; y1=0; y2=-a1;
212
      }
213
    else if( a1==0.0f )
214
      {
215
      y0=0; y1=1; y2=0;
216
      }
217
    else
218
      {
219
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
220
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
221
      }
222

    
223
    float x0 = y1*a2 - y2*a1;  //
224
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
225
    float x2 = y0*a1 - y1*a0;  //
226

    
227
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
228

    
229
    float origin0 = originAlpha*a0; // coords of the point where axis
230
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
231
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
232

    
233
    float v0 = point3D[0] - origin0;
234
    float v1 = point3D[1] - origin1;
235
    float v2 = point3D[2] - origin2;
236

    
237
    output[0] = v0*x0 + v1*x1 + v2*x2;
238
    output[1] = v0*y0 + v1*y1 + v2*y2;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private String getFaceColor(int axis)
244
    {
245
    switch(axis)
246
      {
247
      case 0: return "yellow (bottom) ";
248
      case 1: return "green (back) ";
249
      case 2: return "blue (right) ";
250
      case 3: return "red (left) ";
251
      }
252

    
253
    return null;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
// PUBLIC API
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
261
    {
262
    mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
263
    mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
264
    mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
265

    
266
    mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
267
    mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
268
    mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
269

    
270
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
271
      {
272
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
273
        {
274
        if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) )
275
          {
276
          castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
277
          convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, mPoint2D);
278

    
279
          if( isInsideFace(mPoint2D) )
280
            {
281
            fillPossibleRotations(mLastTouchedAxis, mPossible);
282
            return true;
283
            }
284
          }
285
        }
286
      }
287

    
288
    return false;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public Static2D newRotation(Static4D rotatedTouchPoint)
294
    {
295
    mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
296
    mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
297
    mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
298

    
299
    castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
300
    convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, mMove2D);
301

    
302
    mMove2D[0] -= mPoint2D[0];
303
    mMove2D[1] -= mPoint2D[1];
304

    
305
    mRotationVect= computeRotationVect(mLastTouchedAxis, mLastTouchedLR, mMove2D);
306
    int index = mLastTouchedAxis*mNumFacesPerAxis+mLastTouchedLR;
307
    float offset = computeOffset(mPoint2D, mCastAxis[index][mRotationVect]);
308

    
309
    return new Static2D(mRotationVect,offset);
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public float continueRotation(Static4D rotatedTouchPoint)
315
    {
316
    mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[0];
317
    mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[1];
318
    mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[2];
319

    
320
    return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible);
321
    }
322
  }
(6-6/8)