Project

General

Profile

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

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

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/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 RubikObjectMovement 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
    void setMovement(RubikObjectMovement movement)
109
      {
110
      mMovement = movement;
111
      }
112

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

    
115
    private Static4D quatFromDrag(float dragX, float dragY)
116
      {
117
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
118
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
119
      float axisZ = 0;
120
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
121

    
122
      if( axisL>0 )
123
        {
124
        axisX /= axisL;
125
        axisY /= axisL;
126
        axisZ /= axisL;
127

    
128
        float ratio = axisL;
129
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
130

    
131
        float cosA = (float)Math.cos(Math.PI*ratio);
132
        float sinA = (float)Math.sqrt(1-cosA*cosA);
133

    
134
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
135
        }
136

    
137
      return new Static4D(0f, 0f, 0f, 1f);
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// return quat1*quat2
142

    
143
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
144
      {
145
      float qx = quat1.get0();
146
      float qy = quat1.get1();
147
      float qz = quat1.get2();
148
      float qw = quat1.get3();
149

    
150
      float rx = quat2.get0();
151
      float ry = quat2.get1();
152
      float rz = quat2.get2();
153
      float rw = quat2.get3();
154

    
155
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
156
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
157
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
158
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
159

    
160
      return new Static4D(tx,ty,tz,tw);
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
165

    
166
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
167
      {
168
      float qx = quat.get0();
169
      float qy = quat.get1();
170
      float qz = quat.get2();
171
      float qw = quat.get3();
172

    
173
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
174
      Static4D tmp = quatMultiply(quat,vector);
175

    
176
      return quatMultiply(tmp,quatInverted);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
181

    
182
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
183
      {
184
      float qx = quat.get0();
185
      float qy = quat.get1();
186
      float qz = quat.get2();
187
      float qw = quat.get3();
188

    
189
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
190
      Static4D tmp = quatMultiply(quatInverted,vector);
191

    
192
      return quatMultiply(tmp,quat);
193
      }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
// PUBLIC API
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    public RubikSurfaceView(Context context, AttributeSet attrs)
200
      {
201
      super(context,attrs);
202

    
203
      if(!isInEditMode())
204
        {
205
        mRenderer = new RubikRenderer(this);
206

    
207
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
208
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
209
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
210
        setRenderer(mRenderer);
211
        }
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    @Override
217
    public boolean onTouchEvent(MotionEvent event)
218
      {
219
      int action = event.getAction();
220
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
221
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
222

    
223
      switch(action)
224
         {
225
         case MotionEvent.ACTION_DOWN: mX = x;
226
                                       mY = y;
227

    
228
                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
229
                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
230
                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
231

    
232
                                       if( mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
233
                                         {
234
                                         mDragging           = false;
235
                                         mBeginningRotation  = mRenderer.canRotate();
236
                                         mContinuingRotation = false;
237
                                         }
238
                                       else
239
                                         {
240
                                         mDragging           = mRenderer.canDrag();
241
                                         mBeginningRotation  = false;
242
                                         mContinuingRotation = false;
243
                                         }
244
                                       break;
245
         case MotionEvent.ACTION_MOVE: if( mDragging )
246
                                         {
247
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
248
                                         mRenderer.setQuatCurrentOnNextRender();
249

    
250
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
251
                                           {
252
                                           mX = x;
253
                                           mY = y;
254
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
255
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
256
                                           mRenderer.setQuatCurrentOnNextRender();
257
                                           mRenderer.setQuatAccumulatedOnNextRender();
258
                                           }
259
                                         }
260
                                       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(), (int)(object.getSize()*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
                                       break;
291
         case MotionEvent.ACTION_UP  : if( mDragging )
292
                                         {
293
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
294
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
295
                                         mRenderer.setQuatCurrentOnNextRender();
296
                                         mRenderer.setQuatAccumulatedOnNextRender();
297
                                         }
298

    
299
                                       if( mContinuingRotation )
300
                                         {
301
                                         mRenderer.finishRotation();
302
                                         }
303
                                       break;
304
         }
305

    
306
      return true;
307
      }
308
}
309

    
(3-3/3)