1 |
5caf2641
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2022 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7 |
|
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
|
|
package org.distorted.objectlib.touchcontrol;
|
11 |
|
|
|
12 |
aacf5e27
|
Leszek Koltunski
|
import org.distorted.library.helpers.QuatHelper;
|
13 |
5caf2641
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
14 |
|
|
import org.distorted.library.type.Static4D;
|
15 |
|
|
import org.distorted.objectlib.main.TwistyObject;
|
16 |
|
|
|
17 |
b63235a0
|
Leszek Koltunski
|
import static org.distorted.objectlib.main.TwistyObject.SQ2;
|
18 |
|
|
|
19 |
5caf2641
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
// Ball-shaped objects: map the 2D swipes of user's fingers to 3D rotations
|
21 |
|
|
|
22 |
|
|
public class TouchControlBall extends TouchControl
|
23 |
|
|
{
|
24 |
bfdb9aa5
|
Leszek Koltunski
|
private static final float MIN_LEN = 0.35f;
|
25 |
|
|
private static final float[] mTmp = new float[4];
|
26 |
|
|
|
27 |
5caf2641
|
Leszek Koltunski
|
private final Static3D[] mRotAxis;
|
28 |
bfdb9aa5
|
Leszek Koltunski
|
private final float[] mPoint, mCamera, mTouch;
|
29 |
b31249d6
|
Leszek Koltunski
|
private final int[] mEnabledRotAxis;
|
30 |
|
|
private final int[][][] mEnabled;
|
31 |
bfdb9aa5
|
Leszek Koltunski
|
private final float[] mMove2D;
|
32 |
|
|
private final float[][] mCastedRotAxis;
|
33 |
|
|
private float[][] mTouchBorders;
|
34 |
93dc5a55
|
leszek
|
private int mGhostAxisEnabled;
|
35 |
bfdb9aa5
|
Leszek Koltunski
|
|
36 |
|
|
private float mLongitude, mLatitude;
|
37 |
|
|
private float mX, mY, mZ;
|
38 |
5caf2641
|
Leszek Koltunski
|
|
39 |
b63235a0
|
Leszek Koltunski
|
private static final float X = (float)Math.sqrt( (2-SQ2)/(6+SQ2) );
|
40 |
|
|
private static final float Y = (float)Math.sqrt( (2+SQ2)/(6+SQ2) );
|
41 |
|
|
|
42 |
|
|
public static final Static3D[] FACE_AXIS = new Static3D[]
|
43 |
|
|
{
|
44 |
|
|
new Static3D( X, Y, Y),
|
45 |
|
|
new Static3D( X,-Y, Y),
|
46 |
|
|
new Static3D( Y, Y, X),
|
47 |
|
|
new Static3D( Y,-Y, X),
|
48 |
|
|
new Static3D( Y, Y,-X),
|
49 |
|
|
new Static3D( Y,-Y,-X),
|
50 |
|
|
new Static3D( X, Y,-Y),
|
51 |
|
|
new Static3D( X,-Y,-Y),
|
52 |
|
|
new Static3D(-X, Y,-Y),
|
53 |
|
|
new Static3D(-X,-Y,-Y),
|
54 |
|
|
new Static3D(-Y, Y,-X),
|
55 |
|
|
new Static3D(-Y,-Y,-X),
|
56 |
|
|
new Static3D(-Y, Y, X),
|
57 |
|
|
new Static3D(-Y,-Y, X),
|
58 |
|
|
new Static3D(-X, Y, Y),
|
59 |
|
|
new Static3D(-X,-Y, Y),
|
60 |
|
|
};
|
61 |
|
|
|
62 |
5caf2641
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63 |
|
|
|
64 |
|
|
public TouchControlBall(TwistyObject object)
|
65 |
|
|
{
|
66 |
fb1e9a31
|
Leszek Koltunski
|
super(object);
|
67 |
a8efe86f
|
Leszek Koltunski
|
|
68 |
bfdb9aa5
|
Leszek Koltunski
|
int[] numLayers = object.getNumLayers();
|
69 |
|
|
float[][] cuts = object.getCuts(numLayers);
|
70 |
|
|
boolean[][] rotatable = object.getLayerRotatable(numLayers);
|
71 |
|
|
float size = object.getSize();
|
72 |
5caf2641
|
Leszek Koltunski
|
mRotAxis = object.getRotationAxis();
|
73 |
b31249d6
|
Leszek Koltunski
|
mEnabled = object.getEnabled();
|
74 |
|
|
|
75 |
bfdb9aa5
|
Leszek Koltunski
|
mMove2D = new float[2];
|
76 |
|
|
|
77 |
a8efe86f
|
Leszek Koltunski
|
mPoint = new float[3];
|
78 |
|
|
mCamera= new float[3];
|
79 |
bfdb9aa5
|
Leszek Koltunski
|
mTouch = new float[3];
|
80 |
|
|
|
81 |
|
|
int numRotAxis = mRotAxis.length;
|
82 |
|
|
mEnabledRotAxis = new int[numRotAxis+1];
|
83 |
|
|
mCastedRotAxis = new float[numRotAxis][2];
|
84 |
|
|
|
85 |
93dc5a55
|
leszek
|
mGhostAxisEnabled = -1;
|
86 |
4a014840
|
leszek
|
|
87 |
bfdb9aa5
|
Leszek Koltunski
|
computeBorders(cuts,rotatable,size);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91 |
|
|
|
92 |
|
|
private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
|
93 |
|
|
{
|
94 |
|
|
if( cuts==null ) return null;
|
95 |
|
|
|
96 |
|
|
int len = cuts.length;
|
97 |
|
|
float[] border = new float[len];
|
98 |
|
|
|
99 |
|
|
for(int i=0; i<len; i++)
|
100 |
|
|
{
|
101 |
|
|
if( !rotatable[i] )
|
102 |
|
|
{
|
103 |
|
|
border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
|
104 |
|
|
}
|
105 |
|
|
else
|
106 |
|
|
{
|
107 |
|
|
if( rotatable[i+1] ) border[i] = cuts[i]/size;
|
108 |
|
|
else
|
109 |
|
|
{
|
110 |
|
|
int found = -1;
|
111 |
|
|
|
112 |
|
|
for(int j=i+2; j<=len; j++)
|
113 |
|
|
{
|
114 |
|
|
if( rotatable[j] )
|
115 |
|
|
{
|
116 |
|
|
found=j;
|
117 |
|
|
break;
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
return border;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130 |
|
|
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
|
131 |
|
|
|
132 |
|
|
void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
|
133 |
|
|
{
|
134 |
|
|
int numCuts = cuts.length;
|
135 |
|
|
mTouchBorders = new float[numCuts][];
|
136 |
|
|
|
137 |
|
|
for(int axis=0; axis<numCuts; axis++)
|
138 |
|
|
{
|
139 |
|
|
mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144 |
|
|
|
145 |
|
|
private int computeRowFromOffset(int axisIndex, float offset)
|
146 |
|
|
{
|
147 |
|
|
float[] borders = mTouchBorders[axisIndex];
|
148 |
|
|
if( borders==null ) return 0;
|
149 |
|
|
int len = borders.length;
|
150 |
|
|
|
151 |
|
|
for(int i=0; i<len; i++)
|
152 |
|
|
{
|
153 |
|
|
if( offset<borders[i] ) return i;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
return len;
|
157 |
5caf2641
|
Leszek Koltunski
|
}
|
158 |
|
|
|
159 |
54588439
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160 |
b31249d6
|
Leszek Koltunski
|
// Longitude spans from 0 (at Guinea Bay) increasing to the east all the way to 2PI
|
161 |
|
|
// Latitude - from -PI/2 (South Pole) to +PI/2 (North Pole)
|
162 |
54588439
|
Leszek Koltunski
|
|
163 |
|
|
private void computeLongitudeAndLatitude(float A, float B, float C)
|
164 |
|
|
{
|
165 |
b31249d6
|
Leszek Koltunski
|
float sqrt = (float)Math.sqrt(B*B - 4*A*C);
|
166 |
|
|
float alpha= (-B+sqrt)/(2*A); // this is the closer point
|
167 |
|
|
|
168 |
|
|
float cx = mCamera[0];
|
169 |
|
|
float cy = mCamera[1];
|
170 |
|
|
float cz = mCamera[2];
|
171 |
|
|
|
172 |
|
|
float vx = mCamera[0]-mPoint[0];
|
173 |
|
|
float vy = mCamera[1]-mPoint[1];
|
174 |
|
|
float vz = mCamera[2]-mPoint[2];
|
175 |
|
|
|
176 |
bfdb9aa5
|
Leszek Koltunski
|
mX = cx + alpha*vx;
|
177 |
|
|
mY = cy + alpha*vy;
|
178 |
|
|
mZ = cz + alpha*vz;
|
179 |
b31249d6
|
Leszek Koltunski
|
|
180 |
bfdb9aa5
|
Leszek Koltunski
|
mLongitude = mZ==0 ? 0 : (float)Math.atan(mX/mZ);
|
181 |
|
|
mLatitude = (float)Math.asin(2*mY);
|
182 |
b31249d6
|
Leszek Koltunski
|
|
183 |
bfdb9aa5
|
Leszek Koltunski
|
if( mZ<0 ) mLongitude += Math.PI;
|
184 |
|
|
else if( mX<0 ) mLongitude += 2*Math.PI;
|
185 |
b31249d6
|
Leszek Koltunski
|
}
|
186 |
|
|
|
187 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188 |
|
|
// this is Masterball-specific. See TwistyMasterball.getEnabled()
|
189 |
|
|
|
190 |
|
|
private int returnTouchedFace()
|
191 |
|
|
{
|
192 |
|
|
float t = (float)(mLongitude + Math.PI/8);
|
193 |
|
|
if( t>2*Math.PI ) t-=(2*Math.PI);
|
194 |
|
|
int ret = (int)(t/(Math.PI/4));
|
195 |
|
|
return ret<8 ? ret : 7;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199 |
|
|
// this is Masterball-specific. No parts in any faces.
|
200 |
54588439
|
Leszek Koltunski
|
|
201 |
b31249d6
|
Leszek Koltunski
|
private int returnTouchedPart()
|
202 |
|
|
{
|
203 |
|
|
return 0;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
bfdb9aa5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207 |
|
|
|
208 |
|
|
private float computeOffset(int rotIndex)
|
209 |
|
|
{
|
210 |
|
|
Static3D axis = mRotAxis[rotIndex];
|
211 |
|
|
return mX*axis.get0() + mY*axis.get1() + mZ*axis.get2();
|
212 |
|
|
}
|
213 |
|
|
|
214 |
b31249d6
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215 |
|
|
|
216 |
|
|
private void computeEnabledAxis()
|
217 |
|
|
{
|
218 |
93dc5a55
|
leszek
|
if( mGhostAxisEnabled<0 )
|
219 |
4a014840
|
leszek
|
{
|
220 |
|
|
int face = returnTouchedFace();
|
221 |
|
|
int part = returnTouchedPart();
|
222 |
|
|
int num = mEnabled[face][0].length;
|
223 |
|
|
mEnabledRotAxis[0] = num;
|
224 |
|
|
System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
|
225 |
|
|
}
|
226 |
|
|
else
|
227 |
|
|
{
|
228 |
93dc5a55
|
leszek
|
mEnabledRotAxis[0] = 1; // if in 'ghost' mode, only one axis is enabled.
|
229 |
|
|
mEnabledRotAxis[1] = mGhostAxisEnabled;
|
230 |
4a014840
|
leszek
|
}
|
231 |
54588439
|
Leszek Koltunski
|
}
|
232 |
|
|
|
233 |
5caf2641
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
234 |
|
|
|
235 |
|
|
public int getTouchedCubitFace()
|
236 |
|
|
{
|
237 |
|
|
return 0;
|
238 |
|
|
}
|
239 |
|
|
|
240 |
644c217a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241 |
|
|
|
242 |
|
|
public float[] getTouchedPuzzleCenter()
|
243 |
|
|
{
|
244 |
|
|
return null;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
5caf2641
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248 |
|
|
|
249 |
|
|
public int getTouchedCubit()
|
250 |
|
|
{
|
251 |
|
|
return 0;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255 |
|
|
|
256 |
|
|
public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
|
257 |
|
|
{
|
258 |
a8efe86f
|
Leszek Koltunski
|
mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
259 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
260 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
261 |
|
|
|
262 |
|
|
mCamera[0] = rotatedCamera.get0()/mObjectRatio;
|
263 |
|
|
mCamera[1] = rotatedCamera.get1()/mObjectRatio;
|
264 |
|
|
mCamera[2] = rotatedCamera.get2()/mObjectRatio;
|
265 |
|
|
|
266 |
b31249d6
|
Leszek Koltunski
|
float vx = mCamera[0]-mPoint[0];
|
267 |
|
|
float vy = mCamera[1]-mPoint[1];
|
268 |
|
|
float vz = mCamera[2]-mPoint[2];
|
269 |
a8efe86f
|
Leszek Koltunski
|
|
270 |
b31249d6
|
Leszek Koltunski
|
float R = 0.5f;
|
271 |
|
|
float A = vx*vx + vy*vy + vz*vz;
|
272 |
|
|
float B = 2*(vx*mCamera[0] + vy*mCamera[1] + vz*mCamera[2]);
|
273 |
|
|
float C = (mCamera[0]*mCamera[0] + mCamera[1]*mCamera[1] + mCamera[2]*mCamera[2]) - R*R;
|
274 |
54588439
|
Leszek Koltunski
|
|
275 |
f2259427
|
Leszek Koltunski
|
if( B*B >= 4*A*C )
|
276 |
54588439
|
Leszek Koltunski
|
{
|
277 |
|
|
computeLongitudeAndLatitude(A,B,C);
|
278 |
|
|
return true;
|
279 |
|
|
}
|
280 |
a8efe86f
|
Leszek Koltunski
|
|
281 |
54588439
|
Leszek Koltunski
|
return false;
|
282 |
5caf2641
|
Leszek Koltunski
|
}
|
283 |
|
|
|
284 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
285 |
|
|
|
286 |
|
|
public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
|
287 |
|
|
{
|
288 |
b31249d6
|
Leszek Koltunski
|
computeEnabledAxis();
|
289 |
bfdb9aa5
|
Leszek Koltunski
|
|
290 |
|
|
mTouch[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
291 |
|
|
mTouch[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
292 |
|
|
mTouch[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
293 |
|
|
|
294 |
|
|
float x = mTouch[0]-mPoint[0];
|
295 |
|
|
float y = mTouch[1]-mPoint[1];
|
296 |
|
|
float z = mTouch[2]-mPoint[2];
|
297 |
|
|
|
298 |
|
|
QuatHelper.rotateVectorByQuat(mTmp,x,y,z,0,quat);
|
299 |
|
|
mMove2D[0] = mTmp[0];
|
300 |
|
|
mMove2D[1] = mTmp[1];
|
301 |
|
|
|
302 |
|
|
for(int i=1; i<=mEnabledRotAxis[0]; i++)
|
303 |
|
|
{
|
304 |
|
|
int enabled = mEnabledRotAxis[i];
|
305 |
|
|
Static3D axis = mRotAxis[enabled];
|
306 |
|
|
float[] vector = mCastedRotAxis[enabled];
|
307 |
|
|
float bx = axis.get0();
|
308 |
|
|
float by = axis.get1();
|
309 |
|
|
float bz = axis.get2();
|
310 |
|
|
|
311 |
|
|
QuatHelper.rotateVectorByQuat(mTmp,bx,by,bz,0,quat);
|
312 |
|
|
float len = (float)Math.sqrt(mTmp[0]*mTmp[0] + mTmp[1]*mTmp[1]);
|
313 |
|
|
|
314 |
|
|
if( len<MIN_LEN )
|
315 |
|
|
{
|
316 |
|
|
vector[0] = 1000f; // switch off the axis because when casted
|
317 |
|
|
vector[1] = 1000f; // onto the screen it is too short
|
318 |
|
|
}
|
319 |
|
|
else
|
320 |
|
|
{
|
321 |
|
|
vector[0] = mTmp[0]/len;
|
322 |
|
|
vector[1] = mTmp[1]/len;
|
323 |
|
|
}
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
int rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabledRotAxis);
|
327 |
|
|
float offset = computeOffset(rotIndex);
|
328 |
|
|
int row = computeRowFromOffset(rotIndex,offset);
|
329 |
5caf2641
|
Leszek Koltunski
|
|
330 |
a8efe86f
|
Leszek Koltunski
|
output[0] = rotIndex;
|
331 |
|
|
output[1] = row;
|
332 |
5caf2641
|
Leszek Koltunski
|
}
|
333 |
|
|
|
334 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335 |
|
|
// simply cast the appropriate rotational axis of the object to the screen surface.
|
336 |
|
|
|
337 |
|
|
public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
|
338 |
|
|
{
|
339 |
|
|
Static3D a = mRotAxis[axisIndex];
|
340 |
|
|
Static4D result = QuatHelper.rotateVectorByQuat(a.get0(),a.get1(),a.get2(),0,quat);
|
341 |
|
|
|
342 |
|
|
float cx = result.get0();
|
343 |
|
|
float cy = result.get1();
|
344 |
|
|
float len= (float)Math.sqrt(cx*cx+cy*cy);
|
345 |
|
|
|
346 |
|
|
if( len!=0 )
|
347 |
|
|
{
|
348 |
|
|
output[0] = cx/len;
|
349 |
|
|
output[1] = cy/len;
|
350 |
|
|
}
|
351 |
|
|
else
|
352 |
|
|
{
|
353 |
|
|
output[0] = 1;
|
354 |
|
|
output[1] = 0;
|
355 |
|
|
}
|
356 |
|
|
}
|
357 |
a6aa9a47
|
Leszek Koltunski
|
|
358 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359 |
|
|
|
360 |
|
|
public boolean axisAndFaceAgree(int axisIndex)
|
361 |
|
|
{
|
362 |
|
|
return false;
|
363 |
|
|
}
|
364 |
4a014840
|
leszek
|
|
365 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366 |
|
|
|
367 |
93dc5a55
|
leszek
|
public void enableGhostAxis(int axNum, boolean enable)
|
368 |
4a014840
|
leszek
|
{
|
369 |
93dc5a55
|
leszek
|
mGhostAxisEnabled = enable ? axNum : -1;
|
370 |
4a014840
|
leszek
|
}
|
371 |
5caf2641
|
Leszek Koltunski
|
}
|