Project

General

Profile

« Previous | Next » 

Revision 12ad3fca

Added by Leszek Koltunski about 4 years ago

Progress with object Movement - almost finished.

View differences:

src/main/java/org/distorted/magic/RubikSurfaceView.java
27 27
import android.view.MotionEvent;
28 28

  
29 29
import org.distorted.library.type.Static2D;
30
import org.distorted.library.type.Static3D;
30 31
import org.distorted.library.type.Static4D;
31 32
import org.distorted.object.RubikObject;
32 33
import org.distorted.object.RubikObjectMovement;
......
59 60
    private RubikRenderer mRenderer;
60 61
    private RubikObjectMovement mMovement;
61 62
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
62
    private float mX, mY;
63 63
    private int mScreenWidth, mScreenHeight, mScreenMin;
64 64

  
65
    private float mX, mY;
66
    private float mStartRotX, mStartRotY;
67
    private float mAxisX, mAxisY;
68

  
65 69
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
66 70
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
67 71
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
......
74 78
      mScreenWidth = width;
75 79
      mScreenHeight= height;
76 80

  
77
      mScreenMin = width<height ? width:height;
81
      mScreenMin = Math.min(width, height);
78 82
      }
79 83

  
80 84
///////////////////////////////////////////////////////////////////////////////////////////////////
......
146 150
      return new Static4D(0f, 0f, 0f, 1f);
147 151
      }
148 152

  
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

  
155
    private void setUpDragOrRotate(float x, float y)
156
      {
157
      Static4D touchPoint1 = new Static4D(x, y, 0, 0);
158
      Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
159
      Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
160

  
161
      if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
162
        {
163
        mDragging           = false;
164
        mBeginningRotation  = mRenderer.canRotate();
165
        mContinuingRotation = false;
166
        }
167
      else
168
        {
169
        mDragging           = mRenderer.canDrag();
170
        mBeginningRotation  = false;
171
        mContinuingRotation = false;
172
        }
173
      }
174

  
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
177

  
178
    private void computeCurrentAxis(Static3D axis)
179
      {
180
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
181
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
182

  
183
      mAxisX =result.get0();
184
      mAxisY =result.get1();
185

  
186
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
187
      mAxisX /= len;
188
      mAxisY /= len;
189

  
190
      android.util.Log.e("axis", "axis 2D: "+mAxisX+" , "+mAxisY);
191
      }
192

  
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

  
195
    private float continueRotation(float dx, float dy)
196
      {
197
      float alpha = dx*mAxisX + dy*mAxisY;
198
      float x = dx - alpha*mAxisX;
199
      float y = dy - alpha*mAxisY;
200

  
201
      float len = (float)Math.sqrt(x*x + y*y);
202

  
203

  
204

  
205
      return len;
206
      }
207

  
149 208
///////////////////////////////////////////////////////////////////////////////////////////////////
150 209
// return quat1*quat2
151 210

  
......
220 279
        }
221 280
      }
222 281

  
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

  
225
    private void setUpDragOrRotate(float x, float y)
226
      {
227
      Static4D touchPoint1 = new Static4D(x, y, 0, 0);
228
      Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
229
      Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
230

  
231
      if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
232
        {
233
        mDragging           = false;
234
        mBeginningRotation  = mRenderer.canRotate();
235
        mContinuingRotation = false;
236
        }
237
      else
238
        {
239
        mDragging           = mRenderer.canDrag();
240
        mBeginningRotation  = false;
241
        mContinuingRotation = false;
242
        }
243
      }
244

  
245 282
///////////////////////////////////////////////////////////////////////////////////////////////////
246 283

  
247 284
    @Override
......
261 298
                                         {
262 299
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
263 300
                                           {
301
                                           mStartRotX = x;
302
                                           mStartRotY = y;
303

  
264 304
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
265 305
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
266 306

  
267 307
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
268 308
                                           RubikObject object = mRenderer.getObject();
269 309

  
270
                                           object.beginNewRotation( (int)rot.get0(), object.returnRowFromOffset(rot.get1()) );
310
                                           int axis = (int)rot.get0();
311
                                           computeCurrentAxis( object.getRotationAxis()[axis] );
312

  
313
                                           object.beginNewRotation( axis, object.returnRowFromOffset(rot.get1()) );
271 314

  
272 315
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
273 316
                                             {
......
281 324
                                         }
282 325
                                       else if( mContinuingRotation )
283 326
                                         {
284
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
285
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
327
                                         //Static4D touchPoint3 = new Static4D(x, y, 0, 0);
328
                                         //Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
286 329

  
287
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
330
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
331
                                         //float angle = mMovement.continueRotation(rotatedTouchPoint3);
288 332
                                         mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
289 333
                                         }
290 334
                                       else if( mDragging )

Also available in: Unified diff