Project

General

Profile

« Previous | Next » 

Revision 86c73a69

Added by Leszek Koltunski almost 5 years ago

Movement.

View differences:

src/main/java/org/distorted/magic/RubikSurfaceView.java
46 46
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
47 47
    // given face by SWIPING_SENSITIVITY/2 degrees.
48 48
    private final static int SWIPING_SENSITIVITY  = 240;
49

  
50 49
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
51 50
    private final static int ROTATION_SENSITIVITY =  12;
52

  
53 51
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
54 52
    private final static int DIRECTION_SENSITIVITY=  12;
55 53

  
54
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
55

  
56 56
    private RubikRenderer mRenderer;
57 57
    private RubikCubeMovement mMovement;
58 58

  
......
351 351
         case MotionEvent.ACTION_DOWN: mX = x;
352 352
                                       mY = y;
353 353

  
354
                                       if( mMovement.faceTouched(mQuatAccumulated, x, y) )
354
                                       Static4D touchPoint1 = new Static4D(x, y, 0, 0);
355
                                       Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
356
                                       Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
357

  
358
                                       if( mMovement.faceTouched(rotatedTouchPoint1, rotatedCamera) )
355 359
                                         {
356 360
                                         mDragging           = false;
357 361
                                         mBeginningRotation  = mRenderer.canRotate();
......
383 387
                                         {
384 388
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
385 389
                                           {
386
                                           Static2D rot = mMovement.newRotation(mQuatAccumulated, x, y);
390
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
391
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
392

  
393
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
387 394
                                           RubikCube cube = mRenderer.getCube();
388 395

  
389 396
                                           cube.addNewRotation( (int)rot.get1(), (int)(cube.getSize()*rot.get2()) );
......
394 401
                                         }
395 402
                                       else if( mContinuingRotation )
396 403
                                         {
397
                                         float angle = mMovement.continueRotation(mQuatAccumulated, x, y);
404
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
405
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
406

  
407
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
398 408
                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
399 409
                                         }
400 410
                                       break;
src/main/java/org/distorted/object/RubikCubeMovement.java
21 21

  
22 22
import org.distorted.library.type.Static2D;
23 23
import org.distorted.library.type.Static4D;
24
import org.distorted.magic.RubikRenderer;
25
import org.distorted.magic.RubikSurfaceView;
26 24

  
27 25
///////////////////////////////////////////////////////////////////////////////////////////////////
28 26

  
......
38 36

  
39 37
    private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
40 38

  
41
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint;
39
    private float[] mPoint, mCamera, mDiff, mTouch;
42 40
    private int mRotationVect, mLastTouchedFace;
43 41

  
44 42
///////////////////////////////////////////////////////////////////////////////////////////////////
......
126 124
      return sign*mCamera[zAxis] > cubeHalfSize;
127 125
      }
128 126

  
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

  
131
    private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
132
      {
133
      Static4D touchPoint = new Static4D(x, y, 0, 0);
134
      Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated);
135

  
136
      mPoint[0] = rotatedTouchPoint.get1();
137
      mPoint[1] = rotatedTouchPoint.get2();
138
      mPoint[2] = rotatedTouchPoint.get3();
139
      }
140

  
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

  
143
    private void convertCameraPointToScreenSpace(Static4D accumulated)
144
      {
145
      Static4D cameraPoint = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
146
      Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated);
147

  
148
      mCamera[0] = rotatedCamera.get1();
149
      mCamera[1] = rotatedCamera.get2();
150
      mCamera[2] = rotatedCamera.get3();
151
      }
152

  
153 127
///////////////////////////////////////////////////////////////////////////////////////////////////
154 128
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
155 129
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
......
174 148

  
175 149
    public RubikCubeMovement()
176 150
      {
177
      mRotationVect = VECT[0];
178

  
179 151
      mPoint = new float[3];
180 152
      mCamera= new float[3];
181 153
      mDiff  = new float[3];
182
      mTouchPoint = new float[3];
183
      mTouchPointCastOntoFace = new float[3];
154
      mTouch = new float[3];
184 155
      }
185 156

  
186 157
///////////////////////////////////////////////////////////////////////////////////////////////////
187 158

  
188
    public boolean faceTouched(Static4D rotQuaternion, float x, float y)
159
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
189 160
      {
190 161
      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
191 162

  
192
      convertTouchPointToScreenSpace(rotQuaternion,x,y);
193
      convertCameraPointToScreenSpace(rotQuaternion);
163
      mPoint[0]  = rotatedTouchPoint.get1();
164
      mPoint[1]  = rotatedTouchPoint.get2();
165
      mPoint[2]  = rotatedTouchPoint.get3();
166

  
167
      mCamera[0] = rotatedCamera.get1();
168
      mCamera[1] = rotatedCamera.get2();
169
      mCamera[2] = rotatedCamera.get3();
194 170

  
195 171
      for( mLastTouchedFace=FRONT; mLastTouchedFace<=BOTTOM; mLastTouchedFace++)
196 172
        {
197 173
        if( faceIsVisible(mLastTouchedFace,cubeHalfSize) )
198 174
          {
199
          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouchPointCastOntoFace);
175
          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouch);
200 176

  
201
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
202
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
203
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
177
          float qX= (mTouch[0]+cubeHalfSize) / (2*cubeHalfSize);
178
          float qY= (mTouch[1]+cubeHalfSize) / (2*cubeHalfSize);
179
          float qZ= (mTouch[2]+cubeHalfSize) / (2*cubeHalfSize);
204 180

  
205 181
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return true;
206 182
          }
......
212 188

  
213 189
///////////////////////////////////////////////////////////////////////////////////////////////////
214 190

  
215
    public Static2D newRotation(Static4D rotQuaternion, float x, float y)
191
    public Static2D newRotation(Static4D rotatedTouchPoint)
216 192
      {
217 193
      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
218 194

  
219
      convertTouchPointToScreenSpace(rotQuaternion,x,y);
195
      mPoint[0] = rotatedTouchPoint.get1();
196
      mPoint[1] = rotatedTouchPoint.get2();
197
      mPoint[2] = rotatedTouchPoint.get3();
198

  
220 199
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
221 200

  
222
      mDiff[0] -= mTouchPointCastOntoFace[0];
223
      mDiff[1] -= mTouchPointCastOntoFace[1];
224
      mDiff[2] -= mTouchPointCastOntoFace[2];
201
      mDiff[0] -= mTouch[0];
202
      mDiff[1] -= mTouch[1];
203
      mDiff[2] -= mTouch[2];
225 204

  
226 205
      int xAxis = retFaceXaxis(mLastTouchedFace);
227 206
      int yAxis = retFaceYaxis(mLastTouchedFace);
228 207
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
229
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
208
      float offset= (mTouch[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
230 209

  
231
      mTouchPoint[0] = mPoint[0];
232
      mTouchPoint[1] = mPoint[1];
233
      mTouchPoint[2] = mPoint[2];
210
      mTouch[0] = mPoint[0];
211
      mTouch[1] = mPoint[1];
212
      mTouch[2] = mPoint[2];
234 213

  
235 214
      return new Static2D(mRotationVect,offset);
236 215
      }
237 216

  
238 217
///////////////////////////////////////////////////////////////////////////////////////////////////
239 218

  
240
    public float continueRotation(Static4D rotQuaternion, float x, float y)
219
    public float continueRotation(Static4D rotatedTouchPoint)
241 220
      {
242
      convertTouchPointToScreenSpace(rotQuaternion,x,y);
243

  
244
      mDiff[0] = mPoint[0]-mTouchPoint[0];
245
      mDiff[1] = mPoint[1]-mTouchPoint[1];
246
      mDiff[2] = mPoint[2]-mTouchPoint[2];
221
      mDiff[0] = rotatedTouchPoint.get1()-mTouch[0];
222
      mDiff[1] = rotatedTouchPoint.get2()-mTouch[1];
223
      mDiff[2] = rotatedTouchPoint.get3()-mTouch[2];
247 224

  
248 225
      int xAxis= retFaceXaxis(mLastTouchedFace);
249 226
      int yAxis= retFaceYaxis(mLastTouchedFace);

Also available in: Unified diff