Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikMovement.java @ fb377dae

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