1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 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.magic;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.opengl.GLSurfaceView;
|
26
|
import android.util.AttributeSet;
|
27
|
import android.view.MotionEvent;
|
28
|
|
29
|
import org.distorted.library.type.Static4D;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
class RubikSurfaceView extends GLSurfaceView
|
34
|
{
|
35
|
// Moving the finger from the middle of the vertical screen to the right edge will rotate a
|
36
|
// given face by SWIPING_SENSITIVITY/2 degrees.
|
37
|
private final static int SWIPING_SENSITIVITY = 240;
|
38
|
|
39
|
// Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
|
40
|
private final static int ROTATION_SENSITIVITY = 12;
|
41
|
|
42
|
// Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
|
43
|
private final static int DIRECTION_SENSITIVITY= 12;
|
44
|
|
45
|
private final static int NONE =-1;
|
46
|
private final static int FRONT = 0; // has to be 6 consecutive ints
|
47
|
private final static int BACK = 1; // FRONT ... BOTTOM
|
48
|
private final static int LEFT = 2; //
|
49
|
private final static int RIGHT = 3; //
|
50
|
private final static int TOP = 4; //
|
51
|
private final static int BOTTOM = 5; //
|
52
|
|
53
|
private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
|
54
|
|
55
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
56
|
private int mX, mY;
|
57
|
private int mRotationVect;
|
58
|
private RubikRenderer mRenderer;
|
59
|
|
60
|
private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
|
61
|
private int mLastTouchedFace;
|
62
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
63
|
private float mCameraDistance;
|
64
|
|
65
|
private static Static4D mQuatCurrent = new Static4D(0,0,0,1);
|
66
|
private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
67
|
private static Static4D mTempCurrent = new Static4D(0,0,0,1);
|
68
|
private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
// return quat1*quat2
|
72
|
|
73
|
static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
74
|
{
|
75
|
float qx = quat1.get1();
|
76
|
float qy = quat1.get2();
|
77
|
float qz = quat1.get3();
|
78
|
float qw = quat1.get4();
|
79
|
|
80
|
float rx = quat2.get1();
|
81
|
float ry = quat2.get2();
|
82
|
float rz = quat2.get3();
|
83
|
float rw = quat2.get4();
|
84
|
|
85
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
86
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
87
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
88
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
89
|
|
90
|
return new Static4D(tx,ty,tz,tw);
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
95
|
|
96
|
static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
97
|
{
|
98
|
float qx = quat.get1();
|
99
|
float qy = quat.get2();
|
100
|
float qz = quat.get3();
|
101
|
float qw = quat.get4();
|
102
|
|
103
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
104
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
105
|
|
106
|
return quatMultiply(tmp,quat);
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
111
|
|
112
|
static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
113
|
{
|
114
|
float qx = quat.get1();
|
115
|
float qy = quat.get2();
|
116
|
float qz = quat.get3();
|
117
|
float qw = quat.get4();
|
118
|
|
119
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
120
|
Static4D tmp = quatMultiply(quat,vector);
|
121
|
|
122
|
return quatMultiply(tmp,quatInverted);
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private int faceTouched(int xTouch, int yTouch)
|
128
|
{
|
129
|
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
|
130
|
|
131
|
convertTouchPointToScreenSpace(xTouch,yTouch);
|
132
|
convertCameraPointToScreenSpace();
|
133
|
|
134
|
for(int face=FRONT; face<=BOTTOM; face++)
|
135
|
{
|
136
|
if( faceIsVisible(face,cubeHalfSize) )
|
137
|
{
|
138
|
castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
|
139
|
|
140
|
float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
|
141
|
float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
|
142
|
float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
|
143
|
|
144
|
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
|
145
|
}
|
146
|
}
|
147
|
|
148
|
return NONE;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
private void addNewRotation(int x, int y)
|
154
|
{
|
155
|
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
|
156
|
|
157
|
convertTouchPointToScreenSpace(x,y);
|
158
|
castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
|
159
|
|
160
|
mDiff[0] -= mTouchPointCastOntoFace[0];
|
161
|
mDiff[1] -= mTouchPointCastOntoFace[1];
|
162
|
mDiff[2] -= mTouchPointCastOntoFace[2];
|
163
|
|
164
|
int xAxis = retFaceXaxis(mLastTouchedFace);
|
165
|
int yAxis = retFaceYaxis(mLastTouchedFace);
|
166
|
mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
|
167
|
float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
|
168
|
|
169
|
mTouchPoint[0] = mPoint[0];
|
170
|
mTouchPoint[1] = mPoint[1];
|
171
|
mTouchPoint[2] = mPoint[2];
|
172
|
|
173
|
RubikCube cube = mRenderer.getCube();
|
174
|
cube.addNewRotation(mRotationVect, (int)(cube.getSize()*offset) );
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
private boolean isVertical(float x, float y)
|
180
|
{
|
181
|
return (y>x) ? (y>=-x) : (y< -x);
|
182
|
}
|
183
|
|
184
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185
|
|
186
|
private void continueRotation(int x, int y)
|
187
|
{
|
188
|
convertTouchPointToScreenSpace(x,y);
|
189
|
|
190
|
mDiff[0] = mPoint[0]-mTouchPoint[0];
|
191
|
mDiff[1] = mPoint[1]-mTouchPoint[1];
|
192
|
mDiff[2] = mPoint[2]-mTouchPoint[2];
|
193
|
|
194
|
int xAxis= retFaceXaxis(mLastTouchedFace);
|
195
|
int yAxis= retFaceYaxis(mLastTouchedFace);
|
196
|
int sign = retFaceRotationSign(mLastTouchedFace);
|
197
|
float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
|
198
|
|
199
|
mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*sign*angle/mScreenMin);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
private void finishRotation()
|
205
|
{
|
206
|
mRenderer.finishRotation();
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
private Static4D quatFromDrag(float dragX, float dragY)
|
212
|
{
|
213
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
214
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
215
|
float axisZ = 0;
|
216
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
217
|
|
218
|
if( axisL>0 )
|
219
|
{
|
220
|
axisX /= axisL;
|
221
|
axisY /= axisL;
|
222
|
axisZ /= axisL;
|
223
|
|
224
|
float ratio = axisL/mScreenMin;
|
225
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
226
|
|
227
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
228
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
229
|
|
230
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
231
|
}
|
232
|
|
233
|
return new Static4D(0f, 0f, 0f, 1f);
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private boolean faceIsVisible(int face, float cubeHalfSize)
|
239
|
{
|
240
|
int sign = retFaceSign(face);
|
241
|
int zAxis= retFaceZaxis(face);
|
242
|
|
243
|
return sign*mCamera[zAxis] > cubeHalfSize;
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
|
248
|
private void convertTouchPointToScreenSpace(int x, int y)
|
249
|
{
|
250
|
float halfScrWidth = mScreenWidth *0.5f;
|
251
|
float halfScrHeight = mScreenHeight*0.5f;
|
252
|
Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
|
253
|
Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
|
254
|
|
255
|
mPoint[0] = rotatedTouchPoint.get1();
|
256
|
mPoint[1] = rotatedTouchPoint.get2();
|
257
|
mPoint[2] = rotatedTouchPoint.get3();
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
private void convertCameraPointToScreenSpace()
|
263
|
{
|
264
|
Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
|
265
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
|
266
|
|
267
|
mCamera[0] = rotatedCamera.get1();
|
268
|
mCamera[1] = rotatedCamera.get2();
|
269
|
mCamera[2] = rotatedCamera.get3();
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
274
|
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
|
275
|
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
|
276
|
|
277
|
private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
|
278
|
{
|
279
|
int sign = retFaceSign(face);
|
280
|
int zAxis= retFaceZaxis(face);
|
281
|
float diff = mPoint[zAxis]-mCamera[zAxis];
|
282
|
|
283
|
float ratio = diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
|
284
|
|
285
|
output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
|
286
|
output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
|
287
|
output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
|
288
|
}
|
289
|
|
290
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
291
|
|
292
|
private int retFaceSign(int face)
|
293
|
{
|
294
|
return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
|
295
|
}
|
296
|
|
297
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
298
|
|
299
|
private int retFaceRotationSign(int face)
|
300
|
{
|
301
|
return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
|
306
|
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
|
307
|
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
|
308
|
|
309
|
private int retFaceXaxis(int face)
|
310
|
{
|
311
|
switch(face)
|
312
|
{
|
313
|
case FRONT :
|
314
|
case BACK : return RubikCube.VECTX;
|
315
|
case LEFT :
|
316
|
case RIGHT : return RubikCube.VECTZ;
|
317
|
case TOP :
|
318
|
case BOTTOM: return RubikCube.VECTX;
|
319
|
}
|
320
|
|
321
|
return -1;
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
private int retFaceYaxis(int face)
|
327
|
{
|
328
|
switch(face)
|
329
|
{
|
330
|
case FRONT :
|
331
|
case BACK : return RubikCube.VECTY;
|
332
|
case LEFT :
|
333
|
case RIGHT : return RubikCube.VECTY;
|
334
|
case TOP :
|
335
|
case BOTTOM: return RubikCube.VECTZ;
|
336
|
}
|
337
|
|
338
|
return -1;
|
339
|
}
|
340
|
|
341
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
342
|
|
343
|
private int retFaceZaxis(int face)
|
344
|
{
|
345
|
switch(face)
|
346
|
{
|
347
|
case FRONT :
|
348
|
case BACK : return RubikCube.VECTZ;
|
349
|
case LEFT :
|
350
|
case RIGHT : return RubikCube.VECTX;
|
351
|
case TOP :
|
352
|
case BOTTOM: return RubikCube.VECTY;
|
353
|
}
|
354
|
|
355
|
return -1;
|
356
|
}
|
357
|
|
358
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359
|
|
360
|
void setQuatAccumulated()
|
361
|
{
|
362
|
mQuatAccumulated.set(mTempAccumulated);
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
|
367
|
void setQuatCurrent()
|
368
|
{
|
369
|
mQuatCurrent.set(mTempCurrent);
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
|
374
|
Static4D getQuatAccumulated()
|
375
|
{
|
376
|
return mQuatAccumulated;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
Static4D getQuatCurrent()
|
382
|
{
|
383
|
return mQuatCurrent;
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
|
388
|
RubikRenderer getRenderer()
|
389
|
{
|
390
|
return mRenderer;
|
391
|
}
|
392
|
|
393
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
394
|
|
395
|
void setScreenSize(int width, int height)
|
396
|
{
|
397
|
mScreenWidth = width;
|
398
|
mScreenHeight= height;
|
399
|
|
400
|
mScreenMin = width<height ? width:height;
|
401
|
}
|
402
|
|
403
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
404
|
|
405
|
void setCameraDist(float distance)
|
406
|
{
|
407
|
mCameraDistance = distance;
|
408
|
}
|
409
|
|
410
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
411
|
// PUBLIC API
|
412
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
413
|
|
414
|
public RubikSurfaceView(Context context, AttributeSet attrs)
|
415
|
{
|
416
|
super(context,attrs);
|
417
|
|
418
|
if(!isInEditMode())
|
419
|
{
|
420
|
mRotationVect = VECT[0];
|
421
|
|
422
|
mPoint = new float[3];
|
423
|
mCamera= new float[3];
|
424
|
mDiff = new float[3];
|
425
|
mTouchPoint = new float[3];
|
426
|
mTouchPointCastOntoFace = new float[3];
|
427
|
|
428
|
mScreenWidth = mScreenHeight = mScreenMin = 0;
|
429
|
|
430
|
mRenderer = new RubikRenderer(this);
|
431
|
|
432
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
433
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
434
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
435
|
setRenderer(mRenderer);
|
436
|
}
|
437
|
}
|
438
|
|
439
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
440
|
|
441
|
@Override
|
442
|
public boolean onTouchEvent(MotionEvent event)
|
443
|
{
|
444
|
int action = event.getAction();
|
445
|
int x = (int)event.getX();
|
446
|
int y = (int)event.getY();
|
447
|
|
448
|
switch(action)
|
449
|
{
|
450
|
case MotionEvent.ACTION_DOWN: mX = x;
|
451
|
mY = y;
|
452
|
mLastTouchedFace = faceTouched(x,y);
|
453
|
|
454
|
if( mLastTouchedFace != NONE )
|
455
|
{
|
456
|
mDragging = false;
|
457
|
mBeginningRotation = mRenderer.canRotate();
|
458
|
mContinuingRotation = false;
|
459
|
}
|
460
|
else
|
461
|
{
|
462
|
mDragging = mRenderer.canDrag();
|
463
|
mBeginningRotation = false;
|
464
|
mContinuingRotation = false;
|
465
|
}
|
466
|
break;
|
467
|
case MotionEvent.ACTION_MOVE: if( mDragging )
|
468
|
{
|
469
|
mTempCurrent.set(quatFromDrag(mX-x,mY-y));
|
470
|
mRenderer.setQuatCurrentOnNextRender();
|
471
|
|
472
|
int minimumDist = (mScreenMin*mScreenMin)/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY);
|
473
|
|
474
|
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > minimumDist )
|
475
|
{
|
476
|
mX = x;
|
477
|
mY = y;
|
478
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
479
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
480
|
mRenderer.setQuatCurrentOnNextRender();
|
481
|
mRenderer.setQuatAccumulatedOnNextRender();
|
482
|
}
|
483
|
}
|
484
|
if( mBeginningRotation )
|
485
|
{
|
486
|
int minimumDistToStartRotating = (mScreenMin*mScreenMin)/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
|
487
|
|
488
|
if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
|
489
|
{
|
490
|
addNewRotation(x,y);
|
491
|
mBeginningRotation = false;
|
492
|
mContinuingRotation= true;
|
493
|
}
|
494
|
}
|
495
|
else if( mContinuingRotation )
|
496
|
{
|
497
|
continueRotation(x,y);
|
498
|
}
|
499
|
break;
|
500
|
case MotionEvent.ACTION_UP : if( mDragging )
|
501
|
{
|
502
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
503
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
504
|
mRenderer.setQuatCurrentOnNextRender();
|
505
|
mRenderer.setQuatAccumulatedOnNextRender();
|
506
|
}
|
507
|
|
508
|
if( mContinuingRotation )
|
509
|
{
|
510
|
finishRotation();
|
511
|
}
|
512
|
|
513
|
break;
|
514
|
}
|
515
|
|
516
|
return true;
|
517
|
}
|
518
|
}
|
519
|
|