Project

General

Profile

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

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

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 34747dd1 Leszek Koltunski
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
44 47ba5ddc Leszek Koltunski
    private final static int ROTATION_SENSITIVITY =  12;
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 86c73a69 Leszek Koltunski
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
49
50 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
51 27a70eae Leszek Koltunski
    private RubikObjectMovement mMovement;
52 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
53 c896c0b3 Leszek Koltunski
    private float mX, mY;
54 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
55 0c52af30 Leszek Koltunski
56 45686da2 Leszek Koltunski
    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 ee5c2ae1 Leszek Koltunski
61 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
    void setMovement(RubikObjectMovement movement)
109
      {
110
      mMovement = movement;
111
      }
112
113 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141 47ba5ddc Leszek Koltunski
// return quat1*quat2
142 0c52af30 Leszek Koltunski
143 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
144 45686da2 Leszek Koltunski
      {
145 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
146
      float qy = quat1.get1();
147
      float qz = quat1.get2();
148
      float qw = quat1.get3();
149 45686da2 Leszek Koltunski
150 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
151
      float ry = quat2.get1();
152
      float rz = quat2.get2();
153
      float rw = quat2.get3();
154 45686da2 Leszek Koltunski
155 47ba5ddc Leszek Koltunski
      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 45686da2 Leszek Koltunski
160 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
161 45686da2 Leszek Koltunski
      }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
165 45686da2 Leszek Koltunski
166 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
167 45686da2 Leszek Koltunski
      {
168 348dfe69 Leszek Koltunski
      float qx = quat.get0();
169
      float qy = quat.get1();
170
      float qz = quat.get2();
171
      float qw = quat.get3();
172 45686da2 Leszek Koltunski
173 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
174 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
175 0c52af30 Leszek Koltunski
176 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
177 0c52af30 Leszek Koltunski
      }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
181 0c52af30 Leszek Koltunski
182 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
183 0c52af30 Leszek Koltunski
      {
184 348dfe69 Leszek Koltunski
      float qx = quat.get0();
185
      float qy = quat.get1();
186
      float qz = quat.get2();
187
      float qw = quat.get3();
188 0c52af30 Leszek Koltunski
189 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
190 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
191 0c52af30 Leszek Koltunski
192 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
193 0c52af30 Leszek Koltunski
      }
194
195 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
221
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
222 47ba5ddc Leszek Koltunski
223
      switch(action)
224
         {
225
         case MotionEvent.ACTION_DOWN: mX = x;
226
                                       mY = y;
227
228 86c73a69 Leszek Koltunski
                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
229
                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
230
                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
231
232 0333d81e Leszek Koltunski
                                       if( mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
233 47ba5ddc Leszek Koltunski
                                         {
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 c896c0b3 Leszek Koltunski
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
248 47ba5ddc Leszek Koltunski
                                         mRenderer.setQuatCurrentOnNextRender();
249
250 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
251 47ba5ddc Leszek Koltunski
                                           {
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 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 efef689c Leszek Koltunski
                                           object.beginNewRotation( (int)rot.get0(), (int)(object.getSize()*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
                                       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 beb325a0 Leszek Koltunski
                                         mRenderer.finishRotation();
302 47ba5ddc Leszek Koltunski
                                         }
303
                                       break;
304
         }
305
306
      return true;
307
      }
308 0c52af30 Leszek Koltunski
}