Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 12ad3fca

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