Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectMovement.java @ 418aa554

1 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 27a70eae Leszek Koltunski
22 efef689c Leszek Koltunski
import org.distorted.library.type.Static2D;
23 37a25788 Leszek Koltunski
import org.distorted.library.type.Static3D;
24 27a70eae Leszek Koltunski
import org.distorted.library.type.Static4D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
28
public abstract class RubikObjectMovement
29
  {
30 12ad3fca Leszek Koltunski
  private int mLastTouchedAxis;
31
  private float[] mPoint, mCamera, mTouch;
32 14a4712f Leszek Koltunski
  private float[] mPoint2D, mMove2D;
33
  private float[][][] mCastAxis;
34 dd65ead3 Leszek Koltunski
  private int mLastTouchedLR;
35
  private int mNumAxis, mNumFacesPerAxis;
36 14a4712f Leszek Koltunski
  private float mDistanceCenterFace3D, mDistanceCenterFace2D;
37 37a25788 Leszek Koltunski
  private Static3D[] mAxis;
38 dd65ead3 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
  abstract boolean isInsideFace(float[] point);
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 418aa554 Leszek Koltunski
  RubikObjectMovement(Static3D[] axis, float numFacesPerAxis, float distance3D, float distance2D)
46 dd65ead3 Leszek Koltunski
    {
47
    mPoint = new float[3];
48
    mCamera= new float[3];
49
    mTouch = new float[3];
50
51 14a4712f Leszek Koltunski
    mPoint2D = new float[2];
52
    mMove2D  = new float[2];
53 bef47287 Leszek Koltunski
54 37a25788 Leszek Koltunski
    mAxis = axis;
55
    mNumAxis = mAxis.length;
56 418aa554 Leszek Koltunski
    mNumFacesPerAxis = (int)numFacesPerAxis;  // TODO
57 14a4712f Leszek Koltunski
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
58
    mDistanceCenterFace2D = distance2D; // distance from the center of a face to its edge
59
60
    // mCastAxis[1][2]{0,1} are the 2D coords of the 2nd axis cast onto the face defined by the
61
    // 1st pair (axis,lr)
62
    mCastAxis = new float[mNumAxis*mNumFacesPerAxis][mNumAxis][2];
63
64
    for( int casted=0; casted<mNumAxis; casted++)
65
      {
66
      Static3D a = mAxis[casted];
67
      mPoint[0]= a.get0();
68
      mPoint[1]= a.get1();
69
      mPoint[2]= a.get2();
70
71
      for( int surface=0; surface<mNumAxis; surface++)
72
        for(int lr=0; lr<mNumFacesPerAxis; lr++)
73
          {
74
          int index = surface*mNumFacesPerAxis + lr;
75
76
          if( casted!=surface )
77
            {
78
            convertTo2Dcoords( mPoint, mAxis[surface], lr, mPoint2D);
79
            mCastAxis[index][casted][0] = mPoint2D[0];
80
            mCastAxis[index][casted][1] = mPoint2D[1];
81
            normalize2D(mCastAxis[index][casted]);
82
            }
83
          else
84
            {
85
            mCastAxis[index][casted][0] = 0;
86
            mCastAxis[index][casted][1] = 0;
87
            }
88
          }
89
      }
90
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  private void normalize2D(float[] vect)
95
    {
96
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
97
    vect[0] /= len;
98
    vect[1] /= len;
99
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
103
104 12ad3fca Leszek Koltunski
  private int computeRotationIndex(int axis, int lr, float[] move2D)
105 14a4712f Leszek Koltunski
    {
106
    float cosAngle, minCosAngle = Float.MAX_VALUE;
107
    int minIndex=-1;
108
    int index = axis*mNumFacesPerAxis + lr;
109
    float m0 = move2D[0];
110
    float m1 = move2D[1];
111
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
112 b972c160 Leszek Koltunski
113
    if( len!=0.0f )
114
      {
115
      m0 /= len;
116
      m1 /= len;
117
      }
118
    else
119
      {
120
      m0 = 1.0f;  // arbitrarily
121
      m1 = 0.0f;  //
122
      }
123 14a4712f Leszek Koltunski
124
    for(int i=0; i<mNumAxis; i++)
125
      {
126
      if( axis != i )
127
        {
128
        cosAngle = m0*mCastAxis[index][i][0] +  m1*mCastAxis[index][i][1];
129
        if( cosAngle<0 ) cosAngle = -cosAngle;
130
131
        if( cosAngle<minCosAngle )
132
          {
133
          minCosAngle=cosAngle;
134
          minIndex = i;
135
          }
136
        }
137
      }
138
139
    return minIndex;
140
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
  private float computeOffset(float[] point, float[] axis)
145
    {
146
    return point[0]*axis[0] + point[1]*axis[1] + mDistanceCenterFace2D;
147 dd65ead3 Leszek Koltunski
    }
148
149 37a25788 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
  private boolean faceIsVisible(Static3D axis, int lr)
152
    {
153
    float castCameraOnAxis = mCamera[0]*axis.get0() + mCamera[1]*axis.get1() + mCamera[2]*axis.get2();
154 14a4712f Leszek Koltunski
    return (2*lr-1)*castCameraOnAxis > mDistanceCenterFace3D;
155 37a25788 Leszek Koltunski
    }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
159
// compute point 'output[]' which:
160
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0)) [and this
161
//    distance is +-mDistanceCenterFace, depending if it is the face on the left or the right end of
162
//    the axis] (lr=0 or 1, so (2lr-1)*mDistanceCenterFace)
163
// 2) is co-linear with mCamera and mPoint
164
//
165
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
166
167
  private void castTouchPointOntoFace(Static3D axis, int lr, float[] output)
168
    {
169
    float d0 = mPoint[0]-mCamera[0];
170
    float d1 = mPoint[1]-mCamera[1];
171
    float d2 = mPoint[2]-mCamera[2];
172
    float a0 = axis.get0();
173
    float a1 = axis.get1();
174
    float a2 = axis.get2();
175
176
    float denom = a0*d0 + a1*d1 + a2*d2;
177
178
    if( denom != 0.0f )
179
      {
180
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
181 14a4712f Leszek Koltunski
      float distance = (2*lr-1)*mDistanceCenterFace3D;
182 37a25788 Leszek Koltunski
      float alpha = (distance-axisCam)/denom;
183
184
      output[0] = mCamera[0] + d0*alpha;
185
      output[1] = mCamera[1] + d1*alpha;
186
      output[2] = mCamera[2] + d2*alpha;
187
      }
188
    }
189
190 bef47287 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
192
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
193
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
194
// original 3D Y axis and our 2D in-plane origin.
195
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
196
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
197
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
198
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
199
200
  private void convertTo2Dcoords(float[] point3D, Static3D axis, int lr, float[] output)
201
    {
202 cad4b4d6 Leszek Koltunski
    float y0,y1,y2; // base Y vector of the 2D coord system
203 bef47287 Leszek Koltunski
    float a0 = axis.get0();
204
    float a1 = axis.get1();
205
    float a2 = axis.get2();
206
207
    if( lr==0 )
208
      {
209
      a0=-a0; a1=-a1; a2=-a2;
210
      }
211
212
    if( a0==0.0f && a2==0.0f )
213
      {
214
      y0=0; y1=0; y2=-a1;
215
      }
216
    else if( a1==0.0f )
217
      {
218
      y0=0; y1=1; y2=0;
219
      }
220
    else
221
      {
222
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
223 8e2295ad Leszek Koltunski
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
224 bef47287 Leszek Koltunski
      }
225
226 cad4b4d6 Leszek Koltunski
    float x0 = y1*a2 - y2*a1;  //
227
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
228
    float x2 = y0*a1 - y1*a0;  //
229 bef47287 Leszek Koltunski
230
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
231
232
    float origin0 = originAlpha*a0; // coords of the point where axis
233
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
234
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
235
236
    float v0 = point3D[0] - origin0;
237
    float v1 = point3D[1] - origin1;
238
    float v2 = point3D[2] - origin2;
239
240
    output[0] = v0*x0 + v1*x1 + v2*x2;
241
    output[1] = v0*y0 + v1*y1 + v2*y2;
242
    }
243
244 dd65ead3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
245
// PUBLIC API
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
249
    {
250 c7b00dfb Leszek Koltunski
    float objectRatio = RubikObject.getObjectRatio();
251 dd65ead3 Leszek Koltunski
252 c7b00dfb Leszek Koltunski
    mPoint[0]  = rotatedTouchPoint.get0()/objectRatio;
253
    mPoint[1]  = rotatedTouchPoint.get1()/objectRatio;
254
    mPoint[2]  = rotatedTouchPoint.get2()/objectRatio;
255
256
    mCamera[0] = rotatedCamera.get0()/objectRatio;
257
    mCamera[1] = rotatedCamera.get1()/objectRatio;
258
    mCamera[2] = rotatedCamera.get2()/objectRatio;
259 dd65ead3 Leszek Koltunski
260
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
261
      {
262
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
263
        {
264 37a25788 Leszek Koltunski
        if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) )
265 dd65ead3 Leszek Koltunski
          {
266 37a25788 Leszek Koltunski
          castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
267 14a4712f Leszek Koltunski
          convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, mPoint2D);
268 dd65ead3 Leszek Koltunski
269 12ad3fca Leszek Koltunski
          if( isInsideFace(mPoint2D) ) return true;
270 dd65ead3 Leszek Koltunski
          }
271
        }
272
      }
273
274
    return false;
275
    }
276
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279
  public Static2D newRotation(Static4D rotatedTouchPoint)
280
    {
281 c7b00dfb Leszek Koltunski
    float objectRatio = RubikObject.getObjectRatio();
282
283
    mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
284
    mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
285
    mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
286 dd65ead3 Leszek Koltunski
287 14a4712f Leszek Koltunski
    castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
288
    convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, mMove2D);
289
290
    mMove2D[0] -= mPoint2D[0];
291
    mMove2D[1] -= mPoint2D[1];
292
293 12ad3fca Leszek Koltunski
    int rotIndex = computeRotationIndex(mLastTouchedAxis, mLastTouchedLR, mMove2D);
294
    int index    = mLastTouchedAxis*mNumFacesPerAxis+mLastTouchedLR;
295
    float offset = computeOffset(mPoint2D, mCastAxis[index][rotIndex]);
296 dd65ead3 Leszek Koltunski
297 12ad3fca Leszek Koltunski
    return new Static2D(rotIndex,offset);
298 dd65ead3 Leszek Koltunski
    }
299 473611ee Leszek Koltunski
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
302
  public int getTouchedFace()
303
    {
304 ca292407 Leszek Koltunski
    return mNumFacesPerAxis==2 ? 2*mLastTouchedAxis + 1 - mLastTouchedLR : mLastTouchedAxis;
305 473611ee Leszek Koltunski
    }
306
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
309 9621255f Leszek Koltunski
  public float[] getTouchedPoint3D()
310 473611ee Leszek Koltunski
    {
311 9621255f Leszek Koltunski
    return mTouch;
312 473611ee Leszek Koltunski
    }
313 27a70eae Leszek Koltunski
  }