Project

General

Profile

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

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

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 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
31 27a70eae Leszek Koltunski
import org.distorted.object.RubikObject;
32
import org.distorted.object.RubikObjectMovement;
33 0333d81e Leszek Koltunski
import org.distorted.uistate.RubikState;
34
import org.distorted.uistate.RubikStateSolving;
35 0c52af30 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
39 0c52af30 Leszek Koltunski
{
40 775e675d Leszek Koltunski
    // 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 14a4712f Leszek Koltunski
    // 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 eac805bd Leszek Koltunski
    // 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 84ddf691 Leszek Koltunski
    // 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 86c73a69 Leszek Koltunski
59 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
60 27a70eae Leszek Koltunski
    private RubikObjectMovement mMovement;
61 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
62 c896c0b3 Leszek Koltunski
    private float mX, mY;
63 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
64 0c52af30 Leszek Koltunski
65 45686da2 Leszek Koltunski
    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 ee5c2ae1 Leszek Koltunski
70 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
    void setMovement(RubikObjectMovement movement)
118
      {
119
      mMovement = movement;
120
      }
121
122 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150 47ba5ddc Leszek Koltunski
// return quat1*quat2
151 0c52af30 Leszek Koltunski
152 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
153 45686da2 Leszek Koltunski
      {
154 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
155
      float qy = quat1.get1();
156
      float qz = quat1.get2();
157
      float qw = quat1.get3();
158 45686da2 Leszek Koltunski
159 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
160
      float ry = quat2.get1();
161
      float rz = quat2.get2();
162
      float rw = quat2.get3();
163 45686da2 Leszek Koltunski
164 47ba5ddc Leszek Koltunski
      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 45686da2 Leszek Koltunski
169 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
170 45686da2 Leszek Koltunski
      }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
174 45686da2 Leszek Koltunski
175 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
176 45686da2 Leszek Koltunski
      {
177 348dfe69 Leszek Koltunski
      float qx = quat.get0();
178
      float qy = quat.get1();
179
      float qz = quat.get2();
180
      float qw = quat.get3();
181 45686da2 Leszek Koltunski
182 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
183 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
184 0c52af30 Leszek Koltunski
185 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
186 0c52af30 Leszek Koltunski
      }
187
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
190 0c52af30 Leszek Koltunski
191 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
192 0c52af30 Leszek Koltunski
      {
193 348dfe69 Leszek Koltunski
      float qx = quat.get0();
194
      float qy = quat.get1();
195
      float qz = quat.get2();
196
      float qw = quat.get3();
197 0c52af30 Leszek Koltunski
198 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
199 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
200 0c52af30 Leszek Koltunski
201 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
202 0c52af30 Leszek Koltunski
      }
203
204 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 bef47287 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    @Override
248
    public boolean onTouchEvent(MotionEvent event)
249
      {
250
      int action = event.getAction();
251 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
252
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
253 47ba5ddc Leszek Koltunski
254
      switch(action)
255
         {
256
         case MotionEvent.ACTION_DOWN: mX = x;
257
                                       mY = y;
258 bef47287 Leszek Koltunski
                                       setUpDragOrRotate(x,y);
259 47ba5ddc Leszek Koltunski
                                       break;
260 517f9fb9 Leszek Koltunski
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
261 47ba5ddc Leszek Koltunski
                                         {
262 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
263 47ba5ddc Leszek Koltunski
                                           {
264 86c73a69 Leszek Koltunski
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
265
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
266
267 efef689c Leszek Koltunski
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
268 27a70eae Leszek Koltunski
                                           RubikObject object = mRenderer.getObject();
269 775e675d Leszek Koltunski
270 39e74052 Leszek Koltunski
                                           object.beginNewRotation( (int)rot.get0(), object.returnRowFromOffset(rot.get1()) );
271 775e675d Leszek Koltunski
272 0333d81e Leszek Koltunski
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
273
                                             {
274
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
275
                                             solving.startCounting( (RubikActivity)getContext() );
276
                                             }
277
278 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
279
                                           mContinuingRotation= true;
280
                                           }
281
                                         }
282
                                       else if( mContinuingRotation )
283
                                         {
284 86c73a69 Leszek Koltunski
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
285
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
286
287
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
288 27a70eae Leszek Koltunski
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
289 47ba5ddc Leszek Koltunski
                                         }
290 bef47287 Leszek Koltunski
                                       else if( mDragging )
291 517f9fb9 Leszek Koltunski
                                         {
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 8e2295ad Leszek Koltunski
                                       else if( mRenderer.canRotate() || mRenderer.canDrag() )
306 bef47287 Leszek Koltunski
                                         {
307
                                         setUpDragOrRotate(x,y);
308
                                         }
309 47ba5ddc Leszek Koltunski
                                       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 beb325a0 Leszek Koltunski
                                         mRenderer.finishRotation();
321 47ba5ddc Leszek Koltunski
                                         }
322
                                       break;
323
         }
324
325
      return true;
326
      }
327 0c52af30 Leszek Koltunski
}