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