Revision 6e18bd32
Added by Leszek Koltunski about 5 years ago
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java | ||
---|---|---|
24 | 24 |
import android.content.pm.ConfigurationInfo; |
25 | 25 |
import android.opengl.GLSurfaceView; |
26 | 26 |
import android.util.AttributeSet; |
27 |
import android.view.MotionEvent; |
|
28 | 27 |
|
29 |
import org.distorted.library.type.Static4D;
|
|
28 |
import java.lang.ref.WeakReference;
|
|
30 | 29 |
|
31 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
32 | 31 |
|
33 | 32 |
class RubikSurfaceView extends GLSurfaceView |
34 | 33 |
{ |
35 |
// Moving the finger from the middle of the vertical screen to the right edge will rotate a |
|
36 |
// given face by SWIPING_SENSITIVITY/2 degrees. |
|
37 |
private final static int SWIPING_SENSITIVITY = 240; |
|
38 |
|
|
39 |
private final static int NONE =-1; |
|
40 |
private final static int FRONT = 0; // has to be 6 consecutive ints |
|
41 |
private final static int BACK = 1; // FRONT ... BOTTOM |
|
42 |
private final static int LEFT = 2; // |
|
43 |
private final static int RIGHT = 3; // |
|
44 |
private final static int TOP = 4; // |
|
45 |
private final static int BOTTOM = 5; // |
|
46 |
|
|
47 |
static final int VECTX = 0; // |
|
48 |
static final int VECTY = 1; // don't change this |
|
49 |
static final int VECTZ = 2; // |
|
50 |
|
|
51 |
private static final int[] VECT = {VECTX,VECTY,VECTZ}; |
|
52 |
|
|
53 |
private boolean mDragging, mBeginningRotation, mContinuingRotation; |
|
54 |
private int mX, mY; |
|
55 |
private Static4D mQuatCurrent, mQuatAccumulated; |
|
56 |
private int mRotationVect; |
|
57 |
private RubikRenderer mRenderer; |
|
58 |
|
|
59 |
private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space |
|
60 |
private int mLastTouchedFace; |
|
61 |
private int mScreenWidth, mScreenHeight, mScreenMin; |
|
62 |
private float mCameraDistance; |
|
34 |
private WeakReference<RubikActivity> mWeakAct; |
|
63 | 35 |
|
64 | 36 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
65 | 37 |
|
... | ... | |
69 | 41 |
|
70 | 42 |
if(!isInEditMode()) |
71 | 43 |
{ |
72 |
mRotationVect = VECT[0]; |
|
73 |
|
|
74 |
mPoint = new float[3]; |
|
75 |
mCamera= new float[3]; |
|
76 |
mDiff = new float[3]; |
|
77 |
mTouchPoint = new float[3]; |
|
78 |
mTouchPointCastOntoFace = new float[3]; |
|
79 |
|
|
80 |
mScreenWidth = mScreenHeight = mScreenMin = 0; |
|
81 |
|
|
82 |
mRenderer = new RubikRenderer(this); |
|
83 |
mRenderer.createCubeNow(RubikActivity.DEFAULT_SIZE); |
|
84 |
|
|
85 |
mQuatCurrent = new Static4D(0,0,0,1); |
|
86 |
mQuatAccumulated = mRenderer.initializeQuat(); |
|
44 |
mWeakAct = new WeakReference<>( (RubikActivity)context); |
|
87 | 45 |
|
88 | 46 |
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
89 | 47 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
90 | 48 |
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 ); |
91 |
setRenderer(mRenderer); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
96 |
|
|
97 |
@Override |
|
98 |
public boolean onTouchEvent(MotionEvent event) |
|
99 |
{ |
|
100 |
int action = event.getAction(); |
|
101 |
int x = (int)event.getX(); |
|
102 |
int y = (int)event.getY(); |
|
103 |
|
|
104 |
switch(action) |
|
105 |
{ |
|
106 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
107 |
mY = y; |
|
108 |
mLastTouchedFace = faceTouched(x,y); |
|
109 |
|
|
110 |
if( mLastTouchedFace != NONE ) |
|
111 |
{ |
|
112 |
mDragging = false; |
|
113 |
mBeginningRotation = mRenderer.canRotate(); |
|
114 |
mContinuingRotation = false; |
|
115 |
} |
|
116 |
else |
|
117 |
{ |
|
118 |
mDragging = true; |
|
119 |
mBeginningRotation = false; |
|
120 |
mContinuingRotation = false; |
|
121 |
} |
|
122 |
break; |
|
123 |
case MotionEvent.ACTION_MOVE: if( mDragging ) |
|
124 |
{ |
|
125 |
mQuatCurrent.set(quatFromDrag(mX-x,mY-y)); |
|
126 |
mRenderer.setQuatCurrent(mQuatCurrent); |
|
127 |
} |
|
128 |
if( mBeginningRotation ) |
|
129 |
{ |
|
130 |
int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100; |
|
131 |
|
|
132 |
if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating ) |
|
133 |
{ |
|
134 |
addNewRotation(x,y); |
|
135 |
mBeginningRotation = false; |
|
136 |
mContinuingRotation= true; |
|
137 |
} |
|
138 |
} |
|
139 |
else if( mContinuingRotation ) |
|
140 |
{ |
|
141 |
continueRotation(x,y); |
|
142 |
} |
|
143 |
break; |
|
144 |
case MotionEvent.ACTION_UP : if( mDragging ) |
|
145 |
{ |
|
146 |
mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated)); |
|
147 |
mQuatCurrent.set(0f, 0f, 0f, 1f); |
|
148 |
mRenderer.setQuatCurrent(mQuatCurrent); |
|
149 |
mRenderer.setQuatAccumulated(mQuatAccumulated); |
|
150 |
} |
|
151 |
|
|
152 |
if( mContinuingRotation ) |
|
153 |
{ |
|
154 |
finishRotation(); |
|
155 |
} |
|
156 |
|
|
157 |
break; |
|
158 |
} |
|
159 |
|
|
160 |
return true; |
|
161 |
} |
|
162 |
|
|
163 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
164 |
|
|
165 |
void setNewCubeSize(int newCubeSize) |
|
166 |
{ |
|
167 |
mRenderer.createCube(newCubeSize); |
|
168 |
} |
|
169 |
|
|
170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 |
|
|
172 |
void scrambleCube() |
|
173 |
{ |
|
174 |
mRenderer.scrambleCube(); |
|
175 |
} |
|
176 |
|
|
177 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
178 |
|
|
179 |
void setScreenSize(int width, int height) |
|
180 |
{ |
|
181 |
mScreenWidth = width; |
|
182 |
mScreenHeight= height; |
|
183 |
|
|
184 |
mScreenMin = width<height ? width:height; |
|
185 |
} |
|
186 |
|
|
187 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
188 |
|
|
189 |
void setCameraDist(float distance) |
|
190 |
{ |
|
191 |
mCameraDistance = distance; |
|
192 |
} |
|
193 |
|
|
194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
195 |
|
|
196 |
private int faceTouched(int xTouch, int yTouch) |
|
197 |
{ |
|
198 |
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f; |
|
199 |
|
|
200 |
convertTouchPointToScreenSpace(xTouch,yTouch); |
|
201 |
convertCameraPointToScreenSpace(); |
|
202 |
|
|
203 |
for(int face=FRONT; face<=BOTTOM; face++) |
|
204 |
{ |
|
205 |
if( faceIsVisible(face,cubeHalfSize) ) |
|
206 |
{ |
|
207 |
castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace); |
|
208 |
|
|
209 |
float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize); |
|
210 |
float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize); |
|
211 |
float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize); |
|
212 |
|
|
213 |
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face; |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 |
return NONE; |
|
218 |
} |
|
219 |
|
|
220 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
221 |
|
|
222 |
private void addNewRotation(int x, int y) |
|
223 |
{ |
|
224 |
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f; |
|
225 |
|
|
226 |
convertTouchPointToScreenSpace(x,y); |
|
227 |
castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff); |
|
228 |
|
|
229 |
mDiff[0] -= mTouchPointCastOntoFace[0]; |
|
230 |
mDiff[1] -= mTouchPointCastOntoFace[1]; |
|
231 |
mDiff[2] -= mTouchPointCastOntoFace[2]; |
|
232 |
|
|
233 |
int xAxis = retFaceXaxis(mLastTouchedFace); |
|
234 |
int yAxis = retFaceYaxis(mLastTouchedFace); |
|
235 |
mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]); |
|
236 |
float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize); |
|
237 |
|
|
238 |
mTouchPoint[0] = mPoint[0]; |
|
239 |
mTouchPoint[1] = mPoint[1]; |
|
240 |
mTouchPoint[2] = mPoint[2]; |
|
241 |
|
|
242 |
mRenderer.getCube().addNewRotation(mRotationVect,offset); |
|
243 |
} |
|
244 |
|
|
245 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
246 |
|
|
247 |
private boolean isVertical(float x, float y) |
|
248 |
{ |
|
249 |
return (y>x) ? (y>=-x) : (y< -x); |
|
250 |
} |
|
251 |
|
|
252 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
253 |
|
|
254 |
private void continueRotation(int x, int y) |
|
255 |
{ |
|
256 |
convertTouchPointToScreenSpace(x,y); |
|
257 |
|
|
258 |
mDiff[0] = mPoint[0]-mTouchPoint[0]; |
|
259 |
mDiff[1] = mPoint[1]-mTouchPoint[1]; |
|
260 |
mDiff[2] = mPoint[2]-mTouchPoint[2]; |
|
261 |
|
|
262 |
int xAxis= retFaceXaxis(mLastTouchedFace); |
|
263 |
int yAxis= retFaceYaxis(mLastTouchedFace); |
|
264 |
int sign = retFaceRotationSign(mLastTouchedFace); |
|
265 |
float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]); |
|
266 |
|
|
267 |
mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*sign*angle/mScreenMin); |
|
268 |
} |
|
269 |
|
|
270 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
271 |
|
|
272 |
private void finishRotation() |
|
273 |
{ |
|
274 |
mRenderer.finishRotation(); |
|
275 |
} |
|
276 |
|
|
277 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
278 |
// return quat1*quat2 |
|
279 |
|
|
280 |
static Static4D quatMultiply( Static4D quat1, Static4D quat2 ) |
|
281 |
{ |
|
282 |
float qx = quat1.get1(); |
|
283 |
float qy = quat1.get2(); |
|
284 |
float qz = quat1.get3(); |
|
285 |
float qw = quat1.get4(); |
|
286 |
|
|
287 |
float rx = quat2.get1(); |
|
288 |
float ry = quat2.get2(); |
|
289 |
float rz = quat2.get3(); |
|
290 |
float rw = quat2.get4(); |
|
291 |
|
|
292 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
293 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
294 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
295 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
296 |
|
|
297 |
return new Static4D(tx,ty,tz,tw); |
|
298 |
} |
|
299 |
|
|
300 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
301 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat ) |
|
302 |
|
|
303 |
static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat) |
|
304 |
{ |
|
305 |
float qx = quat.get1(); |
|
306 |
float qy = quat.get2(); |
|
307 |
float qz = quat.get3(); |
|
308 |
float qw = quat.get4(); |
|
309 |
|
|
310 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
|
311 |
Static4D tmp = quatMultiply(quatInverted,vector); |
|
312 |
|
|
313 |
return quatMultiply(tmp,quat); |
|
314 |
} |
|
315 |
|
|
316 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
317 |
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) ) |
|
318 |
|
|
319 |
static Static4D rotateVectorByQuat(Static4D vector, Static4D quat) |
|
320 |
{ |
|
321 |
float qx = quat.get1(); |
|
322 |
float qy = quat.get2(); |
|
323 |
float qz = quat.get3(); |
|
324 |
float qw = quat.get4(); |
|
325 |
|
|
326 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
|
327 |
Static4D tmp = quatMultiply(quat,vector); |
|
328 |
|
|
329 |
return quatMultiply(tmp,quatInverted); |
|
330 |
} |
|
331 |
|
|
332 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
333 |
|
|
334 |
private Static4D quatFromDrag(float dragX, float dragY) |
|
335 |
{ |
|
336 |
float axisX = dragY; // inverted X and Y - rotation axis is |
|
337 |
float axisY = dragX; // perpendicular to (dragX,dragY) Why not (-dragY, dragX) ? because Y axis is also inverted! |
|
338 |
float axisZ = 0; |
|
339 |
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); |
|
340 |
|
|
341 |
if( axisL>0 ) |
|
342 |
{ |
|
343 |
axisX /= axisL; |
|
344 |
axisY /= axisL; |
|
345 |
axisZ /= axisL; |
|
346 |
|
|
347 |
float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin); |
|
348 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
349 |
|
|
350 |
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
351 |
} |
|
352 |
|
|
353 |
return new Static4D(0f, 0f, 0f, 1f); |
|
354 |
} |
|
355 |
|
|
356 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
357 |
|
|
358 |
private boolean faceIsVisible(int face, float cubeHalfSize) |
|
359 |
{ |
|
360 |
int sign = retFaceSign(face); |
|
361 |
int zAxis= retFaceZaxis(face); |
|
362 |
|
|
363 |
return sign*mCamera[zAxis] > cubeHalfSize; |
|
364 |
} |
|
365 |
|
|
366 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
367 |
|
|
368 |
private void convertTouchPointToScreenSpace(int x, int y) |
|
369 |
{ |
|
370 |
float halfScrWidth = mScreenWidth *0.5f; |
|
371 |
float halfScrHeight = mScreenHeight*0.5f; |
|
372 |
Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0); |
|
373 |
Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated); |
|
374 |
|
|
375 |
mPoint[0] = rotatedTouchPoint.get1(); |
|
376 |
mPoint[1] = rotatedTouchPoint.get2(); |
|
377 |
mPoint[2] = rotatedTouchPoint.get3(); |
|
378 |
} |
|
379 |
|
|
380 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
381 |
|
|
382 |
private void convertCameraPointToScreenSpace() |
|
383 |
{ |
|
384 |
Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0); |
|
385 |
Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated); |
|
386 |
|
|
387 |
mCamera[0] = rotatedCamera.get1(); |
|
388 |
mCamera[1] = rotatedCamera.get2(); |
|
389 |
mCamera[2] = rotatedCamera.get3(); |
|
390 |
} |
|
391 |
|
|
392 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
393 |
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace, |
|
394 |
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'. |
|
395 |
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize. |
|
396 |
|
|
397 |
private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output) |
|
398 |
{ |
|
399 |
int sign = retFaceSign(face); |
|
400 |
int zAxis= retFaceZaxis(face); |
|
401 |
float diff = mPoint[zAxis]-mCamera[zAxis]; |
|
402 |
|
|
403 |
float ratio = diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f; |
|
404 |
|
|
405 |
output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0]; |
|
406 |
output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1]; |
|
407 |
output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2]; |
|
408 |
} |
|
409 |
|
|
410 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
411 |
|
|
412 |
private int retFaceSign(int face) |
|
413 |
{ |
|
414 |
return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1; |
|
415 |
} |
|
416 |
|
|
417 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
418 |
|
|
419 |
private int retFaceRotationSign(int face) |
|
420 |
{ |
|
421 |
return (face==BACK || face==RIGHT || face==TOP) ? 1:-1; |
|
422 |
} |
|
423 |
|
|
424 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
425 |
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look |
|
426 |
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still |
|
427 |
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX. |
|
428 |
|
|
429 |
private int retFaceXaxis(int face) |
|
430 |
{ |
|
431 |
switch(face) |
|
432 |
{ |
|
433 |
case FRONT : |
|
434 |
case BACK : return VECTX; |
|
435 |
case LEFT : |
|
436 |
case RIGHT : return VECTZ; |
|
437 |
case TOP : |
|
438 |
case BOTTOM: return VECTX; |
|
49 |
setRenderer(new RubikRenderer(this)); |
|
439 | 50 |
} |
440 |
|
|
441 |
return -1; |
|
442 | 51 |
} |
443 | 52 |
|
444 | 53 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
445 | 54 |
|
446 |
private int retFaceYaxis(int face)
|
|
55 |
RubikActivity getRubikActivity()
|
|
447 | 56 |
{ |
448 |
switch(face) |
|
449 |
{ |
|
450 |
case FRONT : |
|
451 |
case BACK : return VECTY; |
|
452 |
case LEFT : |
|
453 |
case RIGHT : return VECTY; |
|
454 |
case TOP : |
|
455 |
case BOTTOM: return VECTZ; |
|
456 |
} |
|
457 |
|
|
458 |
return -1; |
|
459 |
} |
|
460 |
|
|
461 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
462 |
|
|
463 |
private int retFaceZaxis(int face) |
|
464 |
{ |
|
465 |
switch(face) |
|
466 |
{ |
|
467 |
case FRONT : |
|
468 |
case BACK : return VECTZ; |
|
469 |
case LEFT : |
|
470 |
case RIGHT : return VECTX; |
|
471 |
case TOP : |
|
472 |
case BOTTOM: return VECTY; |
|
473 |
} |
|
474 |
|
|
475 |
return -1; |
|
57 |
return mWeakAct.get(); |
|
476 | 58 |
} |
477 | 59 |
} |
478 | 60 |
|
Also available in: Unified diff
Turn the Rubik app into a MemoryTest.