1 |
beb325a0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.object;
|
21 |
|
|
|
22 |
|
|
import org.distorted.library.type.Static4D;
|
23 |
|
|
import org.distorted.magic.RubikSurfaceView;
|
24 |
|
|
|
25 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
|
27 |
|
|
public class RubikCubeMovement
|
28 |
|
|
{
|
29 |
|
|
public final static int NONE =-1;
|
30 |
|
|
private final static int FRONT = 0; // has to be 6 consecutive ints
|
31 |
|
|
private final static int BACK = 1; // FRONT ... BOTTOM
|
32 |
|
|
private final static int LEFT = 2; //
|
33 |
|
|
private final static int RIGHT = 3; //
|
34 |
|
|
private final static int TOP = 4; //
|
35 |
|
|
private final static int BOTTOM = 5; //
|
36 |
|
|
|
37 |
|
|
private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
|
38 |
|
|
|
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
|
45 |
|
|
private int mRotationVect;
|
46 |
|
|
|
47 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48 |
|
|
|
49 |
|
|
private boolean isVertical(float x, float y)
|
50 |
|
|
{
|
51 |
|
|
return (y>x) ? (y>=-x) : (y< -x);
|
52 |
|
|
}
|
53 |
|
|
|
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 |
c896c0b3
|
Leszek Koltunski
|
private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
|
67 |
beb325a0
|
Leszek Koltunski
|
{
|
68 |
c896c0b3
|
Leszek Koltunski
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
69 |
beb325a0
|
Leszek Koltunski
|
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 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
|
|
|
108 |
|
|
private int retFaceSign(int face)
|
109 |
|
|
{
|
110 |
|
|
return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114 |
|
|
|
115 |
|
|
private int retFaceRotationSign(int face)
|
116 |
|
|
{
|
117 |
|
|
return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121 |
|
|
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
|
122 |
|
|
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
|
123 |
|
|
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
|
124 |
|
|
|
125 |
|
|
private int retFaceXaxis(int face)
|
126 |
|
|
{
|
127 |
|
|
switch(face)
|
128 |
|
|
{
|
129 |
|
|
case FRONT :
|
130 |
|
|
case BACK : return RubikCube.VECTX;
|
131 |
|
|
case LEFT :
|
132 |
|
|
case RIGHT : return RubikCube.VECTZ;
|
133 |
|
|
case TOP :
|
134 |
|
|
case BOTTOM: return RubikCube.VECTX;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
return -1;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141 |
|
|
|
142 |
|
|
private int retFaceYaxis(int face)
|
143 |
|
|
{
|
144 |
|
|
switch(face)
|
145 |
|
|
{
|
146 |
|
|
case FRONT :
|
147 |
|
|
case BACK : return RubikCube.VECTY;
|
148 |
|
|
case LEFT :
|
149 |
|
|
case RIGHT : return RubikCube.VECTY;
|
150 |
|
|
case TOP :
|
151 |
|
|
case BOTTOM: return RubikCube.VECTZ;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
return -1;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
|
|
|
159 |
|
|
private int retFaceZaxis(int face)
|
160 |
|
|
{
|
161 |
|
|
switch(face)
|
162 |
|
|
{
|
163 |
|
|
case FRONT :
|
164 |
|
|
case BACK : return RubikCube.VECTZ;
|
165 |
|
|
case LEFT :
|
166 |
|
|
case RIGHT : return RubikCube.VECTX;
|
167 |
|
|
case TOP :
|
168 |
|
|
case BOTTOM: return RubikCube.VECTY;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
return -1;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
|
|
// PUBLIC API
|
176 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
|
|
178 |
|
|
public RubikCubeMovement(RubikCube cube)
|
179 |
|
|
{
|
180 |
|
|
mCube = cube;
|
181 |
|
|
|
182 |
|
|
mRotationVect = VECT[0];
|
183 |
|
|
|
184 |
|
|
mPoint = new float[3];
|
185 |
|
|
mCamera= new float[3];
|
186 |
|
|
mDiff = new float[3];
|
187 |
|
|
mTouchPoint = new float[3];
|
188 |
|
|
mTouchPointCastOntoFace = new float[3];
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192 |
|
|
|
193 |
c896c0b3
|
Leszek Koltunski
|
public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y)
|
194 |
beb325a0
|
Leszek Koltunski
|
{
|
195 |
|
|
float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
|
196 |
|
|
|
197 |
c896c0b3
|
Leszek Koltunski
|
convertTouchPointToScreenSpace(accumulated,x,y);
|
198 |
beb325a0
|
Leszek Koltunski
|
convertCameraPointToScreenSpace(accumulated, cameraDistance);
|
199 |
|
|
|
200 |
|
|
for(int face=FRONT; face<=BOTTOM; face++)
|
201 |
|
|
{
|
202 |
|
|
if( faceIsVisible(face,cubeHalfSize) )
|
203 |
|
|
{
|
204 |
|
|
castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
|
205 |
|
|
|
206 |
|
|
float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
|
207 |
|
|
float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
|
208 |
|
|
float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
|
209 |
|
|
|
210 |
|
|
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
return NONE;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218 |
|
|
|
219 |
c896c0b3
|
Leszek Koltunski
|
public void addNewRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
|
220 |
beb325a0
|
Leszek Koltunski
|
{
|
221 |
|
|
float cubeHalfSize= mCube.returnCubeSizeInScreenSpace()*0.5f;
|
222 |
|
|
|
223 |
c896c0b3
|
Leszek Koltunski
|
convertTouchPointToScreenSpace(accumulated,x,y);
|
224 |
beb325a0
|
Leszek Koltunski
|
castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff);
|
225 |
|
|
|
226 |
|
|
mDiff[0] -= mTouchPointCastOntoFace[0];
|
227 |
|
|
mDiff[1] -= mTouchPointCastOntoFace[1];
|
228 |
|
|
mDiff[2] -= mTouchPointCastOntoFace[2];
|
229 |
|
|
|
230 |
|
|
int xAxis = retFaceXaxis(lastTouchedFace);
|
231 |
|
|
int yAxis = retFaceYaxis(lastTouchedFace);
|
232 |
|
|
mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
|
233 |
|
|
float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
|
234 |
|
|
|
235 |
|
|
mTouchPoint[0] = mPoint[0];
|
236 |
|
|
mTouchPoint[1] = mPoint[1];
|
237 |
|
|
mTouchPoint[2] = mPoint[2];
|
238 |
|
|
|
239 |
|
|
mCube.addNewRotation(mRotationVect, (int)(mCube.getSize()*offset) );
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243 |
|
|
|
244 |
c896c0b3
|
Leszek Koltunski
|
public void continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y, int scrMin)
|
245 |
beb325a0
|
Leszek Koltunski
|
{
|
246 |
c896c0b3
|
Leszek Koltunski
|
convertTouchPointToScreenSpace(accumulated,x,y);
|
247 |
beb325a0
|
Leszek Koltunski
|
|
248 |
|
|
mDiff[0] = mPoint[0]-mTouchPoint[0];
|
249 |
|
|
mDiff[1] = mPoint[1]-mTouchPoint[1];
|
250 |
|
|
mDiff[2] = mPoint[2]-mTouchPoint[2];
|
251 |
|
|
|
252 |
|
|
int xAxis= retFaceXaxis(lastTouchedFace);
|
253 |
|
|
int yAxis= retFaceYaxis(lastTouchedFace);
|
254 |
|
|
int sign = retFaceRotationSign(lastTouchedFace);
|
255 |
|
|
float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
|
256 |
|
|
|
257 |
|
|
mCube.continueRotation(SWIPING_SENSITIVITY*sign*angle/scrMin);
|
258 |
|
|
}
|
259 |
|
|
}
|