Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 211b48f2

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

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class RubikSurfaceView extends GLSurfaceView
37
{
38
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
39
    // given face by SWIPING_SENSITIVITY/2 degrees.
40
    private final static int SWIPING_SENSITIVITY  = 240;
41
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
42
    private final static int ROTATION_SENSITIVITY =  12;
43
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
44
    private final static int DIRECTION_SENSITIVITY=  12;
45

    
46
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
47

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

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
66
      mScreenMin = width<height ? width:height;
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    RubikRenderer getRenderer()
72
      {
73
      return mRenderer;
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    void setQuatAccumulated()
79
      {
80
      mQuatAccumulated.set(mTempAccumulated);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
    void setQuatCurrent()
86
      {
87
      mQuatCurrent.set(mTempCurrent);
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
    Static4D getQuatAccumulated()
93
      {
94
      return mQuatAccumulated;
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
    Static4D getQuatCurrent()
100
      {
101
      return mQuatCurrent;
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

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

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

    
125
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
126
        }
127

    
128
      return new Static4D(0f, 0f, 0f, 1f);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// return quat1*quat2
133

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

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

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

    
151
      return new Static4D(tx,ty,tz,tw);
152
      }
153

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

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

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

    
167
      return quatMultiply(tmp,quatInverted);
168
      }
169

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

    
173
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
174
      {
175
      float qx = quat.get0();
176
      float qy = quat.get1();
177
      float qz = quat.get2();
178
      float qw = quat.get3();
179

    
180
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
181
      Static4D tmp = quatMultiply(quatInverted,vector);
182

    
183
      return quatMultiply(tmp,quat);
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// PUBLIC API
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    public RubikSurfaceView(Context context, AttributeSet attrs)
191
      {
192
      super(context,attrs);
193

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

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

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

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

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

    
259
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
260
                                           RubikCube cube = mRenderer.getCube();
261

    
262
                                           cube.addNewRotation( (int)rot.get0(), (int)(cube.getSize()*rot.get1()) );
263

    
264
                                           mBeginningRotation = false;
265
                                           mContinuingRotation= true;
266
                                           }
267
                                         }
268
                                       else if( mContinuingRotation )
269
                                         {
270
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
271
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
272

    
273
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
274
                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
275
                                         }
276
                                       break;
277
         case MotionEvent.ACTION_UP  : if( mDragging )
278
                                         {
279
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
280
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
281
                                         mRenderer.setQuatCurrentOnNextRender();
282
                                         mRenderer.setQuatAccumulatedOnNextRender();
283
                                         }
284

    
285
                                       if( mContinuingRotation )
286
                                         {
287
                                         mRenderer.finishRotation();
288
                                         }
289
                                       break;
290
         }
291

    
292
      return true;
293
      }
294
}
295

    
(5-5/5)