Project

General

Profile

« Previous | Next » 

Revision 93594b95

Added by Leszek Koltunski over 4 years ago

Improve RubikCubeMovement

View differences:

src/main/java/org/distorted/object/RubikCubeMovement.java
26 26

  
27 27
class RubikCubeMovement extends RubikObjectMovement
28 28
{
29
    private final static int NONE   =-1;
30
    private final static int FRONT  = 0;  //
31
    private final static int BACK   = 1;  //
32
    private final static int LEFT   = 2;  // has to be 6 consecutive ints
33
    private final static int RIGHT  = 3;  // FRONT ... BOTTOM
34
    private final static int TOP    = 4;  //
35
    private final static int BOTTOM = 5;  //
29
    private final static int LEFT   = 0;  // axisX left
30
    private final static int RIGHT  = 1;  // axisX right
31
    private final static int BOTTOM = 2;  // axisY left
32
    private final static int TOP    = 3;  // axisY right
33
    private final static int BACK   = 4;  // axisZ left
34
    private final static int FRONT  = 5;  // axisZ right
36 35

  
37 36
    private static final int VECTX  = 0;  //
38 37
    private static final int VECTY  = 1;  // don't change this
39 38
    private static final int VECTZ  = 2;  //
40 39

  
41
    private static final int[] VECT = {VECTX,VECTY,VECTZ};
42

  
43 40
    private float[] mPoint, mCamera, mDiff, mTouch;
44 41
    private int mRotationVect, mLastTouchedFace;
45 42

  
......
71 68

  
72 69
    private int retFaceXaxis(int face)
73 70
      {
74
      switch(face)
75
        {
76
        case FRONT :
77
        case BACK  : return VECTX;
78
        case LEFT  :
79
        case RIGHT : return VECTZ;
80
        case TOP   :
81
        case BOTTOM: return VECTX;
82
        }
83

  
84
      return -1;
71
      return face==LEFT || face==RIGHT ? VECTZ : VECTX;
85 72
      }
86 73

  
87 74
///////////////////////////////////////////////////////////////////////////////////////////////////
88 75

  
89 76
    private int retFaceYaxis(int face)
90 77
      {
91
      switch(face)
92
        {
93
        case FRONT :
94
        case BACK  : return VECTY;
95
        case LEFT  :
96
        case RIGHT : return VECTY;
97
        case TOP   :
98
        case BOTTOM: return VECTZ;
99
        }
100

  
101
      return -1;
78
      return face==TOP || face==BOTTOM ? VECTZ : VECTY;
102 79
      }
103 80

  
104 81
///////////////////////////////////////////////////////////////////////////////////////////////////
105 82

  
106
    private int retFaceZaxis(int face)
83
    private int retFaceAxis(int face)
107 84
      {
108 85
      switch(face)
109 86
        {
......
120 97

  
121 98
///////////////////////////////////////////////////////////////////////////////////////////////////
122 99

  
123
    private boolean faceIsVisible(int face, float cubeHalfSize)
100
    private boolean faceIsVisible(int face)
124 101
      {
125 102
      int sign = retFaceSign(face);
126
      int zAxis= retFaceZaxis(face);
103
      int zAxis= retFaceAxis(face);
127 104

  
128
      return sign*mCamera[zAxis] > cubeHalfSize;
105
      return sign*mCamera[zAxis] > 0.5f;
129 106
      }
130 107

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

  
136
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
113
    private void castTouchPointOntoFace(int face, float[] output)
137 114
      {
138 115
      int sign = retFaceSign(face);
139
      int zAxis= retFaceZaxis(face);
116
      int zAxis= retFaceAxis(face);
140 117
      float diff = mPoint[zAxis]-mCamera[zAxis];
141 118

  
142
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
119
      float ratio =  diff!=0.0f ? (sign*0.5f-mCamera[zAxis])/diff : 0.0f;
143 120

  
144 121
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
145 122
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
......
162 139

  
163 140
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
164 141
      {
165
      float cubeHalfSize= RubikObject.OBJECT_SCREEN_RATIO*0.5f;
166

  
167
      mPoint[0]  = rotatedTouchPoint.get0();
168
      mPoint[1]  = rotatedTouchPoint.get1();
169
      mPoint[2]  = rotatedTouchPoint.get2();
142
      mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
143
      mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
144
      mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
170 145

  
171
      mCamera[0] = rotatedCamera.get0();
172
      mCamera[1] = rotatedCamera.get1();
173
      mCamera[2] = rotatedCamera.get2();
146
      mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
147
      mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
148
      mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
174 149

  
175
      for( mLastTouchedFace=FRONT; mLastTouchedFace<=BOTTOM; mLastTouchedFace++)
150
      for( mLastTouchedFace=LEFT; mLastTouchedFace<=FRONT; mLastTouchedFace++)
176 151
        {
177
        if( faceIsVisible(mLastTouchedFace,cubeHalfSize) )
152
        if( faceIsVisible(mLastTouchedFace) )
178 153
          {
179
          castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize, mTouch);
154
          castTouchPointOntoFace(mLastTouchedFace, mTouch);
180 155

  
181
          float qX= (mTouch[0]+cubeHalfSize) / (2*cubeHalfSize);
182
          float qY= (mTouch[1]+cubeHalfSize) / (2*cubeHalfSize);
183
          float qZ= (mTouch[2]+cubeHalfSize) / (2*cubeHalfSize);
184

  
185
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return true;
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;
186 159
          }
187 160
        }
188 161

  
189
      mLastTouchedFace = NONE;
190 162
      return false;
191 163
      }
192 164

  
......
194 166

  
195 167
    public Static2D newRotation(Static4D rotatedTouchPoint)
196 168
      {
197
      float cubeHalfSize= RubikObject.OBJECT_SCREEN_RATIO*0.5f;
198

  
199
      mPoint[0] = rotatedTouchPoint.get0();
200
      mPoint[1] = rotatedTouchPoint.get1();
201
      mPoint[2] = rotatedTouchPoint.get2();
169
      mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
170
      mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
171
      mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
202 172

  
203
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
173
      castTouchPointOntoFace(mLastTouchedFace,mDiff);
204 174

  
205 175
      mDiff[0] -= mTouch[0];
206 176
      mDiff[1] -= mTouch[1];
......
208 178

  
209 179
      int xAxis = retFaceXaxis(mLastTouchedFace);
210 180
      int yAxis = retFaceYaxis(mLastTouchedFace);
211
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
212
      float offset= (mTouch[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
181
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
182
      float offset= mTouch[mRotationVect]+0.5f;
213 183

  
214 184
      mTouch[0] = mPoint[0];
215 185
      mTouch[1] = mPoint[1];
......
222 192

  
223 193
    public float continueRotation(Static4D rotatedTouchPoint)
224 194
      {
225
      mDiff[0] = rotatedTouchPoint.get0()-mTouch[0];
226
      mDiff[1] = rotatedTouchPoint.get1()-mTouch[1];
227
      mDiff[2] = rotatedTouchPoint.get2()-mTouch[2];
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];
228 198

  
229 199
      int xAxis= retFaceXaxis(mLastTouchedFace);
230 200
      int yAxis= retFaceYaxis(mLastTouchedFace);
231 201
      int sign = retFaceRotationSign(mLastTouchedFace);
232 202
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
233 203

  
234
      return sign*angle;
204
      return sign*angle*0.5f;
235 205
      }
236 206
}

Also available in: Unified diff