Project

General

Profile

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

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

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/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
45
    private final static int ROTATION_SENSITIVITY =  12;
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
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
50

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

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
    void setMovement(RubikObjectMovement movement)
110
      {
111
      mMovement = movement;
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

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

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

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

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

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

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// return quat1*quat2
143

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

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

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

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

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

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

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

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

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

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

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

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// PUBLIC API
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

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

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

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

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

    
251
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
252
                                           {
253
                                           mX = x;
254
                                           mY = y;
255
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
256
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
257
                                           mRenderer.setQuatCurrentOnNextRender();
258
                                           mRenderer.setQuatAccumulatedOnNextRender();
259
                                           }
260
                                         }
261
                                       if( mBeginningRotation )
262
                                         {
263
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
264
                                           {
265
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
266
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
267

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

    
271
                                           object.beginNewRotation( (int)rot.get0(), (int)(object.getSize()*rot.get1()) );
272

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

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

    
288
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
289
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
290
                                         }
291
                                       break;
292
         case MotionEvent.ACTION_UP  : if( mDragging )
293
                                         {
294
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
295
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
296
                                         mRenderer.setQuatCurrentOnNextRender();
297
                                         mRenderer.setQuatAccumulatedOnNextRender();
298
                                         }
299

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

    
307
      return true;
308
      }
309
}
310

    
(3-3/3)