Project

General

Profile

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

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

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 RubikMovement
29
  {
30
  private int mLastTouchedAxis;
31
  private float[] mPoint, mCamera, mTouch;
32
  private float[] mPoint2D, mMove2D;
33
  private float[][][] mCastAxis;
34
  private int mNumRotAxis, mNumFaceAxis;
35
  private float mDistanceCenterFace3D, mDistanceCenterFace2D;
36
  private Static3D[] mFaceAxis;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
  abstract boolean isInsideFace(float[] point);
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  RubikMovement(Static3D[] rotAxis, Static3D[] faceAxis, float distance3D, float distance2D)
45
    {
46
    mPoint = new float[3];
47
    mCamera= new float[3];
48
    mTouch = new float[3];
49

    
50
    mPoint2D = new float[2];
51
    mMove2D  = new float[2];
52

    
53
    mNumRotAxis = rotAxis.length;
54
    mFaceAxis   = faceAxis;
55
    mNumFaceAxis= mFaceAxis.length;
56

    
57
    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[mNumFaceAxis][mNumRotAxis][2];
63

    
64
    for( int casted=0; casted<mNumRotAxis; casted++)
65
      {
66
      Static3D a = rotAxis[casted];
67
      mPoint[0]= a.get0();
68
      mPoint[1]= a.get1();
69
      mPoint[2]= a.get2();
70

    
71
      for( int face=0; face<mNumFaceAxis; face++)
72
        {
73
        convertTo2Dcoords( mPoint, mFaceAxis[face], mCastAxis[face][casted]);
74
        normalize2D(mCastAxis[face][casted]);
75
        }
76
      }
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  private void normalize2D(float[] vect)
82
    {
83
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
84
    vect[0] /= len;
85
    vect[1] /= len;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
90

    
91
  private int computeRotationIndex(int faceAxis, float[] move2D)
92
    {
93
    float cosAngle, minCosAngle = Float.MAX_VALUE;
94
    int minIndex=-1;
95
    float m0 = move2D[0];
96
    float m1 = move2D[1];
97
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
98
    float x,y;
99

    
100
    if( len!=0.0f )
101
      {
102
      m0 /= len;
103
      m1 /= len;
104
      }
105
    else
106
      {
107
      m0 = 1.0f;  // arbitrarily
108
      m1 = 0.0f;  //
109
      }
110

    
111
    for(int rotAxis=0; rotAxis<mNumRotAxis; rotAxis++)
112
      {
113
      x = mCastAxis[faceAxis][rotAxis][0];
114
      y = mCastAxis[faceAxis][rotAxis][1];
115

    
116
      if( x*x + y*y > 0.01f )
117
        {
118
        cosAngle = m0*x + m1*y;
119
        if( cosAngle<0 ) cosAngle = -cosAngle;
120

    
121
        if( cosAngle<minCosAngle )
122
          {
123
          minCosAngle=cosAngle;
124
          minIndex = rotAxis;
125
          }
126
        }
127
      }
128

    
129
    return minIndex;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private float computeOffset(float[] point, float[] axis)
135
    {
136
    return point[0]*axis[0] + point[1]*axis[1] + mDistanceCenterFace2D;
137
    }
138

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

    
141
  private boolean faceIsVisible(Static3D faceAxis)
142
    {
143
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
144
    return castCameraOnAxis > mDistanceCenterFace3D;
145
    }
146

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

    
157
  private void castTouchPointOntoFace(Static3D faceAxis, float[] output)
158
    {
159
    float d0 = mPoint[0]-mCamera[0];
160
    float d1 = mPoint[1]-mCamera[1];
161
    float d2 = mPoint[2]-mCamera[2];
162
    float a0 = faceAxis.get0();
163
    float a1 = faceAxis.get1();
164
    float a2 = faceAxis.get2();
165

    
166
    float denom = a0*d0 + a1*d1 + a2*d2;
167

    
168
    if( denom != 0.0f )
169
      {
170
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
171
      float distance = mDistanceCenterFace3D;
172
      float alpha = (distance-axisCam)/denom;
173

    
174
      output[0] = mCamera[0] + d0*alpha;
175
      output[1] = mCamera[1] + d1*alpha;
176
      output[2] = mCamera[2] + d2*alpha;
177
      }
178
    }
179

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

    
190
  private void convertTo2Dcoords(float[] point3D, Static3D faceAxis, float[] output)
191
    {
192
    float y0,y1,y2; // base Y vector of the 2D coord system
193
    float a0 = faceAxis.get0();
194
    float a1 = faceAxis.get1();
195
    float a2 = faceAxis.get2();
196

    
197
    if( a0==0.0f && a2==0.0f )
198
      {
199
      y0=0; y1=0; y2=-a1;
200
      }
201
    else if( a1==0.0f )
202
      {
203
      y0=0; y1=1; y2=0;
204
      }
205
    else
206
      {
207
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
208
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
209
      }
210

    
211
    float x0 = y1*a2 - y2*a1;  //
212
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
213
    float x2 = y0*a1 - y1*a0;  //
214

    
215
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
216

    
217
    float origin0 = originAlpha*a0; // coords of the point where axis
218
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
219
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
220

    
221
    float v0 = point3D[0] - origin0;
222
    float v1 = point3D[1] - origin1;
223
    float v2 = point3D[2] - origin2;
224

    
225
    output[0] = v0*x0 + v1*x1 + v2*x2;
226
    output[1] = v0*y0 + v1*y1 + v2*y2;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
// PUBLIC API
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
234
    {
235
    float objectRatio = RubikObject.getObjectRatio();
236

    
237
    mPoint[0]  = rotatedTouchPoint.get0()/objectRatio;
238
    mPoint[1]  = rotatedTouchPoint.get1()/objectRatio;
239
    mPoint[2]  = rotatedTouchPoint.get2()/objectRatio;
240

    
241
    mCamera[0] = rotatedCamera.get0()/objectRatio;
242
    mCamera[1] = rotatedCamera.get1()/objectRatio;
243
    mCamera[2] = rotatedCamera.get2()/objectRatio;
244

    
245
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumFaceAxis; mLastTouchedAxis++)
246
      {
247
      if( faceIsVisible(mFaceAxis[mLastTouchedAxis]) )
248
        {
249
        castTouchPointOntoFace(mFaceAxis[mLastTouchedAxis], mTouch);
250
        convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedAxis], mPoint2D);
251
        if( isInsideFace(mPoint2D) ) return true;
252
        }
253
      }
254

    
255
    return false;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public Static2D newRotation(Static4D rotatedTouchPoint)
261
    {
262
    float objectRatio = RubikObject.getObjectRatio();
263

    
264
    mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
265
    mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
266
    mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
267

    
268
    castTouchPointOntoFace(mFaceAxis[mLastTouchedAxis], mTouch);
269
    convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedAxis], mMove2D);
270

    
271
    mMove2D[0] -= mPoint2D[0];
272
    mMove2D[1] -= mPoint2D[1];
273

    
274
    int rotIndex = computeRotationIndex(mLastTouchedAxis, mMove2D);
275
    float offset = computeOffset(mPoint2D, mCastAxis[mLastTouchedAxis][rotIndex]);
276

    
277
    return new Static2D(rotIndex,offset);
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public int getTouchedFace()
283
    {
284
    return mLastTouchedAxis;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public float[] getTouchedPoint3D()
290
    {
291
    return mTouch;
292
    }
293
  }
(4-4/10)