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