Project

General

Profile

« Previous | Next » 

Revision d4374cd3

Added by Leszek Koltunski about 5 years ago

RubikCube: make the post-rotation effect 'nice'.

View differences:

src/main/java/org/distorted/examples/rubik/RubikCube.java
42 42

  
43 43
class RubikCube
44 44
{
45
    private static final int POST_ROTATION_MILLISEC = 500;
45 46
    private static final int TEXTURE_SIZE = 100;
46 47

  
47 48
    private static final Static3D VectX = new Static3D(1,0,0);
......
54 55
    private Static3D[][][] mRotationAxis;
55 56
    private Dynamic1D[][][] mRotationAngle;
56 57
    private Static3D[][][] mCurrentPosition;
57
    private Static1D mRotationAngleStatic, mRotationAngleNearest;
58
    private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
58 59
    private DistortedTexture mTexture;
59 60
    private DistortedEffects mEffectsListeningForNow;
60 61

  
......
67 68
      {
68 69
      mSize = size;
69 70

  
70
      mRotationAngleStatic  = new Static1D(0);
71
      mRotationAngleNearest = new Static1D(0);
71
      mRotationAngleStatic = new Static1D(0);
72
      mRotationAngleMiddle = new Static1D(0);
73
      mRotationAngleFinal  = new Static1D(0);
72 74

  
73 75
      mRotAxis= RubikSurfaceView.VECTX;
74 76
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
......
218 220
    void finishRotationCalledOnNextRender(EffectListener listener)
219 221
      {
220 222
      boolean first = true;
221
      int nearestAngleInDegrees = computeNearestAngle(mRotationAngleStatic.get1());
222

  
223

  
224
      android.util.Log.e("cube", "finish: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
223
      float startingAngle = mRotationAngleStatic.get1();
224
      int nearestAngleInDegrees = computeNearestAngle(startingAngle);
225 225

  
226

  
227
      mRotationAngleNearest.set1(nearestAngleInDegrees);
226
      mRotationAngleFinal.set1(nearestAngleInDegrees);
227
      mRotationAngleMiddle.set1( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
228 228

  
229 229
      for(int x=0; x<mSize; x++)
230 230
        for(int y=0; y<mSize; y++)
......
233 233
              {
234 234
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
235 235
                {
236
                mRotationAngle[x][y][z].makeRunNowFor(2000);
237
                mRotationAngle[x][y][z].add(mRotationAngleNearest);
236
                mRotationAngle[x][y][z].makeRunNowFor(POST_ROTATION_MILLISEC);
237
                mRotationAngle[x][y][z].add(mRotationAngleMiddle);
238
                mRotationAngle[x][y][z].add(mRotationAngleFinal);
238 239

  
239 240
                if( first )
240 241
                  {
......
257 258
      float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
258 259
      float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
259 260

  
260
      android.util.Log.e("cube", "remove: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
261

  
262 261
      mRotationAngleStatic.set1(0);
263
      mRotationAngleNearest.set1(0);
264 262

  
265 263
      float qx=0,qy=0,qz=0;
266 264

  
src/main/java/org/distorted/examples/rubik/RubikRenderer.java
47 47
    private Static4D mTempCurrent, mTempAccumulated;
48 48
    private float mCubeSizeInScreenSpace;
49 49
    private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
50
    private boolean mCanRotate;
50 51
    private RubikCube mCube;
51 52

  
52 53
///////////////////////////////////////////////////////////////////////////////////////////////////
......
70 71
      mFinishDragCurrent     = false;
71 72
      mFinishDragAccumulated = false;
72 73

  
74
      mCanRotate = true;
75

  
73 76
      mCube = new RubikCube(NUM_CUBES, mMove, mScale, mQuatCurrent, mQuatAccumulated);
74 77
      }
75 78

  
......
95 98

  
96 99
      if( mFinishRotation )
97 100
        {
101
        mCanRotate = false;
98 102
        mFinishRotation=false;
99 103
        mCube.finishRotationCalledOnNextRender(this);
100 104
        }
......
103 107
        {
104 108
        mRemoveRotation=false;
105 109
        mCube.removeRotationCalledOnNextRender(this);
110
        mCanRotate = true;
106 111
        }
107 112
      }
108 113

  
......
181 186
      return mCubeSizeInScreenSpace;
182 187
      }
183 188

  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

  
191
    boolean canRotate()
192
      {
193
      return mCanRotate;
194
      }
195

  
184 196
///////////////////////////////////////////////////////////////////////////////////////////////////
185 197

  
186 198
    RubikCube getCube()
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
45 45

  
46 46
    private static final int[] VECT = {VECTX,VECTY,VECTZ};
47 47

  
48
    private boolean mDragging, mBeginRot;
48
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
49 49
    private int mX, mY;
50 50
    private Static4D mQuatCurrent, mQuatAccumulated;
51 51
    private int mRotationVect;
......
107 107
                                       mY = y;
108 108
                                       mLastTouchedFace = faceTouched(x,y);
109 109

  
110
                                       if( mLastTouchedFace != NONE ) { mBeginRot = true; mDragging = false; }
111
                                       else                           { mDragging = true; mBeginRot = false; }
110
                                       if( mLastTouchedFace != NONE )
111
                                         {
112
                                         mDragging           = false;
113
                                         mBeginningRotation  = mRenderer.canRotate();
114
                                         mContinuingRotation = false;
115
                                         }
116
                                       else
117
                                         {
118
                                         mDragging           = true;
119
                                         mBeginningRotation  = false;
120
                                         mContinuingRotation = false;
121
                                         }
112 122
                                       break;
113 123
         case MotionEvent.ACTION_MOVE: if( mDragging )
114 124
                                         {
115 125
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
116 126
                                         mRenderer.setQuatCurrent(mQuatCurrent);
117 127
                                         }
118
                                       else if( mBeginRot )
128
                                       if( mBeginningRotation )
119 129
                                         {
120 130
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100;
121 131

  
122 132
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
123 133
                                           {
124 134
                                           addNewRotation(x,y);
125
                                           mBeginRot = false;
135
                                           mBeginningRotation = false;
136
                                           mContinuingRotation= true;
126 137
                                           }
127 138
                                         }
128
                                       else
139
                                       else if( mContinuingRotation )
129 140
                                         {
130 141
                                         continueRotation(x,y);
131 142
                                         }
132 143
                                       break;
133
         case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
134
                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
135
                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
136
                                       mRenderer.setQuatCurrent(mQuatCurrent);
137
                                       mRenderer.setQuatAccumulated(mQuatAccumulated);
144
         case MotionEvent.ACTION_UP  : if( mDragging )
145
                                         {
146
                                         mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
147
                                         mQuatCurrent.set(0f, 0f, 0f, 1f);
148
                                         mRenderer.setQuatCurrent(mQuatCurrent);
149
                                         mRenderer.setQuatAccumulated(mQuatAccumulated);
150
                                         }
151

  
152
                                       if( mContinuingRotation )
153
                                         {
154
                                         finishRotation();
155
                                         }
156

  
138 157
                                       break;
139 158
         }
140 159

  

Also available in: Unified diff