Project

General

Profile

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

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

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 RubikObjectMovement mMovement;
62
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
63
    private int mScreenWidth, mScreenHeight, mScreenMin;
64

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

    
70
    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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
    RubikRenderer getRenderer()
88
      {
89
      return mRenderer;
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
    void setQuatAccumulated()
95
      {
96
      mQuatAccumulated.set(mTempAccumulated);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    void setQuatCurrent()
102
      {
103
      mQuatCurrent.set(mTempCurrent);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    Static4D getQuatAccumulated()
109
      {
110
      return mQuatAccumulated;
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    Static4D getQuatCurrent()
116
      {
117
      return mQuatCurrent;
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    void setMovement(RubikObjectMovement movement)
123
      {
124
      mMovement = movement;
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    private Static4D quatFromDrag(float dragX, float dragY)
130
      {
131
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
132
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
133
      float axisZ = 0;
134
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
135

    
136
      if( axisL>0 )
137
        {
138
        axisX /= axisL;
139
        axisY /= axisL;
140
        axisZ /= axisL;
141

    
142
        float ratio = axisL;
143
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
144

    
145
        float cosA = (float)Math.cos(Math.PI*ratio);
146
        float sinA = (float)Math.sqrt(1-cosA*cosA);
147

    
148
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
149
        }
150

    
151
      return new Static4D(0f, 0f, 0f, 1f);
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    private void setUpDragOrRotate(float x, float y)
157
      {
158
      Static4D touchPoint1 = new Static4D(x, y, 0, 0);
159
      Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
160
      Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
161

    
162
      if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
163
        {
164
        mDragging           = false;
165
        mBeginningRotation  = mRenderer.canRotate();
166
        mContinuingRotation = false;
167
        }
168
      else
169
        {
170
        mDragging           = mRenderer.canDrag();
171
        mBeginningRotation  = false;
172
        mContinuingRotation = false;
173
        }
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
178

    
179
    private void computeCurrentAxis(Static3D axis)
180
      {
181
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
182
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
183

    
184
      mAxisX =result.get0();
185
      mAxisY =result.get1();
186

    
187
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
188
      mAxisX /= len;
189
      mAxisY /= len;
190

    
191
      // android.util.Log.e("axis", "axis 2D: "+mAxisX+" , "+mAxisY);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    private float continueRotation(float dx, float dy)
197
      {
198
      float alpha = dx*mAxisX + dy*mAxisY;
199
      float x = dx - alpha*mAxisX;
200
      float y = dy - alpha*mAxisY;
201

    
202
      float len = (float)Math.sqrt(x*x + y*y);
203

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

    
207
      return (tmp>0 ? 1:-1)*len*mRotationFactor;
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
// return quat1*quat2
212

    
213
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
214
      {
215
      float qx = quat1.get0();
216
      float qy = quat1.get1();
217
      float qz = quat1.get2();
218
      float qw = quat1.get3();
219

    
220
      float rx = quat2.get0();
221
      float ry = quat2.get1();
222
      float rz = quat2.get2();
223
      float rw = quat2.get3();
224

    
225
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
226
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
227
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
228
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
229

    
230
      return new Static4D(tx,ty,tz,tw);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
235

    
236
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
237
      {
238
      float qx = quat.get0();
239
      float qy = quat.get1();
240
      float qz = quat.get2();
241
      float qw = quat.get3();
242

    
243
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
244
      Static4D tmp = quatMultiply(quat,vector);
245

    
246
      return quatMultiply(tmp,quatInverted);
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
251

    
252
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
253
      {
254
      float qx = quat.get0();
255
      float qy = quat.get1();
256
      float qz = quat.get2();
257
      float qw = quat.get3();
258

    
259
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
260
      Static4D tmp = quatMultiply(quatInverted,vector);
261

    
262
      return quatMultiply(tmp,quat);
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// PUBLIC API
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public RubikSurfaceView(Context context, AttributeSet attrs)
270
      {
271
      super(context,attrs);
272

    
273
      if(!isInEditMode())
274
        {
275
        mRenderer = new RubikRenderer(this);
276

    
277
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
278
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
279
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
280
        setRenderer(mRenderer);
281
        }
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    @Override
287
    public boolean onTouchEvent(MotionEvent event)
288
      {
289
      int action = event.getAction();
290
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
291
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
292

    
293
      switch(action)
294
         {
295
         case MotionEvent.ACTION_DOWN: mX = x;
296
                                       mY = y;
297
                                       setUpDragOrRotate(x,y);
298
                                       break;
299
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
300
                                         {
301
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
302
                                           {
303
                                           mStartRotX = x;
304
                                           mStartRotY = y;
305

    
306
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
307
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
308

    
309
                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
310
                                           RubikObject object = mRenderer.getObject();
311

    
312
                                           int axis = (int)res.get0();
313
                                           float offset = res.get1();
314
                                           computeCurrentAxis( object.getRotationAxis()[axis] );
315
                                           mRotationFactor = object.returnRotationFactor(offset);
316

    
317
                                           object.beginNewRotation( axis, object.returnRowFromOffset(offset) );
318

    
319
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
320
                                             {
321
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
322
                                             solving.startCounting( (RubikActivity)getContext() );
323
                                             }
324

    
325
                                           mBeginningRotation = false;
326
                                           mContinuingRotation= true;
327
                                           }
328
                                         }
329
                                       else if( mContinuingRotation )
330
                                         {
331
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
332
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
333
                                         }
334
                                       else if( mDragging )
335
                                         {
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
                                       else if( mRenderer.canRotate() || mRenderer.canDrag() )
350
                                         {
351
                                         setUpDragOrRotate(x,y);
352
                                         }
353
                                       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
                                         mRenderer.finishRotation();
365
                                         }
366
                                       break;
367
         }
368

    
369
      return true;
370
      }
371
}
372

    
(3-3/3)