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 |
4946b635
|
Leszek Koltunski
|
private final float mDistanceCenterFace3D;
|
38 |
12313693
|
Leszek Koltunski
|
private final Static3D[] mFaceAxis;
|
39 |
|
|
|
40 |
|
|
private int mLastTouchedFace;
|
41 |
935f3663
|
Leszek Koltunski
|
private float[][][] mCastedRotAxis;
|
42 |
|
|
private Static4D[][] mCastedRotAxis4D;
|
43 |
4946b635
|
Leszek Koltunski
|
private float[][] mTouchBorders, mA, mB;
|
44 |
dd65ead3
|
Leszek Koltunski
|
|
45 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46 |
|
|
|
47 |
cc99cf91
|
Leszek Koltunski
|
abstract boolean isInsideFace(int face, float[] point);
|
48 |
fb377dae
|
Leszek Koltunski
|
abstract void computeEnabledAxis(int face, float[] touchPoint, int[] enabledAxis);
|
49 |
1ded8771
|
Leszek Koltunski
|
public abstract float returnRotationFactor(int numLayers, int row);
|
50 |
dd65ead3
|
Leszek Koltunski
|
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
4946b635
|
Leszek Koltunski
|
Movement(Static3D[] rotAxis, Static3D[] faceAxis, float[][] cuts, boolean[][] rotatable, float distance3D, int size)
|
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 |
|
|
|
69 |
ee39837d
|
Leszek Koltunski
|
computeCastedAxis(rotAxis);
|
70 |
4946b635
|
Leszek Koltunski
|
computeBorders(cuts,rotatable,size);
|
71 |
|
|
computeLinear(distance3D,rotAxis,faceAxis);
|
72 |
ee39837d
|
Leszek Koltunski
|
}
|
73 |
|
|
|
74 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75 |
|
|
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
|
76 |
|
|
// 1st faceAxis.
|
77 |
|
|
|
78 |
|
|
private void computeCastedAxis(Static3D[] rotAxis)
|
79 |
|
|
{
|
80 |
935f3663
|
Leszek Koltunski
|
mCastedRotAxis = new float[mNumFaceAxis][rotAxis.length][2];
|
81 |
|
|
mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
|
82 |
|
|
|
83 |
|
|
float fx,fy,fz,f;
|
84 |
14a4712f
|
Leszek Koltunski
|
|
85 |
fb377dae
|
Leszek Koltunski
|
for( int casted=0; casted<rotAxis.length; casted++)
|
86 |
14a4712f
|
Leszek Koltunski
|
{
|
87 |
faa3aed0
|
Leszek Koltunski
|
Static3D a = rotAxis[casted];
|
88 |
14a4712f
|
Leszek Koltunski
|
mPoint[0]= a.get0();
|
89 |
|
|
mPoint[1]= a.get1();
|
90 |
|
|
mPoint[2]= a.get2();
|
91 |
|
|
|
92 |
faa3aed0
|
Leszek Koltunski
|
for( int face=0; face<mNumFaceAxis; face++)
|
93 |
|
|
{
|
94 |
935f3663
|
Leszek Koltunski
|
convertTo2Dcoords( mPoint, mFaceAxis[face], mCastedRotAxis[face][casted]);
|
95 |
|
|
normalize2D(mCastedRotAxis[face][casted]);
|
96 |
|
|
|
97 |
ee39837d
|
Leszek Koltunski
|
fx = mFaceAxis[face].get0();
|
98 |
|
|
fy = mFaceAxis[face].get1();
|
99 |
|
|
fz = mFaceAxis[face].get2();
|
100 |
935f3663
|
Leszek Koltunski
|
f = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
|
101 |
|
|
mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
|
102 |
faa3aed0
|
Leszek Koltunski
|
}
|
103 |
14a4712f
|
Leszek Koltunski
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
|
|
|
108 |
|
|
private void normalize2D(float[] vect)
|
109 |
|
|
{
|
110 |
|
|
float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
|
111 |
|
|
vect[0] /= len;
|
112 |
|
|
vect[1] /= len;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116 |
|
|
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
|
117 |
|
|
|
118 |
fb377dae
|
Leszek Koltunski
|
private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
|
119 |
14a4712f
|
Leszek Koltunski
|
{
|
120 |
|
|
float cosAngle, minCosAngle = Float.MAX_VALUE;
|
121 |
cf097581
|
Leszek Koltunski
|
int minIndex=0, index;
|
122 |
14a4712f
|
Leszek Koltunski
|
float m0 = move2D[0];
|
123 |
|
|
float m1 = move2D[1];
|
124 |
|
|
float len = (float)Math.sqrt(m0*m0 + m1*m1);
|
125 |
b972c160
|
Leszek Koltunski
|
|
126 |
|
|
if( len!=0.0f )
|
127 |
|
|
{
|
128 |
|
|
m0 /= len;
|
129 |
|
|
m1 /= len;
|
130 |
|
|
}
|
131 |
|
|
else
|
132 |
|
|
{
|
133 |
|
|
m0 = 1.0f; // arbitrarily
|
134 |
|
|
m1 = 0.0f; //
|
135 |
|
|
}
|
136 |
14a4712f
|
Leszek Koltunski
|
|
137 |
fb377dae
|
Leszek Koltunski
|
int numAxis = enabled[0];
|
138 |
|
|
|
139 |
|
|
for(int axis=1; axis<=numAxis; axis++)
|
140 |
14a4712f
|
Leszek Koltunski
|
{
|
141 |
fb377dae
|
Leszek Koltunski
|
index = enabled[axis];
|
142 |
935f3663
|
Leszek Koltunski
|
cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
|
143 |
fb377dae
|
Leszek Koltunski
|
if( cosAngle<0 ) cosAngle = -cosAngle;
|
144 |
faa3aed0
|
Leszek Koltunski
|
|
145 |
fb377dae
|
Leszek Koltunski
|
if( cosAngle<minCosAngle )
|
146 |
14a4712f
|
Leszek Koltunski
|
{
|
147 |
fb377dae
|
Leszek Koltunski
|
minCosAngle=cosAngle;
|
148 |
|
|
minIndex = index;
|
149 |
14a4712f
|
Leszek Koltunski
|
}
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
return minIndex;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156 |
da178c88
|
Leszek Koltunski
|
// in the center of the face offset is always 0 regardless of the axis
|
157 |
14a4712f
|
Leszek Koltunski
|
|
158 |
|
|
private float computeOffset(float[] point, float[] axis)
|
159 |
|
|
{
|
160 |
da178c88
|
Leszek Koltunski
|
return point[0]*axis[0] + point[1]*axis[1];
|
161 |
dd65ead3
|
Leszek Koltunski
|
}
|
162 |
|
|
|
163 |
37a25788
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164 |
|
|
|
165 |
faa3aed0
|
Leszek Koltunski
|
private boolean faceIsVisible(Static3D faceAxis)
|
166 |
37a25788
|
Leszek Koltunski
|
{
|
167 |
faa3aed0
|
Leszek Koltunski
|
float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
|
168 |
|
|
return castCameraOnAxis > mDistanceCenterFace3D;
|
169 |
37a25788
|
Leszek Koltunski
|
}
|
170 |
|
|
|
171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172 |
|
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
173 |
|
|
// compute point 'output[]' which:
|
174 |
cc99cf91
|
Leszek Koltunski
|
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
|
175 |
37a25788
|
Leszek Koltunski
|
// 2) is co-linear with mCamera and mPoint
|
176 |
|
|
//
|
177 |
|
|
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
|
178 |
|
|
|
179 |
faa3aed0
|
Leszek Koltunski
|
private void castTouchPointOntoFace(Static3D faceAxis, float[] output)
|
180 |
37a25788
|
Leszek Koltunski
|
{
|
181 |
|
|
float d0 = mPoint[0]-mCamera[0];
|
182 |
|
|
float d1 = mPoint[1]-mCamera[1];
|
183 |
|
|
float d2 = mPoint[2]-mCamera[2];
|
184 |
faa3aed0
|
Leszek Koltunski
|
float a0 = faceAxis.get0();
|
185 |
|
|
float a1 = faceAxis.get1();
|
186 |
|
|
float a2 = faceAxis.get2();
|
187 |
37a25788
|
Leszek Koltunski
|
|
188 |
|
|
float denom = a0*d0 + a1*d1 + a2*d2;
|
189 |
|
|
|
190 |
|
|
if( denom != 0.0f )
|
191 |
|
|
{
|
192 |
|
|
float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
|
193 |
faa3aed0
|
Leszek Koltunski
|
float distance = mDistanceCenterFace3D;
|
194 |
37a25788
|
Leszek Koltunski
|
float alpha = (distance-axisCam)/denom;
|
195 |
|
|
|
196 |
|
|
output[0] = mCamera[0] + d0*alpha;
|
197 |
|
|
output[1] = mCamera[1] + d1*alpha;
|
198 |
|
|
output[2] = mCamera[2] + d2*alpha;
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
|
202 |
bef47287
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203 |
|
|
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
|
204 |
|
|
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
|
205 |
|
|
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
|
206 |
|
|
// original 3D Y axis and our 2D in-plane origin.
|
207 |
|
|
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
|
208 |
|
|
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
|
209 |
|
|
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
|
210 |
|
|
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
|
211 |
|
|
|
212 |
faa3aed0
|
Leszek Koltunski
|
private void convertTo2Dcoords(float[] point3D, Static3D faceAxis, float[] output)
|
213 |
bef47287
|
Leszek Koltunski
|
{
|
214 |
cad4b4d6
|
Leszek Koltunski
|
float y0,y1,y2; // base Y vector of the 2D coord system
|
215 |
faa3aed0
|
Leszek Koltunski
|
float a0 = faceAxis.get0();
|
216 |
|
|
float a1 = faceAxis.get1();
|
217 |
|
|
float a2 = faceAxis.get2();
|
218 |
bef47287
|
Leszek Koltunski
|
|
219 |
|
|
if( a0==0.0f && a2==0.0f )
|
220 |
|
|
{
|
221 |
|
|
y0=0; y1=0; y2=-a1;
|
222 |
|
|
}
|
223 |
|
|
else if( a1==0.0f )
|
224 |
|
|
{
|
225 |
|
|
y0=0; y1=1; y2=0;
|
226 |
|
|
}
|
227 |
|
|
else
|
228 |
|
|
{
|
229 |
|
|
float norm = (float)(-a1/Math.sqrt(1-a1*a1));
|
230 |
8e2295ad
|
Leszek Koltunski
|
y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
|
231 |
bef47287
|
Leszek Koltunski
|
}
|
232 |
|
|
|
233 |
cad4b4d6
|
Leszek Koltunski
|
float x0 = y1*a2 - y2*a1; //
|
234 |
|
|
float x1 = y2*a0 - y0*a2; // (2D coord baseY) x (axis) = 2D coord baseX
|
235 |
|
|
float x2 = y0*a1 - y1*a0; //
|
236 |
bef47287
|
Leszek Koltunski
|
|
237 |
|
|
float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
|
238 |
|
|
|
239 |
|
|
float origin0 = originAlpha*a0; // coords of the point where axis
|
240 |
|
|
float origin1 = originAlpha*a1; // intersects surface plane i.e.
|
241 |
|
|
float origin2 = originAlpha*a2; // the origin of our 2D coord system
|
242 |
|
|
|
243 |
|
|
float v0 = point3D[0] - origin0;
|
244 |
|
|
float v1 = point3D[1] - origin1;
|
245 |
|
|
float v2 = point3D[2] - origin2;
|
246 |
|
|
|
247 |
|
|
output[0] = v0*x0 + v1*x1 + v2*x2;
|
248 |
|
|
output[1] = v0*y0 + v1*y1 + v2*y2;
|
249 |
|
|
}
|
250 |
|
|
|
251 |
ef018c1b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252 |
|
|
|
253 |
4946b635
|
Leszek Koltunski
|
private float[] computeBorder(float[] cuts, boolean[] rotatable, int size)
|
254 |
ef018c1b
|
Leszek Koltunski
|
{
|
255 |
|
|
int len = cuts.length;
|
256 |
|
|
float[] border = new float[len];
|
257 |
|
|
|
258 |
|
|
for(int i=0; i<len; i++)
|
259 |
|
|
{
|
260 |
|
|
if( !rotatable[i] )
|
261 |
|
|
{
|
262 |
|
|
border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
|
263 |
|
|
}
|
264 |
|
|
else
|
265 |
|
|
{
|
266 |
4946b635
|
Leszek Koltunski
|
if( rotatable[i+1] ) border[i] = cuts[i]/size;
|
267 |
ef018c1b
|
Leszek Koltunski
|
else
|
268 |
|
|
{
|
269 |
|
|
int found = -1;
|
270 |
|
|
|
271 |
|
|
for(int j=i+2; j<=len; j++)
|
272 |
|
|
{
|
273 |
|
|
if( rotatable[j] )
|
274 |
|
|
{
|
275 |
|
|
found=j;
|
276 |
|
|
break;
|
277 |
|
|
}
|
278 |
|
|
}
|
279 |
|
|
|
280 |
4946b635
|
Leszek Koltunski
|
border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
|
281 |
ef018c1b
|
Leszek Koltunski
|
}
|
282 |
|
|
}
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
return border;
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
289 |
4946b635
|
Leszek Koltunski
|
// size, not numLayers (see Master Skewb where size!=numLayers)
|
290 |
ef018c1b
|
Leszek Koltunski
|
|
291 |
4946b635
|
Leszek Koltunski
|
void computeBorders(float[][] cuts, boolean[][] rotatable, int size)
|
292 |
ef018c1b
|
Leszek Koltunski
|
{
|
293 |
|
|
int numCuts = cuts.length;
|
294 |
4946b635
|
Leszek Koltunski
|
mTouchBorders = new float[numCuts][];
|
295 |
ef018c1b
|
Leszek Koltunski
|
|
296 |
|
|
for(int i=0; i<numCuts; i++)
|
297 |
|
|
{
|
298 |
4946b635
|
Leszek Koltunski
|
mTouchBorders[i] = computeBorder(cuts[i],rotatable[i],size);
|
299 |
|
|
}
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303 |
|
|
|
304 |
|
|
private int computeSign(Static3D a, Static3D b)
|
305 |
|
|
{
|
306 |
|
|
float a1 = a.get0();
|
307 |
|
|
float a2 = a.get1();
|
308 |
|
|
float a3 = a.get2();
|
309 |
|
|
float b1 = b.get0();
|
310 |
|
|
float b2 = b.get1();
|
311 |
|
|
float b3 = b.get2();
|
312 |
|
|
|
313 |
|
|
return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
317 |
|
|
|
318 |
|
|
private float crossProductLen(Static3D a, Static3D b)
|
319 |
|
|
{
|
320 |
|
|
float a1 = a.get0();
|
321 |
|
|
float a2 = a.get1();
|
322 |
|
|
float a3 = a.get2();
|
323 |
|
|
float b1 = b.get0();
|
324 |
|
|
float b2 = b.get1();
|
325 |
|
|
float b3 = b.get2();
|
326 |
|
|
|
327 |
|
|
float x1 = a2*b3-a3*b2;
|
328 |
|
|
float x2 = a3*b1-a1*b3;
|
329 |
|
|
float x3 = a1*b2-a2*b1;
|
330 |
|
|
|
331 |
|
|
return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335 |
|
|
// compute the array of 'A' and 'B' coeffs of the Ax+B linear function by which we need to multiply
|
336 |
|
|
// the 3D 'cuts' to translate it from 3D (i.e. with respect to the rotAxis) to 2D in-face (i.e. with
|
337 |
|
|
// respect to the 2D rotAxis cast into a particular face)
|
338 |
|
|
|
339 |
|
|
void computeLinear(float distance3D, Static3D[] rotAxis, Static3D[] faceAxis)
|
340 |
|
|
{
|
341 |
|
|
int numFaces = faceAxis.length;
|
342 |
|
|
int numRot = rotAxis.length;
|
343 |
|
|
|
344 |
|
|
mA = new float[numFaces][numRot];
|
345 |
|
|
mB = new float[numFaces][numRot];
|
346 |
|
|
|
347 |
|
|
for(int i=0; i<numFaces; i++)
|
348 |
|
|
for(int j=0; j<numRot; j++)
|
349 |
|
|
{
|
350 |
|
|
mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
|
351 |
|
|
|
352 |
|
|
if( mA[i][j]!=0.0f )
|
353 |
|
|
{
|
354 |
|
|
float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
|
355 |
|
|
int sign = computeSign(faceAxis[i],rotAxis[j]);
|
356 |
|
|
mB[i][j] = sign*distance3D*coeff;
|
357 |
|
|
}
|
358 |
|
|
else mB[i][j] = 0.0f;
|
359 |
|
|
|
360 |
|
|
android.util.Log.e("D", "face="+i+" rot="+j+" a="+mA[i][j]+" B="+mB[i][j]);
|
361 |
|
|
}
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365 |
|
|
|
366 |
|
|
private int computeRowFromOffset(int face, int axisIndex, float offset)
|
367 |
|
|
{
|
368 |
|
|
float[] borders = mTouchBorders[axisIndex];
|
369 |
|
|
int len = borders.length;
|
370 |
|
|
float A = mA[face][axisIndex];
|
371 |
|
|
|
372 |
|
|
if( A!=0.0f )
|
373 |
|
|
{
|
374 |
|
|
float B = mB[face][axisIndex];
|
375 |
|
|
|
376 |
|
|
for(int i=0; i<len; i++)
|
377 |
|
|
{
|
378 |
|
|
float translated = B + borders[i]/A;
|
379 |
|
|
|
380 |
|
|
android.util.Log.e("D", "offset="+offset+" borders[i]="+borders[i]+" translated="+translated+" A="+A+" B="+B);
|
381 |
|
|
|
382 |
|
|
if( offset<translated ) return i;
|
383 |
|
|
}
|
384 |
ef018c1b
|
Leszek Koltunski
|
}
|
385 |
|
|
|
386 |
4946b635
|
Leszek Koltunski
|
return len;
|
387 |
ef018c1b
|
Leszek Koltunski
|
}
|
388 |
|
|
|
389 |
dd65ead3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
390 |
|
|
// PUBLIC API
|
391 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392 |
|
|
|
393 |
c7c83fb7
|
Leszek Koltunski
|
public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera, float objectRatio)
|
394 |
dd65ead3
|
Leszek Koltunski
|
{
|
395 |
c7b00dfb
|
Leszek Koltunski
|
mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
|
396 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
|
397 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
|
398 |
|
|
|
399 |
|
|
mCamera[0] = rotatedCamera.get0()/objectRatio;
|
400 |
|
|
mCamera[1] = rotatedCamera.get1()/objectRatio;
|
401 |
|
|
mCamera[2] = rotatedCamera.get2()/objectRatio;
|
402 |
dd65ead3
|
Leszek Koltunski
|
|
403 |
fb377dae
|
Leszek Koltunski
|
for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
|
404 |
dd65ead3
|
Leszek Koltunski
|
{
|
405 |
fb377dae
|
Leszek Koltunski
|
if( faceIsVisible(mFaceAxis[mLastTouchedFace]) )
|
406 |
dd65ead3
|
Leszek Koltunski
|
{
|
407 |
fb377dae
|
Leszek Koltunski
|
castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
|
408 |
|
|
convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mPoint2D);
|
409 |
cc99cf91
|
Leszek Koltunski
|
if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
|
410 |
dd65ead3
|
Leszek Koltunski
|
}
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
return false;
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417 |
|
|
|
418 |
4946b635
|
Leszek Koltunski
|
public Static2D newRotation(Static4D rotatedTouchPoint, float objectRatio)
|
419 |
dd65ead3
|
Leszek Koltunski
|
{
|
420 |
c7b00dfb
|
Leszek Koltunski
|
mPoint[0] = rotatedTouchPoint.get0()/objectRatio;
|
421 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
|
422 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
|
423 |
dd65ead3
|
Leszek Koltunski
|
|
424 |
fb377dae
|
Leszek Koltunski
|
castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
|
425 |
|
|
convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mMove2D);
|
426 |
14a4712f
|
Leszek Koltunski
|
|
427 |
|
|
mMove2D[0] -= mPoint2D[0];
|
428 |
|
|
mMove2D[1] -= mPoint2D[1];
|
429 |
|
|
|
430 |
fb377dae
|
Leszek Koltunski
|
computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
|
431 |
|
|
int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
|
432 |
935f3663
|
Leszek Koltunski
|
float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
|
433 |
4946b635
|
Leszek Koltunski
|
int row = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
|
434 |
dd65ead3
|
Leszek Koltunski
|
|
435 |
ee39837d
|
Leszek Koltunski
|
return new Static2D(rotIndex,row);
|
436 |
dd65ead3
|
Leszek Koltunski
|
}
|
437 |
473611ee
|
Leszek Koltunski
|
|
438 |
935f3663
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439 |
|
|
|
440 |
|
|
public Static4D getCastedRotAxis(int rotIndex)
|
441 |
|
|
{
|
442 |
|
|
return mCastedRotAxis4D[mLastTouchedFace][rotIndex];
|
443 |
|
|
}
|
444 |
|
|
|
445 |
473611ee
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
446 |
|
|
|
447 |
|
|
public int getTouchedFace()
|
448 |
|
|
{
|
449 |
fb377dae
|
Leszek Koltunski
|
return mLastTouchedFace;
|
450 |
473611ee
|
Leszek Koltunski
|
}
|
451 |
|
|
|
452 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
453 |
|
|
|
454 |
9621255f
|
Leszek Koltunski
|
public float[] getTouchedPoint3D()
|
455 |
473611ee
|
Leszek Koltunski
|
{
|
456 |
9621255f
|
Leszek Koltunski
|
return mTouch;
|
457 |
473611ee
|
Leszek Koltunski
|
}
|
458 |
27a70eae
|
Leszek Koltunski
|
}
|