Revision 9cd7695f
Added by Leszek Koltunski about 4 years ago
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; |
Also available in: Unified diff
Improve RubikCubeMovement