Project

General

Profile

« Previous | Next » 

Revision c896c0b3

Added by Leszek Koltunski almost 5 years ago

Simplifications for object movement.

View differences:

src/main/java/org/distorted/magic/RubikSurfaceView.java
54 54
    private int mButton = RubikSize.SIZE3.ordinal();
55 55

  
56 56
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
57
    private int mX, mY;
57
    private float mX, mY;
58 58
    private int mLastTouchedFace;
59 59
    private float mCameraDistance;
60 60
    private int mScreenWidth, mScreenHeight, mScreenMin;
......
352 352
    public boolean onTouchEvent(MotionEvent event)
353 353
      {
354 354
      int action = event.getAction();
355
      int x = (int)event.getX();
356
      int y = (int)event.getY();
355
      float x = event.getX() - mScreenWidth*0.5f;
356
      float y = mScreenHeight*0.5f -event.getY();
357 357

  
358 358
      switch(action)
359 359
         {
360 360
         case MotionEvent.ACTION_DOWN: mX = x;
361 361
                                       mY = y;
362
                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance, x,y, mScreenWidth, mScreenHeight);
362
                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance, x, y);
363 363

  
364 364
                                       if( mLastTouchedFace != RubikCubeMovement.NONE )
365 365
                                         {
......
376 376
                                       break;
377 377
         case MotionEvent.ACTION_MOVE: if( mDragging )
378 378
                                         {
379
                                         mTempCurrent.set(quatFromDrag(mX-x,mY-y));
379
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
380 380
                                         mRenderer.setQuatCurrentOnNextRender();
381 381

  
382 382
                                         int minimumDist = (mScreenMin*mScreenMin)/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY);
......
397 397

  
398 398
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
399 399
                                           {
400
                                           mMovement.addNewRotation(mQuatAccumulated,mLastTouchedFace, x,y, mScreenWidth, mScreenHeight);
400
                                           mMovement.addNewRotation(mQuatAccumulated,mLastTouchedFace, x, y);
401 401
                                           mBeginningRotation = false;
402 402
                                           mContinuingRotation= true;
403 403
                                           }
404 404
                                         }
405 405
                                       else if( mContinuingRotation )
406 406
                                         {
407
                                         mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x,y, mScreenWidth, mScreenHeight);
407
                                         mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x, y, mScreenMin);
408 408
                                         }
409 409
                                       break;
410 410
         case MotionEvent.ACTION_UP  : if( mDragging )
src/main/java/org/distorted/object/RubikCubeMovement.java
63 63

  
64 64
///////////////////////////////////////////////////////////////////////////////////////////////////
65 65

  
66
    private void convertTouchPointToScreenSpace(Static4D accumulated, int x, int y, int scrW, int scrH)
66
    private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
67 67
      {
68
      float halfScrWidth  = scrW*0.5f;
69
      float halfScrHeight = scrH*0.5f;
70
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
68
      Static4D touchPoint = new Static4D(x, y, 0, 0);
71 69
      Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated);
72 70

  
73 71
      mPoint[0] = rotatedTouchPoint.get1();
......
192 190

  
193 191
///////////////////////////////////////////////////////////////////////////////////////////////////
194 192

  
195
    public int faceTouched(Static4D accumulated, float cameraDistance, int x, int y, int scrW, int scrH)
193
    public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y)
196 194
      {
197 195
      float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
198 196

  
199
      convertTouchPointToScreenSpace(accumulated,x,y, scrW, scrH);
197
      convertTouchPointToScreenSpace(accumulated,x,y);
200 198
      convertCameraPointToScreenSpace(accumulated, cameraDistance);
201 199

  
202 200
      for(int face=FRONT; face<=BOTTOM; face++)
......
218 216

  
219 217
///////////////////////////////////////////////////////////////////////////////////////////////////
220 218

  
221
    public void addNewRotation(Static4D accumulated, int lastTouchedFace, int x, int y, int scrW, int scrH)
219
    public void addNewRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
222 220
      {
223 221
      float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
224 222

  
225
      convertTouchPointToScreenSpace(accumulated, x,y, scrW, scrH);
223
      convertTouchPointToScreenSpace(accumulated,x,y);
226 224
      castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff);
227 225

  
228 226
      mDiff[0] -= mTouchPointCastOntoFace[0];
......
243 241

  
244 242
///////////////////////////////////////////////////////////////////////////////////////////////////
245 243

  
246
    public void continueRotation(Static4D accumulated, int lastTouchedFace, int x, int y, int scrW, int scrH)
244
    public void continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y, int scrMin)
247 245
      {
248
      convertTouchPointToScreenSpace(accumulated, x,y, scrW, scrH);
246
      convertTouchPointToScreenSpace(accumulated,x,y);
249 247

  
250 248
      mDiff[0] = mPoint[0]-mTouchPoint[0];
251 249
      mDiff[1] = mPoint[1]-mTouchPoint[1];
......
256 254
      int sign = retFaceRotationSign(lastTouchedFace);
257 255
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
258 256

  
259
      int scrMin = scrW<scrH ? scrW:scrH;
260

  
261 257
      mCube.continueRotation(SWIPING_SENSITIVITY*sign*angle/scrMin);
262 258
      }
263 259
}

Also available in: Unified diff