Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ a42e25a6

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.Static2D;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.object.RubikObject;
33
import org.distorted.object.RubikObjectMovement;
34
import org.distorted.uistate.RubikState;
35
import org.distorted.uistate.RubikStateSolving;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikSurfaceView extends GLSurfaceView
40
{
41
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
42
    // given face by SWIPING_SENSITIVITY/2 degrees.
43
    private final static int SWIPING_SENSITIVITY  = 240;
44
    // Moving the finger by 1/15 the distance of min(scrWidth,scrHeight) will start a Rotation.
45
    private final static int ROTATION_SENSITIVITY =  15;
46
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
47
    private final static int DIRECTION_SENSITIVITY=  12;
48

    
49
    // Where did we get this sqrt(3)/2 ? From the (default, i.e. 60 degrees - see InternalOutputSurface!)
50
    // FOV of the projection matrix of the Node onto the Screen.
51
    // Take a look how the CAMERA_POINT is used in onTouchEvent - (x,y) there are expressed in sort of
52
    // 'half-NDC' coordinates i.e. they range from +0.5 to -0.5; thus CAMERA_POINT also needs to be
53
    // in 'half-NDC'. Since in this coordinate system the height of the screen is equal to 1, then the
54
    // Z-distance from the center of the object to the camera is equal to (scrHeight/2)/tan(FOV/2) =
55
    // 0.5/tan(30) = sqrt(3)/2.
56
    // Why is the Z-distance between the camera and the object equal to (scrHeight/2)/tan(FOV/2)?
57
    // Because of the way the View part of the ModelView matrix is constructed in EffectQueueMatrix.send().
58
    private final Static4D CAMERA_POINT = new Static4D(0, 0, (float)Math.sqrt(3)*0.5f, 0);
59

    
60
    private RubikRenderer mRenderer;
61
    private RubikPostRender mPostRender;
62
    private RubikObjectMovement mMovement;
63
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
64
    private int mScreenWidth, mScreenHeight, mScreenMin;
65

    
66
    private float mX, mY;
67
    private float mStartRotX, mStartRotY;
68
    private float mAxisX, mAxisY;
69
    private float mRotationFactor;
70

    
71
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
72
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
73
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
74
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    void setScreenSize(int width, int height)
79
      {
80
      mScreenWidth = width;
81
      mScreenHeight= height;
82

    
83
      mScreenMin = Math.min(width, height);
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
    boolean isVertical()
89
      {
90
      return mScreenHeight>mScreenWidth;
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    RubikRenderer getRenderer()
96
      {
97
      return mRenderer;
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
    RubikPostRender getPostRender()
103
      {
104
      return mPostRender;
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
    void setQuatAccumulated()
110
      {
111
      mQuatAccumulated.set(mTempAccumulated);
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
    void setQuatCurrent()
117
      {
118
      mQuatCurrent.set(mTempCurrent);
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    Static4D getQuatAccumulated()
124
      {
125
      return mQuatAccumulated;
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    Static4D getQuatCurrent()
131
      {
132
      return mQuatCurrent;
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
    void setMovement(RubikObjectMovement movement)
138
      {
139
      mMovement = movement;
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    private Static4D quatFromDrag(float dragX, float dragY)
145
      {
146
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
147
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
148
      float axisZ = 0;
149
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
150

    
151
      if( axisL>0 )
152
        {
153
        axisX /= axisL;
154
        axisY /= axisL;
155
        axisZ /= axisL;
156

    
157
        float ratio = axisL;
158
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
159

    
160
        float cosA = (float)Math.cos(Math.PI*ratio);
161
        float sinA = (float)Math.sqrt(1-cosA*cosA);
162

    
163
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
164
        }
165

    
166
      return new Static4D(0f, 0f, 0f, 1f);
167
      }
168

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

    
171
    private void setUpDragOrRotate(float x, float y)
172
      {
173
      if( !RubikState.canRotate() )
174
        {
175
        mDragging           = true;
176
        mBeginningRotation  = false;
177
        mContinuingRotation = false;
178
        }
179
      else
180
        {
181
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
182
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
183
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
184

    
185
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
186
          {
187
          mDragging           = false;
188
          mBeginningRotation  = mPostRender.canRotate();
189
          mContinuingRotation = false;
190
          }
191
        else
192
          {
193
          mDragging           = true;
194
          mBeginningRotation  = false;
195
          mContinuingRotation = false;
196
          }
197
        }
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
202

    
203
    private void computeCurrentAxis(Static3D axis)
204
      {
205
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
206
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
207

    
208
      mAxisX =result.get0();
209
      mAxisY =result.get1();
210

    
211
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
212
      mAxisX /= len;
213
      mAxisY /= len;
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    private float continueRotation(float dx, float dy)
219
      {
220
      float alpha = dx*mAxisX + dy*mAxisY;
221
      float x = dx - alpha*mAxisX;
222
      float y = dy - alpha*mAxisY;
223

    
224
      float len = (float)Math.sqrt(x*x + y*y);
225

    
226
      // we have the length of 1D vector 'angle', now the direction:
227
      float tmp = mAxisY==0 ? -mAxisX*y : mAxisY*x;
228

    
229
      return (tmp>0 ? 1:-1)*len*mRotationFactor;
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
// return quat1*quat2
234

    
235
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
236
      {
237
      float qx = quat1.get0();
238
      float qy = quat1.get1();
239
      float qz = quat1.get2();
240
      float qw = quat1.get3();
241

    
242
      float rx = quat2.get0();
243
      float ry = quat2.get1();
244
      float rz = quat2.get2();
245
      float rw = quat2.get3();
246

    
247
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
248
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
249
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
250
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
251

    
252
      return new Static4D(tx,ty,tz,tw);
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
257

    
258
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
259
      {
260
      float qx = quat.get0();
261
      float qy = quat.get1();
262
      float qz = quat.get2();
263
      float qw = quat.get3();
264

    
265
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
266
      Static4D tmp = quatMultiply(quat,vector);
267

    
268
      return quatMultiply(tmp,quatInverted);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
273

    
274
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
275
      {
276
      float qx = quat.get0();
277
      float qy = quat.get1();
278
      float qz = quat.get2();
279
      float qw = quat.get3();
280

    
281
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
282
      Static4D tmp = quatMultiply(quatInverted,vector);
283

    
284
      return quatMultiply(tmp,quat);
285
      }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// PUBLIC API
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    public RubikSurfaceView(Context context, AttributeSet attrs)
292
      {
293
      super(context,attrs);
294

    
295
      if(!isInEditMode())
296
        {
297
        mRenderer   = new RubikRenderer(this);
298
        mPostRender = new RubikPostRender(this);
299

    
300
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
301
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
302
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
303
        setRenderer(mRenderer);
304
        }
305
      }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
    @Override
310
    public boolean onTouchEvent(MotionEvent event)
311
      {
312
      int action = event.getAction();
313
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
314
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
315

    
316
      switch(action)
317
         {
318
         case MotionEvent.ACTION_DOWN: mX = x;
319
                                       mY = y;
320
                                       setUpDragOrRotate(x,y);
321
                                       break;
322
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
323
                                         {
324
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
325
                                           {
326
                                           mStartRotX = x;
327
                                           mStartRotY = y;
328

    
329
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
330
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
331

    
332
                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
333
                                           RubikObject object = mPostRender.getObject();
334

    
335
                                           int axis = (int)res.get0();
336
                                           float offset = res.get1();
337
                                           computeCurrentAxis( object.getRotationAxis()[axis] );
338
                                           mRotationFactor = object.returnRotationFactor(offset);
339

    
340
                                           object.beginNewRotation( axis, object.returnRowFromOffset(offset) );
341

    
342
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
343
                                             {
344
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
345
                                             solving.startCounting( (RubikActivity)getContext() );
346
                                             }
347

    
348
                                           mBeginningRotation = false;
349
                                           mContinuingRotation= true;
350
                                           }
351
                                         }
352
                                       else if( mContinuingRotation )
353
                                         {
354
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
355
                                         mPostRender.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
356
                                         }
357
                                       else if( mDragging )
358
                                         {
359
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
360
                                         mPostRender.setQuatCurrentOnNextRender();
361

    
362
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
363
                                           {
364
                                           mX = x;
365
                                           mY = y;
366
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
367
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
368
                                           mPostRender.setQuatCurrentOnNextRender();
369
                                           mPostRender.setQuatAccumulatedOnNextRender();
370
                                           }
371
                                         }
372
                                       else
373
                                         {
374
                                         setUpDragOrRotate(x,y);
375
                                         }
376
                                       break;
377
         case MotionEvent.ACTION_UP  : if( mDragging )
378
                                         {
379
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
380
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
381
                                         mPostRender.setQuatCurrentOnNextRender();
382
                                         mPostRender.setQuatAccumulatedOnNextRender();
383
                                         }
384

    
385
                                       if( mContinuingRotation )
386
                                         {
387
                                         mPostRender.finishRotation();
388
                                         }
389
                                       break;
390
         }
391

    
392
      return true;
393
      }
394
}
395

    
(4-4/4)