Project

General

Profile

« Previous | Next » 

Revision fb377dae

Added by Leszek Koltunski over 3 years ago

Major progress with Dino; rotations (almost?) work now.

View differences:

src/main/java/org/distorted/objects/RubikMovement.java
27 27

  
28 28
public abstract class RubikMovement
29 29
  {
30
  private int mLastTouchedAxis;
30
  private int mLastTouchedFace, mNumFaceAxis;
31 31
  private float[] mPoint, mCamera, mTouch;
32 32
  private float[] mPoint2D, mMove2D;
33 33
  private float[][][] mCastAxis;
34
  private int mNumRotAxis, mNumFaceAxis;
34
  private int[] mEnabledRotAxis;
35 35
  private float mDistanceCenterFace3D, mDistanceCenterFace2D;
36 36
  private Static3D[] mFaceAxis;
37 37

  
38 38
///////////////////////////////////////////////////////////////////////////////////////////////////
39 39

  
40 40
  abstract boolean isInsideFace(float[] point);
41
  abstract void computeEnabledAxis(int face, float[] touchPoint, int[] enabledAxis);
41 42

  
42 43
///////////////////////////////////////////////////////////////////////////////////////////////////
43 44

  
......
50 51
    mPoint2D = new float[2];
51 52
    mMove2D  = new float[2];
52 53

  
53
    mNumRotAxis = rotAxis.length;
54 54
    mFaceAxis   = faceAxis;
55 55
    mNumFaceAxis= mFaceAxis.length;
56 56

  
57
    mEnabledRotAxis = new int[rotAxis.length+1];
58

  
57 59
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
58 60
    mDistanceCenterFace2D = distance2D; // distance from the center of a face to its edge
59 61

  
60 62
    // mCastAxis[1][2]{0,1} are the 2D coords of the 2nd axis cast onto the face defined by the
61 63
    // 1st pair (axis,lr)
62
    mCastAxis = new float[mNumFaceAxis][mNumRotAxis][2];
64
    mCastAxis = new float[mNumFaceAxis][rotAxis.length][2];
63 65

  
64
    for( int casted=0; casted<mNumRotAxis; casted++)
66
    for( int casted=0; casted<rotAxis.length; casted++)
65 67
      {
66 68
      Static3D a = rotAxis[casted];
67 69
      mPoint[0]= a.get0();
......
88 90
///////////////////////////////////////////////////////////////////////////////////////////////////
89 91
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
90 92

  
91
  private int computeRotationIndex(int faceAxis, float[] move2D)
93
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
92 94
    {
93 95
    float cosAngle, minCosAngle = Float.MAX_VALUE;
94
    int minIndex=-1;
96
    int minIndex=-1, index;
95 97
    float m0 = move2D[0];
96 98
    float m1 = move2D[1];
97 99
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
98
    float x,y;
99 100

  
100 101
    if( len!=0.0f )
101 102
      {
......
108 109
      m1 = 0.0f;  //
109 110
      }
110 111

  
111
    for(int rotAxis=0; rotAxis<mNumRotAxis; rotAxis++)
112
    int numAxis = enabled[0];
113

  
114
    for(int axis=1; axis<=numAxis; axis++)
112 115
      {
113
      x = mCastAxis[faceAxis][rotAxis][0];
114
      y = mCastAxis[faceAxis][rotAxis][1];
116
      index = enabled[axis];
117
      cosAngle = m0*mCastAxis[faceAxis][index][0] + m1*mCastAxis[faceAxis][index][1];
118
      if( cosAngle<0 ) cosAngle = -cosAngle;
115 119

  
116
      if( x*x + y*y > 0.01f )
120
      if( cosAngle<minCosAngle )
117 121
        {
118
        cosAngle = m0*x + m1*y;
119
        if( cosAngle<0 ) cosAngle = -cosAngle;
120

  
121
        if( cosAngle<minCosAngle )
122
          {
123
          minCosAngle=cosAngle;
124
          minIndex = rotAxis;
125
          }
122
        minCosAngle=cosAngle;
123
        minIndex = index;
126 124
        }
127 125
      }
128 126

  
......
242 240
    mCamera[1] = rotatedCamera.get1()/objectRatio;
243 241
    mCamera[2] = rotatedCamera.get2()/objectRatio;
244 242

  
245
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumFaceAxis; mLastTouchedAxis++)
243
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
246 244
      {
247
      if( faceIsVisible(mFaceAxis[mLastTouchedAxis]) )
245
      if( faceIsVisible(mFaceAxis[mLastTouchedFace]) )
248 246
        {
249
        castTouchPointOntoFace(mFaceAxis[mLastTouchedAxis], mTouch);
250
        convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedAxis], mPoint2D);
247
        castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
248
        convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mPoint2D);
251 249
        if( isInsideFace(mPoint2D) ) return true;
252 250
        }
253 251
      }
......
265 263
    mPoint[1] = rotatedTouchPoint.get1()/objectRatio;
266 264
    mPoint[2] = rotatedTouchPoint.get2()/objectRatio;
267 265

  
268
    castTouchPointOntoFace(mFaceAxis[mLastTouchedAxis], mTouch);
269
    convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedAxis], mMove2D);
266
    castTouchPointOntoFace(mFaceAxis[mLastTouchedFace], mTouch);
267
    convertTo2Dcoords(mTouch, mFaceAxis[mLastTouchedFace], mMove2D);
270 268

  
271 269
    mMove2D[0] -= mPoint2D[0];
272 270
    mMove2D[1] -= mPoint2D[1];
273 271

  
274
    int rotIndex = computeRotationIndex(mLastTouchedAxis, mMove2D);
275
    float offset = computeOffset(mPoint2D, mCastAxis[mLastTouchedAxis][rotIndex]);
272
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
273
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
274
    float offset = computeOffset(mPoint2D, mCastAxis[mLastTouchedFace][rotIndex]);
276 275

  
277 276
    return new Static2D(rotIndex,offset);
278 277
    }
......
281 280

  
282 281
  public int getTouchedFace()
283 282
    {
284
    return mLastTouchedAxis;
283
    return mLastTouchedFace;
285 284
    }
286 285

  
287 286
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff