Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 0333d81e

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.RubikCube;
32
import org.distorted.object.RubikCubeMovement;
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/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
44
    private final static int ROTATION_SENSITIVITY =  12;
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
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
49

    
50
    private RubikRenderer mRenderer;
51
    private RubikCubeMovement mMovement;
52
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
53
    private float mX, mY;
54
    private int mScreenWidth, mScreenHeight, mScreenMin;
55

    
56
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
57
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
58
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
59
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    void setScreenSize(int width, int height)
64
      {
65
      mScreenWidth = width;
66
      mScreenHeight= height;
67

    
68
      mScreenMin = width<height ? width:height;
69
      }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
    RubikRenderer getRenderer()
74
      {
75
      return mRenderer;
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
    void setQuatAccumulated()
81
      {
82
      mQuatAccumulated.set(mTempAccumulated);
83
      }
84

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

    
87
    void setQuatCurrent()
88
      {
89
      mQuatCurrent.set(mTempCurrent);
90
      }
91

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

    
94
    Static4D getQuatAccumulated()
95
      {
96
      return mQuatAccumulated;
97
      }
98

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

    
101
    Static4D getQuatCurrent()
102
      {
103
      return mQuatCurrent;
104
      }
105

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

    
108
    private Static4D quatFromDrag(float dragX, float dragY)
109
      {
110
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
111
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
112
      float axisZ = 0;
113
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
114

    
115
      if( axisL>0 )
116
        {
117
        axisX /= axisL;
118
        axisY /= axisL;
119
        axisZ /= axisL;
120

    
121
        float ratio = axisL;
122
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
123

    
124
        float cosA = (float)Math.cos(Math.PI*ratio);
125
        float sinA = (float)Math.sqrt(1-cosA*cosA);
126

    
127
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
128
        }
129

    
130
      return new Static4D(0f, 0f, 0f, 1f);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// return quat1*quat2
135

    
136
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
137
      {
138
      float qx = quat1.get0();
139
      float qy = quat1.get1();
140
      float qz = quat1.get2();
141
      float qw = quat1.get3();
142

    
143
      float rx = quat2.get0();
144
      float ry = quat2.get1();
145
      float rz = quat2.get2();
146
      float rw = quat2.get3();
147

    
148
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
149
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
150
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
151
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
152

    
153
      return new Static4D(tx,ty,tz,tw);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
158

    
159
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
160
      {
161
      float qx = quat.get0();
162
      float qy = quat.get1();
163
      float qz = quat.get2();
164
      float qw = quat.get3();
165

    
166
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
167
      Static4D tmp = quatMultiply(quat,vector);
168

    
169
      return quatMultiply(tmp,quatInverted);
170
      }
171

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

    
175
    public static Static4D rotateVectorByInvertedQuat(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(quatInverted,vector);
184

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// PUBLIC API
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    public RubikSurfaceView(Context context, AttributeSet attrs)
193
      {
194
      super(context,attrs);
195

    
196
      if(!isInEditMode())
197
        {
198
        mRenderer = new RubikRenderer(this);
199
        mMovement = new RubikCubeMovement();
200

    
201
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
202
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
203
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
204
        setRenderer(mRenderer);
205
        }
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    @Override
211
    public boolean onTouchEvent(MotionEvent event)
212
      {
213
      int action = event.getAction();
214
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
215
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
216

    
217
      switch(action)
218
         {
219
         case MotionEvent.ACTION_DOWN: mX = x;
220
                                       mY = y;
221

    
222
                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
223
                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
224
                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
225

    
226
                                       if( mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
227
                                         {
228
                                         mDragging           = false;
229
                                         mBeginningRotation  = mRenderer.canRotate();
230
                                         mContinuingRotation = false;
231
                                         }
232
                                       else
233
                                         {
234
                                         mDragging           = mRenderer.canDrag();
235
                                         mBeginningRotation  = false;
236
                                         mContinuingRotation = false;
237
                                         }
238
                                       break;
239
         case MotionEvent.ACTION_MOVE: if( mDragging )
240
                                         {
241
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
242
                                         mRenderer.setQuatCurrentOnNextRender();
243

    
244
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
245
                                           {
246
                                           mX = x;
247
                                           mY = y;
248
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
249
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
250
                                           mRenderer.setQuatCurrentOnNextRender();
251
                                           mRenderer.setQuatAccumulatedOnNextRender();
252
                                           }
253
                                         }
254
                                       if( mBeginningRotation )
255
                                         {
256
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
257
                                           {
258
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
259
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
260

    
261
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
262
                                           RubikCube cube = mRenderer.getCube();
263

    
264
                                           cube.addNewRotation( (int)rot.get0(), (int)(cube.getSize()*rot.get1()) );
265

    
266
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
267
                                             {
268
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
269
                                             solving.startCounting( (RubikActivity)getContext() );
270
                                             }
271

    
272
                                           mBeginningRotation = false;
273
                                           mContinuingRotation= true;
274
                                           }
275
                                         }
276
                                       else if( mContinuingRotation )
277
                                         {
278
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
279
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
280

    
281
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
282
                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
283
                                         }
284
                                       break;
285
         case MotionEvent.ACTION_UP  : if( mDragging )
286
                                         {
287
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
288
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
289
                                         mRenderer.setQuatCurrentOnNextRender();
290
                                         mRenderer.setQuatAccumulatedOnNextRender();
291
                                         }
292

    
293
                                       if( mContinuingRotation )
294
                                         {
295
                                         mRenderer.finishRotation();
296
                                         }
297
                                       break;
298
         }
299

    
300
      return true;
301
      }
302
}
303

    
(3-3/3)