Project

General

Profile

« Previous | Next » 

Revision 9cd7695f

Added by Leszek Koltunski over 4 years ago

Improve RubikCubeMovement

View differences:

src/main/java/org/distorted/magic/RubikRenderer.java
242 242
       mCanRotate      = false;
243 243
       mCanUI          = false;
244 244
       mRotationFinishedID = mNewObject.finishRotationNow(this);
245

  
246
       if( mRotationFinishedID==0 ) // failed to add effect - should never happen
247
         {
248
         mCanRotate = true;
249
         mCanUI     = true;
250
         }
245 251
       }
246 252

  
247 253
     if( mRemoveRotation )
src/main/java/org/distorted/object/RubikCube.java
40 40
class RubikCube extends RubikObject
41 41
{
42 42
  // the three rotation axis of a RubikCube. Must be normalized.
43
  private static final Static3D[] AXIS = new Static3D[]
43
  static final Static3D[] AXIS = new Static3D[]
44 44
         {
45 45
           new Static3D(1,0,0),
46 46
           new Static3D(0,1,0),
47 47
           new Static3D(0,0,1)
48 48
         };
49 49

  
50
  private static final int[] FACE_COLORS = new int[]
50
  static final int[] FACE_COLORS = new int[]
51 51
         {
52 52
           0xffffff00, 0xffffffff,   // AXIS[0]right (right-YELLOW) AXIS[0]left (left  -WHITE)
53 53
           0xff0000ff, 0xff00ff00,   // AXIS[1]right (top  -BLUE  ) AXIS[1]left (bottom-GREEN)
src/main/java/org/distorted/object/RubikCubeMovement.java
38 38
    private static final int VECTZ  = 2;  //
39 39

  
40 40
    private float[] mPoint, mCamera, mDiff, mTouch;
41
    private int mRotationVect, mLastTouchedFace;
41
    private int mRotationVect, mLastTouchedAxis, mLastTouchedLR;
42
    private int mNumAxis, mNumFacesPerAxis;
42 43

  
43 44
///////////////////////////////////////////////////////////////////////////////////////////////////
44 45

  
......
49 50

  
50 51
///////////////////////////////////////////////////////////////////////////////////////////////////
51 52

  
52
    private int retFaceSign(int face)
53
      {
54
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
55
      }
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
    private int retFaceRotationSign(int face)
53
    private int retFaceRotationSign(int axis, int lr)
60 54
      {
55
      int face = axis*mNumFacesPerAxis + lr;
61 56
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
62 57
      }
63 58

  
64 59
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
66
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
67
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceXaxis(RIGHT) = VECTZ.
68 60

  
69
    private int retFaceXaxis(int face)
61
    private int retFaceXaxis(int axis)
70 62
      {
71
      return face==LEFT || face==RIGHT ? VECTZ : VECTX;
63
      return axis==0 ? VECTZ : VECTX;
72 64
      }
73 65

  
74 66
///////////////////////////////////////////////////////////////////////////////////////////////////
75 67

  
76
    private int retFaceYaxis(int face)
68
    private int retFaceYaxis(int axis)
77 69
      {
78
      return face==TOP || face==BOTTOM ? VECTZ : VECTY;
70
      return axis==1 ? VECTZ : VECTY;
79 71
      }
80 72

  
81 73
///////////////////////////////////////////////////////////////////////////////////////////////////
82 74

  
83
    private int retFaceAxis(int face)
75
    private boolean faceIsVisible(int axis, int lr)
84 76
      {
85
      switch(face)
86
        {
87
        case FRONT :
88
        case BACK  : return VECTZ;
89
        case LEFT  :
90
        case RIGHT : return VECTX;
91
        case TOP   :
92
        case BOTTOM: return VECTY;
93
        }
94

  
95
      return -1;
96
      }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
    private boolean faceIsVisible(int face)
101
      {
102
      int sign = retFaceSign(face);
103
      int zAxis= retFaceAxis(face);
104

  
105
      return sign*mCamera[zAxis] > 0.5f;
77
      return (lr==0 ? -1:1)*mCamera[axis] > 0.5f;
106 78
      }
107 79

  
108 80
///////////////////////////////////////////////////////////////////////////////////////////////////
......
110 82
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
111 83
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
112 84

  
113
    private void castTouchPointOntoFace(int face, float[] output)
85
    private void castTouchPointOntoFace(int axis, int lr, float[] output)
114 86
      {
115
      int sign = retFaceSign(face);
116
      int zAxis= retFaceAxis(face);
117
      float diff = mPoint[zAxis]-mCamera[zAxis];
118

  
119
      float ratio =  diff!=0.0f ? (sign*0.5f-mCamera[zAxis])/diff : 0.0f;
87
      float diff = mPoint[axis]-mCamera[axis];
88
      float ratio =  diff!=0.0f ? ( (lr-0.5f)-mCamera[axis])/diff : 0.0f;
120 89

  
121 90
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
122 91
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
......
131 100
      mCamera= new float[3];
132 101
      mDiff  = new float[3];
133 102
      mTouch = new float[3];
103

  
104
      mNumAxis = RubikCube.AXIS.length;
105
      mNumFacesPerAxis = RubikCube.FACE_COLORS.length / mNumAxis;
134 106
      }
135 107

  
136 108
///////////////////////////////////////////////////////////////////////////////////////////////////
......
147 119
      mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
148 120
      mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
149 121

  
150
      for( mLastTouchedFace=LEFT; mLastTouchedFace<=FRONT; mLastTouchedFace++)
122
      for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
151 123
        {
152
        if( faceIsVisible(mLastTouchedFace) )
124
        for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
153 125
          {
154
          castTouchPointOntoFace(mLastTouchedFace, mTouch);
155

  
156
          if( mTouch[0]<=0.5f && mTouch[0]>=-0.5f &&
157
              mTouch[1]<=0.5f && mTouch[1]>=-0.5f &&
158
              mTouch[2]<=0.5f && mTouch[2]>=-0.5f  ) return true;
126
          if( faceIsVisible(mLastTouchedAxis, mLastTouchedLR) )
127
            {
128
            castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mTouch);
129

  
130
            if( mTouch[0]<=0.5f && mTouch[0]>=-0.5f &&
131
                mTouch[1]<=0.5f && mTouch[1]>=-0.5f &&
132
                mTouch[2]<=0.5f && mTouch[2]>=-0.5f  )  return true;
133
            }
159 134
          }
160 135
        }
161 136

  
......
170 145
      mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
171 146
      mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
172 147

  
173
      castTouchPointOntoFace(mLastTouchedFace,mDiff);
148
      castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
174 149

  
175 150
      mDiff[0] -= mTouch[0];
176 151
      mDiff[1] -= mTouch[1];
177 152
      mDiff[2] -= mTouch[2];
178 153

  
179
      int xAxis = retFaceXaxis(mLastTouchedFace);
180
      int yAxis = retFaceYaxis(mLastTouchedFace);
154
      int xAxis = retFaceXaxis(mLastTouchedAxis);
155
      int yAxis = retFaceYaxis(mLastTouchedAxis);
181 156
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
182 157
      float offset= mTouch[mRotationVect]+0.5f;
183 158

  
......
192 167

  
193 168
    public float continueRotation(Static4D rotatedTouchPoint)
194 169
      {
195
      mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[0];
196
      mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[1];
197
      mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[2];
170
      mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[0];
171
      mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[1];
172
      mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[2];
198 173

  
199
      int xAxis= retFaceXaxis(mLastTouchedFace);
200
      int yAxis= retFaceYaxis(mLastTouchedFace);
201
      int sign = retFaceRotationSign(mLastTouchedFace);
174
      int xAxis= retFaceXaxis(mLastTouchedAxis);
175
      int yAxis= retFaceYaxis(mLastTouchedAxis);
176
      int sign = retFaceRotationSign(mLastTouchedAxis, mLastTouchedLR);
202 177
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
203 178

  
204 179
      return sign*angle*0.5f;
src/main/java/org/distorted/object/RubikObject.java
394 394

  
395 395
  public void beginNewRotation(int axis, int row )
396 396
    {
397
    if( axis<0 || axis>=ROTATION_AXIS.length )
398
      {
399
      android.util.Log.e("object", "invalid rotation axis: "+axis);
400
      return;
401
      }
402
    if( row<0 || row>=mSize )
403
      {
404
      android.util.Log.e("object", "invalid rotation row: "+row);
405
      return;
406
      }
407

  
397 408
    mRotAxis = axis;
398 409
    mRotRow  = row;
399 410

  

Also available in: Unified diff