Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 39e74052

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

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

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

    
59
    private RubikRenderer mRenderer;
60
    private RubikObjectMovement mMovement;
61
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
62
    private float mX, mY;
63
    private int mScreenWidth, mScreenHeight, mScreenMin;
64

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    void setScreenSize(int width, int height)
73
      {
74
      mScreenWidth = width;
75
      mScreenHeight= height;
76

    
77
      mScreenMin = width<height ? width:height;
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    RubikRenderer getRenderer()
83
      {
84
      return mRenderer;
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    void setQuatAccumulated()
90
      {
91
      mQuatAccumulated.set(mTempAccumulated);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    void setQuatCurrent()
97
      {
98
      mQuatCurrent.set(mTempCurrent);
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
    Static4D getQuatAccumulated()
104
      {
105
      return mQuatAccumulated;
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
    Static4D getQuatCurrent()
111
      {
112
      return mQuatCurrent;
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
    void setMovement(RubikObjectMovement movement)
118
      {
119
      mMovement = movement;
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

    
131
      if( axisL>0 )
132
        {
133
        axisX /= axisL;
134
        axisY /= axisL;
135
        axisZ /= axisL;
136

    
137
        float ratio = axisL;
138
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
139

    
140
        float cosA = (float)Math.cos(Math.PI*ratio);
141
        float sinA = (float)Math.sqrt(1-cosA*cosA);
142

    
143
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
144
        }
145

    
146
      return new Static4D(0f, 0f, 0f, 1f);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// return quat1*quat2
151

    
152
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
153
      {
154
      float qx = quat1.get0();
155
      float qy = quat1.get1();
156
      float qz = quat1.get2();
157
      float qw = quat1.get3();
158

    
159
      float rx = quat2.get0();
160
      float ry = quat2.get1();
161
      float rz = quat2.get2();
162
      float rw = quat2.get3();
163

    
164
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
165
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
166
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
167
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
168

    
169
      return new Static4D(tx,ty,tz,tw);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
174

    
175
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
176
      {
177
      float qx = quat.get0();
178
      float qy = quat.get1();
179
      float qz = quat.get2();
180
      float qw = quat.get3();
181

    
182
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
183
      Static4D tmp = quatMultiply(quat,vector);
184

    
185
      return quatMultiply(tmp,quatInverted);
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
190

    
191
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
192
      {
193
      float qx = quat.get0();
194
      float qy = quat.get1();
195
      float qz = quat.get2();
196
      float qw = quat.get3();
197

    
198
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
199
      Static4D tmp = quatMultiply(quatInverted,vector);
200

    
201
      return quatMultiply(tmp,quat);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// PUBLIC API
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    public RubikSurfaceView(Context context, AttributeSet attrs)
209
      {
210
      super(context,attrs);
211

    
212
      if(!isInEditMode())
213
        {
214
        mRenderer = new RubikRenderer(this);
215

    
216
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
217
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
218
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
219
        setRenderer(mRenderer);
220
        }
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    private void setUpDragOrRotate(float x, float y)
226
      {
227
      Static4D touchPoint1 = new Static4D(x, y, 0, 0);
228
      Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
229
      Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
230

    
231
      if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
232
        {
233
        mDragging           = false;
234
        mBeginningRotation  = mRenderer.canRotate();
235
        mContinuingRotation = false;
236
        }
237
      else
238
        {
239
        mDragging           = mRenderer.canDrag();
240
        mBeginningRotation  = false;
241
        mContinuingRotation = false;
242
        }
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    @Override
248
    public boolean onTouchEvent(MotionEvent event)
249
      {
250
      int action = event.getAction();
251
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
252
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
253

    
254
      switch(action)
255
         {
256
         case MotionEvent.ACTION_DOWN: mX = x;
257
                                       mY = y;
258
                                       setUpDragOrRotate(x,y);
259
                                       break;
260
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
261
                                         {
262
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
263
                                           {
264
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
265
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
266

    
267
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
268
                                           RubikObject object = mRenderer.getObject();
269

    
270
                                           object.beginNewRotation( (int)rot.get0(), object.returnRowFromOffset(rot.get1()) );
271

    
272
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
273
                                             {
274
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
275
                                             solving.startCounting( (RubikActivity)getContext() );
276
                                             }
277

    
278
                                           mBeginningRotation = false;
279
                                           mContinuingRotation= true;
280
                                           }
281
                                         }
282
                                       else if( mContinuingRotation )
283
                                         {
284
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
285
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
286

    
287
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
288
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
289
                                         }
290
                                       else if( mDragging )
291
                                         {
292
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
293
                                         mRenderer.setQuatCurrentOnNextRender();
294

    
295
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
296
                                           {
297
                                           mX = x;
298
                                           mY = y;
299
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
300
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
301
                                           mRenderer.setQuatCurrentOnNextRender();
302
                                           mRenderer.setQuatAccumulatedOnNextRender();
303
                                           }
304
                                         }
305
                                       else if( mRenderer.canRotate() || mRenderer.canDrag() )
306
                                         {
307
                                         setUpDragOrRotate(x,y);
308
                                         }
309
                                       break;
310
         case MotionEvent.ACTION_UP  : if( mDragging )
311
                                         {
312
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
313
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
314
                                         mRenderer.setQuatCurrentOnNextRender();
315
                                         mRenderer.setQuatAccumulatedOnNextRender();
316
                                         }
317

    
318
                                       if( mContinuingRotation )
319
                                         {
320
                                         mRenderer.finishRotation();
321
                                         }
322
                                       break;
323
         }
324

    
325
      return true;
326
      }
327
}
328

    
(3-3/3)