Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 5560eea9

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
    static final int VECTX = 0;  //
51
    static final int VECTY = 1;  // don't change this
52
    static final int VECTZ = 2;  //
53

    
54
    private static final int[] VECT = {VECTX,VECTY,VECTZ};
55

    
56
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
57
    private int mX, mY;
58
    private int mRotationVect;
59
    private RubikRenderer mRenderer;
60

    
61
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
62
    private int mLastTouchedFace;
63
    private int mScreenWidth, mScreenHeight, mScreenMin;
64
    private float mCameraDistance;
65

    
66
    private static Static4D mQuatCurrent    =new Static4D(0,0,0,1);
67
    private static Static4D mQuatAccumulated=new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    public RubikSurfaceView(Context context, AttributeSet attrs)
72
      {
73
      super(context,attrs);
74

    
75
      if(!isInEditMode())
76
        {
77
        mRotationVect = VECT[0];
78

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

    
85
        mScreenWidth = mScreenHeight = mScreenMin = 0;
86

    
87
        mRenderer = new RubikRenderer(this);
88
        mRenderer.setQuatAccumulated(mQuatAccumulated);
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
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
128
                                         mRenderer.setQuatCurrent(mQuatCurrent);
129
                                         }
130
                                       if( mBeginningRotation )
131
                                         {
132
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
133

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

    
154
                                       if( mContinuingRotation )
155
                                         {
156
                                         finishRotation();
157
                                         }
158

    
159
                                       break;
160
         }
161

    
162
      return true;
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    RubikRenderer getRenderer()
168
      {
169
      return mRenderer;
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    void setScreenSize(int width, int height)
175
      {
176
      mScreenWidth = width;
177
      mScreenHeight= height;
178

    
179
      mScreenMin = width<height ? width:height;
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    void setCameraDist(float distance)
185
      {
186
      mCameraDistance = distance;
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    private int faceTouched(int xTouch, int yTouch)
192
      {
193
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
194

    
195
      convertTouchPointToScreenSpace(xTouch,yTouch);
196
      convertCameraPointToScreenSpace();
197

    
198
      for(int face=FRONT; face<=BOTTOM; face++)
199
        {
200
        if( faceIsVisible(face,cubeHalfSize) )
201
          {
202
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
203

    
204
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
205
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
206
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
207

    
208
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
209
          }
210
        }
211

    
212
      return NONE;
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    private void addNewRotation(int x, int y)
218
      {
219
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
220

    
221
      convertTouchPointToScreenSpace(x,y);
222
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
223

    
224
      mDiff[0] -= mTouchPointCastOntoFace[0];
225
      mDiff[1] -= mTouchPointCastOntoFace[1];
226
      mDiff[2] -= mTouchPointCastOntoFace[2];
227

    
228
      int xAxis = retFaceXaxis(mLastTouchedFace);
229
      int yAxis = retFaceYaxis(mLastTouchedFace);
230
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
231
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
232

    
233
      mTouchPoint[0] = mPoint[0];
234
      mTouchPoint[1] = mPoint[1];
235
      mTouchPoint[2] = mPoint[2];
236

    
237
      mRenderer.getCube().addNewRotation(mRotationVect,offset);
238
      }
239

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

    
242
    private boolean isVertical(float x, float y)
243
      {
244
      return (y>x) ? (y>=-x) : (y< -x);
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
    private void continueRotation(int x, int y)
250
      {
251
      convertTouchPointToScreenSpace(x,y);
252

    
253
      mDiff[0] = mPoint[0]-mTouchPoint[0];
254
      mDiff[1] = mPoint[1]-mTouchPoint[1];
255
      mDiff[2] = mPoint[2]-mTouchPoint[2];
256

    
257
      int xAxis= retFaceXaxis(mLastTouchedFace);
258
      int yAxis= retFaceYaxis(mLastTouchedFace);
259
      int sign = retFaceRotationSign(mLastTouchedFace);
260
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
261

    
262
      mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*sign*angle/mScreenMin);
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    private void finishRotation()
268
      {
269
      mRenderer.finishRotation();
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// return quat1*quat2
274

    
275
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
276
      {
277
      float qx = quat1.get1();
278
      float qy = quat1.get2();
279
      float qz = quat1.get3();
280
      float qw = quat1.get4();
281

    
282
      float rx = quat2.get1();
283
      float ry = quat2.get2();
284
      float rz = quat2.get3();
285
      float rw = quat2.get4();
286

    
287
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
288
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
289
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
290
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
291

    
292
      return new Static4D(tx,ty,tz,tw);
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
297

    
298
    static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
299
      {
300
      float qx = quat.get1();
301
      float qy = quat.get2();
302
      float qz = quat.get3();
303
      float qw = quat.get4();
304

    
305
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
306
      Static4D tmp = quatMultiply(quatInverted,vector);
307

    
308
      return quatMultiply(tmp,quat);
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
313

    
314
    static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
315
      {
316
      float qx = quat.get1();
317
      float qy = quat.get2();
318
      float qz = quat.get3();
319
      float qw = quat.get4();
320

    
321
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
322
      Static4D tmp = quatMultiply(quat,vector);
323

    
324
      return quatMultiply(tmp,quatInverted);
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    private Static4D quatFromDrag(float dragX, float dragY)
330
      {
331
      float axisX = dragY;  // inverted X and Y - rotation axis is
332
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
333
      float axisZ = 0;
334
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
335

    
336
      if( axisL>0 )
337
        {
338
        axisX /= axisL;
339
        axisY /= axisL;
340
        axisZ /= axisL;
341

    
342
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
343
        float sinA = (float)Math.sqrt(1-cosA*cosA);
344

    
345
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
346
        }
347

    
348
      return new Static4D(0f, 0f, 0f, 1f);
349
      }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
    private boolean faceIsVisible(int face, float cubeHalfSize)
354
      {
355
      int sign = retFaceSign(face);
356
      int zAxis= retFaceZaxis(face);
357

    
358
      return sign*mCamera[zAxis] > cubeHalfSize;
359
      }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
    private void convertTouchPointToScreenSpace(int x, int y)
364
      {
365
      float halfScrWidth  = mScreenWidth *0.5f;
366
      float halfScrHeight = mScreenHeight*0.5f;
367
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
368
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
369

    
370
      mPoint[0] = rotatedTouchPoint.get1();
371
      mPoint[1] = rotatedTouchPoint.get2();
372
      mPoint[2] = rotatedTouchPoint.get3();
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    private void convertCameraPointToScreenSpace()
378
      {
379
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
380
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
381

    
382
      mCamera[0] = rotatedCamera.get1();
383
      mCamera[1] = rotatedCamera.get2();
384
      mCamera[2] = rotatedCamera.get3();
385
      }
386

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

    
392
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
393
      {
394
      int sign = retFaceSign(face);
395
      int zAxis= retFaceZaxis(face);
396
      float diff = mPoint[zAxis]-mCamera[zAxis];
397

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

    
400
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
401
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
402
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
403
      }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    private int retFaceSign(int face)
408
      {
409
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
410
      }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
    private int retFaceRotationSign(int face)
415
      {
416
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
417
      }
418

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

    
424
    private int retFaceXaxis(int face)
425
      {
426
      switch(face)
427
        {
428
        case FRONT :
429
        case BACK  : return VECTX;
430
        case LEFT  :
431
        case RIGHT : return VECTZ;
432
        case TOP   :
433
        case BOTTOM: return VECTX;
434
        }
435

    
436
      return -1;
437
      }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
    private int retFaceYaxis(int face)
442
      {
443
      switch(face)
444
        {
445
        case FRONT :
446
        case BACK  : return VECTY;
447
        case LEFT  :
448
        case RIGHT : return VECTY;
449
        case TOP   :
450
        case BOTTOM: return VECTZ;
451
        }
452

    
453
      return -1;
454
      }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
    private int retFaceZaxis(int face)
459
      {
460
      switch(face)
461
        {
462
        case FRONT :
463
        case BACK  : return VECTZ;
464
        case LEFT  :
465
        case RIGHT : return VECTX;
466
        case TOP   :
467
        case BOTTOM: return VECTY;
468
        }
469

    
470
      return -1;
471
      }
472
}
473

    
(6-6/6)