Revision 775e675d
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/object/RubikCubeMovement.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.object; |
21 | 21 |
|
22 |
import org.distorted.library.type.Static2D; |
|
22 | 23 |
import org.distorted.library.type.Static4D; |
23 | 24 |
import org.distorted.magic.RubikSurfaceView; |
24 | 25 |
|
... | ... | |
36 | 37 |
|
37 | 38 |
private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ}; |
38 | 39 |
|
39 |
// Moving the finger from the middle of the vertical screen to the right edge will rotate a |
|
40 |
// given face by SWIPING_SENSITIVITY/2 degrees. |
|
41 |
private final static int SWIPING_SENSITIVITY = 240; |
|
42 |
|
|
43 |
private RubikCube mCube; |
|
44 |
private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space |
|
40 |
private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; |
|
45 | 41 |
private int mRotationVect; |
46 | 42 |
|
47 | 43 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
51 | 47 |
return (y>x) ? (y>=-x) : (y< -x); |
52 | 48 |
} |
53 | 49 |
|
54 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
55 |
|
|
56 |
private boolean faceIsVisible(int face, float cubeHalfSize) |
|
57 |
{ |
|
58 |
int sign = retFaceSign(face); |
|
59 |
int zAxis= retFaceZaxis(face); |
|
60 |
|
|
61 |
return sign*mCamera[zAxis] > cubeHalfSize; |
|
62 |
} |
|
63 |
|
|
64 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
65 |
|
|
66 |
private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y) |
|
67 |
{ |
|
68 |
Static4D touchPoint = new Static4D(x, y, 0, 0); |
|
69 |
Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated); |
|
70 |
|
|
71 |
mPoint[0] = rotatedTouchPoint.get1(); |
|
72 |
mPoint[1] = rotatedTouchPoint.get2(); |
|
73 |
mPoint[2] = rotatedTouchPoint.get3(); |
|
74 |
} |
|
75 |
|
|
76 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
77 |
|
|
78 |
private void convertCameraPointToScreenSpace(Static4D accumulated, float cameraDistance) |
|
79 |
{ |
|
80 |
Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0); |
|
81 |
Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated); |
|
82 |
|
|
83 |
mCamera[0] = rotatedCamera.get1(); |
|
84 |
mCamera[1] = rotatedCamera.get2(); |
|
85 |
mCamera[2] = rotatedCamera.get3(); |
|
86 |
} |
|
87 |
|
|
88 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
89 |
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace, |
|
90 |
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'. |
|
91 |
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize. |
|
92 |
|
|
93 |
private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output) |
|
94 |
{ |
|
95 |
int sign = retFaceSign(face); |
|
96 |
int zAxis= retFaceZaxis(face); |
|
97 |
float diff = mPoint[zAxis]-mCamera[zAxis]; |
|
98 |
|
|
99 |
float ratio = diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f; |
|
100 |
|
|
101 |
output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0]; |
|
102 |
output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1]; |
|
103 |
output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2]; |
|
104 |
} |
|
105 |
|
|
106 | 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
107 | 51 |
|
108 | 52 |
private int retFaceSign(int face) |
... | ... | |
172 | 116 |
} |
173 | 117 |
|
174 | 118 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
175 |
// PUBLIC API |
|
119 |
|
|
120 |
private boolean faceIsVisible(int face, float cubeHalfSize) |
|
121 |
{ |
|
122 |
int sign = retFaceSign(face); |
|
123 |
int zAxis= retFaceZaxis(face); |
|
124 |
|
|
125 |
return sign*mCamera[zAxis] > cubeHalfSize; |
|
126 |
} |
|
127 |
|
|
176 | 128 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
177 | 129 |
|
178 |
public RubikCubeMovement(RubikCube cube)
|
|
130 |
private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
|
|
179 | 131 |
{ |
180 |
mCube = cube; |
|
132 |
Static4D touchPoint = new Static4D(x, y, 0, 0); |
|
133 |
Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated); |
|
181 | 134 |
|
135 |
mPoint[0] = rotatedTouchPoint.get1(); |
|
136 |
mPoint[1] = rotatedTouchPoint.get2(); |
|
137 |
mPoint[2] = rotatedTouchPoint.get3(); |
|
138 |
} |
|
139 |
|
|
140 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
141 |
|
|
142 |
private void convertCameraPointToScreenSpace(Static4D accumulated, float cameraDistance) |
|
143 |
{ |
|
144 |
Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0); |
|
145 |
Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated); |
|
146 |
|
|
147 |
mCamera[0] = rotatedCamera.get1(); |
|
148 |
mCamera[1] = rotatedCamera.get2(); |
|
149 |
mCamera[2] = rotatedCamera.get3(); |
|
150 |
} |
|
151 |
|
|
152 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
153 |
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace, |
|
154 |
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'. |
|
155 |
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize. |
|
156 |
|
|
157 |
private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output) |
|
158 |
{ |
|
159 |
int sign = retFaceSign(face); |
|
160 |
int zAxis= retFaceZaxis(face); |
|
161 |
float diff = mPoint[zAxis]-mCamera[zAxis]; |
|
162 |
|
|
163 |
float ratio = diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f; |
|
164 |
|
|
165 |
output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0]; |
|
166 |
output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1]; |
|
167 |
output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2]; |
|
168 |
} |
|
169 |
|
|
170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 |
// PUBLIC API |
|
172 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
173 |
|
|
174 |
public RubikCubeMovement() |
|
175 |
{ |
|
182 | 176 |
mRotationVect = VECT[0]; |
183 | 177 |
|
184 | 178 |
mPoint = new float[3]; |
... | ... | |
192 | 186 |
|
193 | 187 |
public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y) |
194 | 188 |
{ |
195 |
float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
|
|
189 |
float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
|
|
196 | 190 |
|
197 | 191 |
convertTouchPointToScreenSpace(accumulated,x,y); |
198 | 192 |
convertCameraPointToScreenSpace(accumulated, cameraDistance); |
... | ... | |
216 | 210 |
|
217 | 211 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
218 | 212 |
|
219 |
public void addNewRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
|
|
213 |
public Static2D newRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
|
|
220 | 214 |
{ |
221 |
float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
|
|
215 |
float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
|
|
222 | 216 |
|
223 | 217 |
convertTouchPointToScreenSpace(accumulated,x,y); |
224 | 218 |
castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff); |
... | ... | |
236 | 230 |
mTouchPoint[1] = mPoint[1]; |
237 | 231 |
mTouchPoint[2] = mPoint[2]; |
238 | 232 |
|
239 |
mCube.addNewRotation(mRotationVect, (int)(mCube.getSize()*offset) );
|
|
233 |
return new Static2D(mRotationVect,offset);
|
|
240 | 234 |
} |
241 | 235 |
|
242 | 236 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
243 | 237 |
|
244 |
public void continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y, int scrMin)
|
|
238 |
public float continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
|
|
245 | 239 |
{ |
246 | 240 |
convertTouchPointToScreenSpace(accumulated,x,y); |
247 | 241 |
|
... | ... | |
254 | 248 |
int sign = retFaceRotationSign(lastTouchedFace); |
255 | 249 |
float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]); |
256 | 250 |
|
257 |
mCube.continueRotation(SWIPING_SENSITIVITY*sign*angle/scrMin);
|
|
251 |
return sign*angle;
|
|
258 | 252 |
} |
259 | 253 |
} |
Also available in: Unified diff
Further simplifications for object movement - remove from it a reference to the Object altogether.