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