Project

General

Profile

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

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

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 3c4a326c Leszek Koltunski
import org.distorted.library.type.Static3D;
31 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
32 27a70eae Leszek Koltunski
import org.distorted.object.RubikObject;
33
import org.distorted.object.RubikObjectMovement;
34 0333d81e Leszek Koltunski
import org.distorted.uistate.RubikState;
35
import org.distorted.uistate.RubikStateSolving;
36 0c52af30 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
40 0c52af30 Leszek Koltunski
{
41 775e675d Leszek Koltunski
    // 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 34747dd1 Leszek Koltunski
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
45 47ba5ddc Leszek Koltunski
    private final static int ROTATION_SENSITIVITY =  12;
46 eac805bd Leszek Koltunski
    // 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 86c73a69 Leszek Koltunski
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
50
51 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
52 27a70eae Leszek Koltunski
    private RubikObjectMovement mMovement;
53 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
54 c896c0b3 Leszek Koltunski
    private float mX, mY;
55 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
56 0c52af30 Leszek Koltunski
57 45686da2 Leszek Koltunski
    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 ee5c2ae1 Leszek Koltunski
62 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
    void setMovement(RubikObjectMovement movement)
110
      {
111
      mMovement = movement;
112
      }
113
114 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142 47ba5ddc Leszek Koltunski
// return quat1*quat2
143 0c52af30 Leszek Koltunski
144 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
145 45686da2 Leszek Koltunski
      {
146 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
147
      float qy = quat1.get1();
148
      float qz = quat1.get2();
149
      float qw = quat1.get3();
150 45686da2 Leszek Koltunski
151 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
152
      float ry = quat2.get1();
153
      float rz = quat2.get2();
154
      float rw = quat2.get3();
155 45686da2 Leszek Koltunski
156 47ba5ddc Leszek Koltunski
      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 45686da2 Leszek Koltunski
161 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
162 45686da2 Leszek Koltunski
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
166 45686da2 Leszek Koltunski
167 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
168 45686da2 Leszek Koltunski
      {
169 348dfe69 Leszek Koltunski
      float qx = quat.get0();
170
      float qy = quat.get1();
171
      float qz = quat.get2();
172
      float qw = quat.get3();
173 45686da2 Leszek Koltunski
174 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
175 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
176 0c52af30 Leszek Koltunski
177 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
178 0c52af30 Leszek Koltunski
      }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
182 0c52af30 Leszek Koltunski
183 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
184 0c52af30 Leszek Koltunski
      {
185 348dfe69 Leszek Koltunski
      float qx = quat.get0();
186
      float qy = quat.get1();
187
      float qz = quat.get2();
188
      float qw = quat.get3();
189 0c52af30 Leszek Koltunski
190 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
191 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
192 0c52af30 Leszek Koltunski
193 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
194 0c52af30 Leszek Koltunski
      }
195
196 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
222
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
223 47ba5ddc Leszek Koltunski
224
      switch(action)
225
         {
226
         case MotionEvent.ACTION_DOWN: mX = x;
227
                                       mY = y;
228
229 86c73a69 Leszek Koltunski
                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
230
                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
231
                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
232
233 0333d81e Leszek Koltunski
                                       if( mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
234 47ba5ddc Leszek Koltunski
                                         {
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 c896c0b3 Leszek Koltunski
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
249 47ba5ddc Leszek Koltunski
                                         mRenderer.setQuatCurrentOnNextRender();
250
251 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
252 47ba5ddc Leszek Koltunski
                                           {
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 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
264 47ba5ddc Leszek Koltunski
                                           {
265 86c73a69 Leszek Koltunski
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
266
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
267
268 efef689c Leszek Koltunski
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
269 27a70eae Leszek Koltunski
                                           RubikObject object = mRenderer.getObject();
270 775e675d Leszek Koltunski
271 efef689c Leszek Koltunski
                                           object.beginNewRotation( (int)rot.get0(), (int)(object.getSize()*rot.get1()) );
272 775e675d Leszek Koltunski
273 0333d81e Leszek Koltunski
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
274
                                             {
275
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
276
                                             solving.startCounting( (RubikActivity)getContext() );
277
                                             }
278
279 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
280
                                           mContinuingRotation= true;
281
                                           }
282
                                         }
283
                                       else if( mContinuingRotation )
284
                                         {
285 86c73a69 Leszek Koltunski
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
286
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
287
288
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
289 27a70eae Leszek Koltunski
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
290 47ba5ddc Leszek Koltunski
                                         }
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 beb325a0 Leszek Koltunski
                                         mRenderer.finishRotation();
303 47ba5ddc Leszek Koltunski
                                         }
304
                                       break;
305
         }
306
307
      return true;
308
      }
309 0c52af30 Leszek Koltunski
}