Project

General

Profile

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

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

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 775e675d Leszek Koltunski
import org.distorted.library.type.Static2D;
30 12ad3fca Leszek Koltunski
import org.distorted.library.type.Static3D;
31 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
32 27a70eae Leszek Koltunski
import org.distorted.object.RubikObject;
33
import org.distorted.object.RubikObjectMovement;
34 0333d81e Leszek Koltunski
import org.distorted.uistate.RubikState;
35
import org.distorted.uistate.RubikStateSolving;
36 0c52af30 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
40 0c52af30 Leszek Koltunski
{
41 775e675d Leszek Koltunski
    // 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 14a4712f Leszek Koltunski
    // 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 eac805bd Leszek Koltunski
    // 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 84ddf691 Leszek Koltunski
    // 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 86c73a69 Leszek Koltunski
60 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
61 27a70eae Leszek Koltunski
    private RubikObjectMovement mMovement;
62 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
63 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
64 0c52af30 Leszek Koltunski
65 12ad3fca Leszek Koltunski
    private float mX, mY;
66
    private float mStartRotX, mStartRotY;
67
    private float mAxisX, mAxisY;
68 e46e17fb Leszek Koltunski
    private float mRotationFactor;
69 12ad3fca Leszek Koltunski
70 45686da2 Leszek Koltunski
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
71
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
72
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
73
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
74 ee5c2ae1 Leszek Koltunski
75 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
    void setScreenSize(int width, int height)
78
      {
79
      mScreenWidth = width;
80
      mScreenHeight= height;
81
82 12ad3fca Leszek Koltunski
      mScreenMin = Math.min(width, height);
83 0fd3ac1f Leszek Koltunski
      }
84
85 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
    boolean isVertical()
88
      {
89
      return mScreenHeight>mScreenWidth;
90
      }
91
92 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
    RubikRenderer getRenderer()
95
      {
96
      return mRenderer;
97
      }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
    void setQuatAccumulated()
102
      {
103
      mQuatAccumulated.set(mTempAccumulated);
104
      }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
    void setQuatCurrent()
109
      {
110
      mQuatCurrent.set(mTempCurrent);
111
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
    Static4D getQuatAccumulated()
116
      {
117
      return mQuatAccumulated;
118
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
    Static4D getQuatCurrent()
123
      {
124
      return mQuatCurrent;
125
      }
126
127 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
    void setMovement(RubikObjectMovement movement)
130
      {
131
      mMovement = movement;
132
      }
133
134 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
    private Static4D quatFromDrag(float dragX, float dragY)
137
      {
138
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
139
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
140
      float axisZ = 0;
141
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
142
143
      if( axisL>0 )
144
        {
145
        axisX /= axisL;
146
        axisY /= axisL;
147
        axisZ /= axisL;
148
149
        float ratio = axisL;
150
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
151
152
        float cosA = (float)Math.cos(Math.PI*ratio);
153
        float sinA = (float)Math.sqrt(1-cosA*cosA);
154
155
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
156
        }
157
158
      return new Static4D(0f, 0f, 0f, 1f);
159
      }
160
161 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163
    private void setUpDragOrRotate(float x, float y)
164
      {
165 a6d3b158 Leszek Koltunski
      boolean rota = mRenderer.canRotate();
166
      boolean drag = mRenderer.canDrag();
167 12ad3fca Leszek Koltunski
168 a6d3b158 Leszek Koltunski
      if( !rota && drag )
169 12ad3fca Leszek Koltunski
        {
170 a6d3b158 Leszek Koltunski
        mDragging           = true;
171
        mBeginningRotation  = false;
172 12ad3fca Leszek Koltunski
        mContinuingRotation = false;
173
        }
174
      else
175
        {
176 a6d3b158 Leszek Koltunski
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
177
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
178
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
179
180
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
181
          {
182
          mDragging           = false;
183
          mBeginningRotation  = rota;
184
          mContinuingRotation = false;
185
          }
186
        else
187
          {
188
          mDragging           = drag;
189
          mBeginningRotation  = false;
190
          mContinuingRotation = false;
191
          }
192 12ad3fca Leszek Koltunski
        }
193
      }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
197
198
    private void computeCurrentAxis(Static3D axis)
199
      {
200
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
201
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
202
203
      mAxisX =result.get0();
204
      mAxisY =result.get1();
205
206
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
207
      mAxisX /= len;
208
      mAxisY /= len;
209
210 167ba2d6 Leszek Koltunski
      // android.util.Log.e("axis", "axis 2D: "+mAxisX+" , "+mAxisY);
211 12ad3fca Leszek Koltunski
      }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
    private float continueRotation(float dx, float dy)
216
      {
217
      float alpha = dx*mAxisX + dy*mAxisY;
218
      float x = dx - alpha*mAxisX;
219
      float y = dy - alpha*mAxisY;
220
221
      float len = (float)Math.sqrt(x*x + y*y);
222
223 167ba2d6 Leszek Koltunski
      // we have the length of 1D vector 'angle', now the direction:
224
      float tmp = mAxisY==0 ? -mAxisX*y : mAxisY*x;
225 12ad3fca Leszek Koltunski
226 e46e17fb Leszek Koltunski
      return (tmp>0 ? 1:-1)*len*mRotationFactor;
227 12ad3fca Leszek Koltunski
      }
228
229 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230 47ba5ddc Leszek Koltunski
// return quat1*quat2
231 0c52af30 Leszek Koltunski
232 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
233 45686da2 Leszek Koltunski
      {
234 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
235
      float qy = quat1.get1();
236
      float qz = quat1.get2();
237
      float qw = quat1.get3();
238 45686da2 Leszek Koltunski
239 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
240
      float ry = quat2.get1();
241
      float rz = quat2.get2();
242
      float rw = quat2.get3();
243 45686da2 Leszek Koltunski
244 47ba5ddc Leszek Koltunski
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
245
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
246
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
247
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
248 45686da2 Leszek Koltunski
249 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
250 45686da2 Leszek Koltunski
      }
251
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
254 45686da2 Leszek Koltunski
255 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
256 45686da2 Leszek Koltunski
      {
257 348dfe69 Leszek Koltunski
      float qx = quat.get0();
258
      float qy = quat.get1();
259
      float qz = quat.get2();
260
      float qw = quat.get3();
261 45686da2 Leszek Koltunski
262 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
263 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
264 0c52af30 Leszek Koltunski
265 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
266 0c52af30 Leszek Koltunski
      }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
270 0c52af30 Leszek Koltunski
271 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
272 0c52af30 Leszek Koltunski
      {
273 348dfe69 Leszek Koltunski
      float qx = quat.get0();
274
      float qy = quat.get1();
275
      float qz = quat.get2();
276
      float qw = quat.get3();
277 0c52af30 Leszek Koltunski
278 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
279 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
280 0c52af30 Leszek Koltunski
281 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
282 0c52af30 Leszek Koltunski
      }
283
284 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
// PUBLIC API
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
    public RubikSurfaceView(Context context, AttributeSet attrs)
289
      {
290
      super(context,attrs);
291
292
      if(!isInEditMode())
293
        {
294
        mRenderer = new RubikRenderer(this);
295
296
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
297
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
298
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
299
        setRenderer(mRenderer);
300
        }
301
      }
302
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305
    @Override
306
    public boolean onTouchEvent(MotionEvent event)
307
      {
308
      int action = event.getAction();
309 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
310
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
311 47ba5ddc Leszek Koltunski
312
      switch(action)
313
         {
314
         case MotionEvent.ACTION_DOWN: mX = x;
315
                                       mY = y;
316 bef47287 Leszek Koltunski
                                       setUpDragOrRotate(x,y);
317 47ba5ddc Leszek Koltunski
                                       break;
318 517f9fb9 Leszek Koltunski
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
319 47ba5ddc Leszek Koltunski
                                         {
320 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
321 47ba5ddc Leszek Koltunski
                                           {
322 12ad3fca Leszek Koltunski
                                           mStartRotX = x;
323
                                           mStartRotY = y;
324
325 86c73a69 Leszek Koltunski
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
326
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
327
328 e46e17fb Leszek Koltunski
                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
329 27a70eae Leszek Koltunski
                                           RubikObject object = mRenderer.getObject();
330 775e675d Leszek Koltunski
331 e46e17fb Leszek Koltunski
                                           int axis = (int)res.get0();
332
                                           float offset = res.get1();
333 12ad3fca Leszek Koltunski
                                           computeCurrentAxis( object.getRotationAxis()[axis] );
334 e46e17fb Leszek Koltunski
                                           mRotationFactor = object.returnRotationFactor(offset);
335 12ad3fca Leszek Koltunski
336 e46e17fb Leszek Koltunski
                                           object.beginNewRotation( axis, object.returnRowFromOffset(offset) );
337 775e675d Leszek Koltunski
338 0333d81e Leszek Koltunski
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
339
                                             {
340
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
341
                                             solving.startCounting( (RubikActivity)getContext() );
342
                                             }
343
344 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
345
                                           mContinuingRotation= true;
346
                                           }
347
                                         }
348
                                       else if( mContinuingRotation )
349
                                         {
350 12ad3fca Leszek Koltunski
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
351 27a70eae Leszek Koltunski
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
352 47ba5ddc Leszek Koltunski
                                         }
353 bef47287 Leszek Koltunski
                                       else if( mDragging )
354 517f9fb9 Leszek Koltunski
                                         {
355
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
356
                                         mRenderer.setQuatCurrentOnNextRender();
357
358
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
359
                                           {
360
                                           mX = x;
361
                                           mY = y;
362
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
363
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
364
                                           mRenderer.setQuatCurrentOnNextRender();
365
                                           mRenderer.setQuatAccumulatedOnNextRender();
366
                                           }
367
                                         }
368 8e2295ad Leszek Koltunski
                                       else if( mRenderer.canRotate() || mRenderer.canDrag() )
369 bef47287 Leszek Koltunski
                                         {
370
                                         setUpDragOrRotate(x,y);
371
                                         }
372 47ba5ddc Leszek Koltunski
                                       break;
373
         case MotionEvent.ACTION_UP  : if( mDragging )
374
                                         {
375
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
376
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
377
                                         mRenderer.setQuatCurrentOnNextRender();
378
                                         mRenderer.setQuatAccumulatedOnNextRender();
379
                                         }
380
381
                                       if( mContinuingRotation )
382
                                         {
383 beb325a0 Leszek Koltunski
                                         mRenderer.finishRotation();
384 47ba5ddc Leszek Koltunski
                                         }
385
                                       break;
386
         }
387
388
      return true;
389
      }
390 0c52af30 Leszek Koltunski
}