Project

General

Profile

Download (18.6 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 45686da2

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
    private final static int NONE   =-1;
43
    private final static int FRONT  = 0;  // has to be 6 consecutive ints
44
    private final static int BACK   = 1;  // FRONT ... BOTTOM
45
    private final static int LEFT   = 2;  //
46
    private final static int RIGHT  = 3;  //
47
    private final static int TOP    = 4;  //
48
    private final static int BOTTOM = 5;  //
49

    
50
    private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
51

    
52
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
53
    private int mX, mY;
54
    private int mRotationVect;
55
    private RubikRenderer mRenderer;
56

    
57
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
58
    private int mLastTouchedFace;
59
    private int mScreenWidth, mScreenHeight, mScreenMin;
60
    private float mCameraDistance;
61

    
62
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
63
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
64
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
65
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    public RubikSurfaceView(Context context, AttributeSet attrs)
70
      {
71
      super(context,attrs);
72

    
73
      if(!isInEditMode())
74
        {
75
        mRotationVect = VECT[0];
76

    
77
        mPoint = new float[3];
78
        mCamera= new float[3];
79
        mDiff  = new float[3];
80
        mTouchPoint = new float[3];
81
        mTouchPointCastOntoFace = new float[3];
82

    
83
        mScreenWidth = mScreenHeight = mScreenMin = 0;
84

    
85
        mRenderer = new RubikRenderer(this);
86

    
87
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
88
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
89
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
90
        setRenderer(mRenderer);
91
        }
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    @Override
97
    public boolean onTouchEvent(MotionEvent event)
98
      {
99
      int action = event.getAction();
100
      int x = (int)event.getX();
101
      int y = (int)event.getY();
102

    
103
      switch(action)
104
         {
105
         case MotionEvent.ACTION_DOWN: mX = x;
106
                                       mY = y;
107
                                       mLastTouchedFace = faceTouched(x,y);
108

    
109
                                       if( mLastTouchedFace != NONE )
110
                                         {
111
                                         mDragging           = false;
112
                                         mBeginningRotation  = mRenderer.canRotate();
113
                                         mContinuingRotation = false;
114
                                         }
115
                                       else
116
                                         {
117
                                         mDragging           = mRenderer.canDrag();
118
                                         mBeginningRotation  = false;
119
                                         mContinuingRotation = false;
120
                                         }
121
                                       break;
122
         case MotionEvent.ACTION_MOVE: if( mDragging )
123
                                         {
124
                                         mTempCurrent.set(quatFromDrag(mX-x,mY-y));
125
                                         mRenderer.setQuatCurrentOnNextRender();
126
                                         }
127
                                       if( mBeginningRotation )
128
                                         {
129
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
130

    
131
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
132
                                           {
133
                                           addNewRotation(x,y);
134
                                           mBeginningRotation = false;
135
                                           mContinuingRotation= true;
136
                                           }
137
                                         }
138
                                       else if( mContinuingRotation )
139
                                         {
140
                                         continueRotation(x,y);
141
                                         }
142
                                       break;
143
         case MotionEvent.ACTION_UP  : if( mDragging )
144
                                         {
145
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
146
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
147
                                         mRenderer.setQuatCurrentOnNextRender();
148
                                         mRenderer.setQuatAccumulatedOnNextRender();
149
                                         }
150

    
151
                                       if( mContinuingRotation )
152
                                         {
153
                                         finishRotation();
154
                                         }
155

    
156
                                       break;
157
         }
158

    
159
      return true;
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
    void setQuatAccumulated()
165
      {
166
      mQuatAccumulated.set(mTempAccumulated);
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    void setQuatCurrent()
172
      {
173
      mQuatCurrent.set(mTempCurrent);
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
    Static4D getQuatAccumulated()
179
      {
180
      return mQuatAccumulated;
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    Static4D getQuatCurrent()
186
      {
187
      return mQuatCurrent;
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    RubikRenderer getRenderer()
193
      {
194
      return mRenderer;
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    void setScreenSize(int width, int height)
200
      {
201
      mScreenWidth = width;
202
      mScreenHeight= height;
203

    
204
      mScreenMin = width<height ? width:height;
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    void setCameraDist(float distance)
210
      {
211
      mCameraDistance = distance;
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    private int faceTouched(int xTouch, int yTouch)
217
      {
218
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
219

    
220
      convertTouchPointToScreenSpace(xTouch,yTouch);
221
      convertCameraPointToScreenSpace();
222

    
223
      for(int face=FRONT; face<=BOTTOM; face++)
224
        {
225
        if( faceIsVisible(face,cubeHalfSize) )
226
          {
227
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
228

    
229
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
230
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
231
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
232

    
233
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
234
          }
235
        }
236

    
237
      return NONE;
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    private void addNewRotation(int x, int y)
243
      {
244
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
245

    
246
      convertTouchPointToScreenSpace(x,y);
247
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
248

    
249
      mDiff[0] -= mTouchPointCastOntoFace[0];
250
      mDiff[1] -= mTouchPointCastOntoFace[1];
251
      mDiff[2] -= mTouchPointCastOntoFace[2];
252

    
253
      int xAxis = retFaceXaxis(mLastTouchedFace);
254
      int yAxis = retFaceYaxis(mLastTouchedFace);
255
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
256
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
257

    
258
      mTouchPoint[0] = mPoint[0];
259
      mTouchPoint[1] = mPoint[1];
260
      mTouchPoint[2] = mPoint[2];
261

    
262
      RubikCube cube = mRenderer.getCube();
263
      cube.addNewRotation(mRotationVect, (int)(cube.getSize()*offset) );
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    private boolean isVertical(float x, float y)
269
      {
270
      return (y>x) ? (y>=-x) : (y< -x);
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    private void continueRotation(int x, int y)
276
      {
277
      convertTouchPointToScreenSpace(x,y);
278

    
279
      mDiff[0] = mPoint[0]-mTouchPoint[0];
280
      mDiff[1] = mPoint[1]-mTouchPoint[1];
281
      mDiff[2] = mPoint[2]-mTouchPoint[2];
282

    
283
      int xAxis= retFaceXaxis(mLastTouchedFace);
284
      int yAxis= retFaceYaxis(mLastTouchedFace);
285
      int sign = retFaceRotationSign(mLastTouchedFace);
286
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
287

    
288
      mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*sign*angle/mScreenMin);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    private void finishRotation()
294
      {
295
      mRenderer.finishRotation();
296
      }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// return quat1*quat2
300

    
301
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
302
      {
303
      float qx = quat1.get1();
304
      float qy = quat1.get2();
305
      float qz = quat1.get3();
306
      float qw = quat1.get4();
307

    
308
      float rx = quat2.get1();
309
      float ry = quat2.get2();
310
      float rz = quat2.get3();
311
      float rw = quat2.get4();
312

    
313
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
314
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
315
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
316
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
317

    
318
      return new Static4D(tx,ty,tz,tw);
319
      }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
323

    
324
    static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
325
      {
326
      float qx = quat.get1();
327
      float qy = quat.get2();
328
      float qz = quat.get3();
329
      float qw = quat.get4();
330

    
331
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
332
      Static4D tmp = quatMultiply(quatInverted,vector);
333

    
334
      return quatMultiply(tmp,quat);
335
      }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
339

    
340
    static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
341
      {
342
      float qx = quat.get1();
343
      float qy = quat.get2();
344
      float qz = quat.get3();
345
      float qw = quat.get4();
346

    
347
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
348
      Static4D tmp = quatMultiply(quat,vector);
349

    
350
      return quatMultiply(tmp,quatInverted);
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    private Static4D quatFromDrag(float dragX, float dragY)
356
      {
357
      float axisX = dragY;  // inverted X and Y - rotation axis is
358
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
359
      float axisZ = 0;
360
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
361

    
362
      if( axisL>0 )
363
        {
364
        axisX /= axisL;
365
        axisY /= axisL;
366
        axisZ /= axisL;
367

    
368
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
369
        float sinA = (float)Math.sqrt(1-cosA*cosA);
370

    
371
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
372
        }
373

    
374
      return new Static4D(0f, 0f, 0f, 1f);
375
      }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
    private boolean faceIsVisible(int face, float cubeHalfSize)
380
      {
381
      int sign = retFaceSign(face);
382
      int zAxis= retFaceZaxis(face);
383

    
384
      return sign*mCamera[zAxis] > cubeHalfSize;
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
    private void convertTouchPointToScreenSpace(int x, int y)
390
      {
391
      float halfScrWidth  = mScreenWidth *0.5f;
392
      float halfScrHeight = mScreenHeight*0.5f;
393
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
394
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
395

    
396
      mPoint[0] = rotatedTouchPoint.get1();
397
      mPoint[1] = rotatedTouchPoint.get2();
398
      mPoint[2] = rotatedTouchPoint.get3();
399
      }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
    private void convertCameraPointToScreenSpace()
404
      {
405
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
406
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
407

    
408
      mCamera[0] = rotatedCamera.get1();
409
      mCamera[1] = rotatedCamera.get2();
410
      mCamera[2] = rotatedCamera.get3();
411
      }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
415
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
416
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
417

    
418
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
419
      {
420
      int sign = retFaceSign(face);
421
      int zAxis= retFaceZaxis(face);
422
      float diff = mPoint[zAxis]-mCamera[zAxis];
423

    
424
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
425

    
426
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
427
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
428
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
429
      }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
    private int retFaceSign(int face)
434
      {
435
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
436
      }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
    private int retFaceRotationSign(int face)
441
      {
442
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
443
      }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
447
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
448
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
449

    
450
    private int retFaceXaxis(int face)
451
      {
452
      switch(face)
453
        {
454
        case FRONT :
455
        case BACK  : return RubikCube.VECTX;
456
        case LEFT  :
457
        case RIGHT : return RubikCube.VECTZ;
458
        case TOP   :
459
        case BOTTOM: return RubikCube.VECTX;
460
        }
461

    
462
      return -1;
463
      }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
    private int retFaceYaxis(int face)
468
      {
469
      switch(face)
470
        {
471
        case FRONT :
472
        case BACK  : return RubikCube.VECTY;
473
        case LEFT  :
474
        case RIGHT : return RubikCube.VECTY;
475
        case TOP   :
476
        case BOTTOM: return RubikCube.VECTZ;
477
        }
478

    
479
      return -1;
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
    private int retFaceZaxis(int face)
485
      {
486
      switch(face)
487
        {
488
        case FRONT :
489
        case BACK  : return RubikCube.VECTZ;
490
        case LEFT  :
491
        case RIGHT : return RubikCube.VECTX;
492
        case TOP   :
493
        case BOTTOM: return RubikCube.VECTY;
494
        }
495

    
496
      return -1;
497
      }
498
}
499

    
(6-6/6)