1 |
27a70eae
|
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 |
efef689c
|
Leszek Koltunski
|
import org.distorted.library.type.Static2D;
|
23 |
37a25788
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
24 |
27a70eae
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
25 |
|
|
|
26 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27 |
|
|
|
28 |
|
|
public abstract class RubikObjectMovement
|
29 |
|
|
{
|
30 |
dd65ead3
|
Leszek Koltunski
|
int mRotationVect, mLastTouchedAxis;
|
31 |
|
|
|
32 |
9f1f377d
|
Leszek Koltunski
|
private float[] mPoint, mCamera, mDiff, mTouch, m2Dpoint;
|
33 |
dd65ead3
|
Leszek Koltunski
|
private int mLastTouchedLR;
|
34 |
|
|
private int mNumAxis, mNumFacesPerAxis;
|
35 |
|
|
private int[] mPossible;
|
36 |
37a25788
|
Leszek Koltunski
|
private float mDistanceCenterFace;
|
37 |
|
|
private Static3D[] mAxis;
|
38 |
dd65ead3
|
Leszek Koltunski
|
|
39 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40 |
|
|
|
41 |
|
|
abstract boolean isInsideFace(float[] point);
|
42 |
9f1f377d
|
Leszek Koltunski
|
abstract float fillUpRotationVectAndOffset(float[] vect, float[] touch, int[] possible);
|
43 |
dd65ead3
|
Leszek Koltunski
|
abstract float returnAngle(float[] vect, int[] possible);
|
44 |
37a25788
|
Leszek Koltunski
|
abstract void fillPossibleRotations(int axis, int[] output);
|
45 |
dd65ead3
|
Leszek Koltunski
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
37a25788
|
Leszek Koltunski
|
RubikObjectMovement(Static3D[] axis, int numFacesPerAxis, float distance)
|
49 |
dd65ead3
|
Leszek Koltunski
|
{
|
50 |
|
|
mPoint = new float[3];
|
51 |
|
|
mCamera= new float[3];
|
52 |
|
|
mDiff = new float[3];
|
53 |
|
|
mTouch = new float[3];
|
54 |
|
|
|
55 |
bef47287
|
Leszek Koltunski
|
m2Dpoint = new float[2];
|
56 |
|
|
|
57 |
37a25788
|
Leszek Koltunski
|
mAxis = axis;
|
58 |
|
|
mNumAxis = mAxis.length;
|
59 |
dd65ead3
|
Leszek Koltunski
|
mNumFacesPerAxis = numFacesPerAxis;
|
60 |
37a25788
|
Leszek Koltunski
|
mDistanceCenterFace = distance;
|
61 |
dd65ead3
|
Leszek Koltunski
|
mPossible = new int[mNumAxis-1];
|
62 |
|
|
}
|
63 |
|
|
|
64 |
37a25788
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65 |
|
|
|
66 |
|
|
private boolean faceIsVisible(Static3D axis, int lr)
|
67 |
|
|
{
|
68 |
|
|
float castCameraOnAxis = mCamera[0]*axis.get0() + mCamera[1]*axis.get1() + mCamera[2]*axis.get2();
|
69 |
|
|
return (2*lr-1)*castCameraOnAxis > mDistanceCenterFace;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73 |
|
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
74 |
|
|
// compute point 'output[]' which:
|
75 |
|
|
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0)) [and this
|
76 |
|
|
// distance is +-mDistanceCenterFace, depending if it is the face on the left or the right end of
|
77 |
|
|
// the axis] (lr=0 or 1, so (2lr-1)*mDistanceCenterFace)
|
78 |
|
|
// 2) is co-linear with mCamera and mPoint
|
79 |
|
|
//
|
80 |
|
|
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
|
81 |
|
|
|
82 |
|
|
private void castTouchPointOntoFace(Static3D axis, int lr, float[] output)
|
83 |
|
|
{
|
84 |
|
|
float d0 = mPoint[0]-mCamera[0];
|
85 |
|
|
float d1 = mPoint[1]-mCamera[1];
|
86 |
|
|
float d2 = mPoint[2]-mCamera[2];
|
87 |
|
|
float a0 = axis.get0();
|
88 |
|
|
float a1 = axis.get1();
|
89 |
|
|
float a2 = axis.get2();
|
90 |
|
|
|
91 |
|
|
float denom = a0*d0 + a1*d1 + a2*d2;
|
92 |
|
|
|
93 |
|
|
if( denom != 0.0f )
|
94 |
|
|
{
|
95 |
|
|
float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
|
96 |
|
|
float distance = (2*lr-1)*mDistanceCenterFace;
|
97 |
|
|
float alpha = (distance-axisCam)/denom;
|
98 |
|
|
|
99 |
|
|
output[0] = mCamera[0] + d0*alpha;
|
100 |
|
|
output[1] = mCamera[1] + d1*alpha;
|
101 |
|
|
output[2] = mCamera[2] + d2*alpha;
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
bef47287
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
|
107 |
|
|
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
|
108 |
|
|
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
|
109 |
|
|
// original 3D Y axis and our 2D in-plane origin.
|
110 |
|
|
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
|
111 |
|
|
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
|
112 |
|
|
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
|
113 |
|
|
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
|
114 |
|
|
|
115 |
|
|
private void convertTo2Dcoords(float[] point3D, Static3D axis, int lr, float[] output)
|
116 |
|
|
{
|
117 |
|
|
float y0,y1,y2, x0,x1,x2; // base X and Y vectors of the 2D coord system
|
118 |
|
|
float a0 = axis.get0();
|
119 |
|
|
float a1 = axis.get1();
|
120 |
|
|
float a2 = axis.get2();
|
121 |
|
|
|
122 |
|
|
if( lr==0 )
|
123 |
|
|
{
|
124 |
|
|
a0=-a0; a1=-a1; a2=-a2;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
if( a0==0.0f && a2==0.0f )
|
128 |
|
|
{
|
129 |
|
|
y0=0; y1=0; y2=-a1;
|
130 |
|
|
}
|
131 |
|
|
else if( a1==0.0f )
|
132 |
|
|
{
|
133 |
|
|
y0=0; y1=1; y2=0;
|
134 |
|
|
}
|
135 |
|
|
else
|
136 |
|
|
{
|
137 |
|
|
float norm = (float)(-a1/Math.sqrt(1-a1*a1));
|
138 |
8e2295ad
|
Leszek Koltunski
|
y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
|
139 |
bef47287
|
Leszek Koltunski
|
}
|
140 |
|
|
|
141 |
|
|
x0 = y1*a2 - y2*a1; //
|
142 |
|
|
x1 = y2*a0 - y0*a2; // (2D coord baseY) x (axis) = 2D coord baseX
|
143 |
|
|
x2 = y0*a1 - y1*a0; //
|
144 |
|
|
|
145 |
|
|
float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
|
146 |
|
|
|
147 |
|
|
float origin0 = originAlpha*a0; // coords of the point where axis
|
148 |
|
|
float origin1 = originAlpha*a1; // intersects surface plane i.e.
|
149 |
|
|
float origin2 = originAlpha*a2; // the origin of our 2D coord system
|
150 |
|
|
|
151 |
|
|
float v0 = point3D[0] - origin0;
|
152 |
|
|
float v1 = point3D[1] - origin1;
|
153 |
|
|
float v2 = point3D[2] - origin2;
|
154 |
|
|
|
155 |
|
|
output[0] = v0*x0 + v1*x1 + v2*x2;
|
156 |
|
|
output[1] = v0*y0 + v1*y1 + v2*y2;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
8e2295ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160 |
|
|
|
161 |
|
|
private String getFaceColor(int axis)
|
162 |
|
|
{
|
163 |
|
|
switch(axis)
|
164 |
|
|
{
|
165 |
|
|
case 0: return "yellow ";
|
166 |
|
|
case 1: return "green ";
|
167 |
|
|
case 2: return "blue ";
|
168 |
|
|
case 3: return "red ";
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
return null;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
dd65ead3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
|
|
// PUBLIC API
|
176 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
|
|
178 |
|
|
public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
|
179 |
|
|
{
|
180 |
|
|
mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
|
181 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
|
182 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
|
183 |
|
|
|
184 |
|
|
mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
|
185 |
|
|
mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
|
186 |
|
|
mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
|
187 |
|
|
|
188 |
|
|
for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
|
189 |
|
|
{
|
190 |
|
|
for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
|
191 |
|
|
{
|
192 |
37a25788
|
Leszek Koltunski
|
if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) )
|
193 |
dd65ead3
|
Leszek Koltunski
|
{
|
194 |
37a25788
|
Leszek Koltunski
|
castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
|
195 |
bef47287
|
Leszek Koltunski
|
convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, m2Dpoint);
|
196 |
dd65ead3
|
Leszek Koltunski
|
|
197 |
bef47287
|
Leszek Koltunski
|
if( isInsideFace(m2Dpoint) )
|
198 |
dd65ead3
|
Leszek Koltunski
|
{
|
199 |
8e2295ad
|
Leszek Koltunski
|
android.util.Log.e("move", "face "+getFaceColor(mLastTouchedAxis)+" ("+m2Dpoint[0]+","+m2Dpoint[1]+")");
|
200 |
bef47287
|
Leszek Koltunski
|
|
201 |
dd65ead3
|
Leszek Koltunski
|
fillPossibleRotations(mLastTouchedAxis, mPossible);
|
202 |
|
|
return true;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
return false;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212 |
|
|
|
213 |
|
|
public Static2D newRotation(Static4D rotatedTouchPoint)
|
214 |
|
|
{
|
215 |
|
|
mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
|
216 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
|
217 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
|
218 |
|
|
|
219 |
37a25788
|
Leszek Koltunski
|
castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mDiff);
|
220 |
dd65ead3
|
Leszek Koltunski
|
|
221 |
|
|
mDiff[0] -= mTouch[0];
|
222 |
|
|
mDiff[1] -= mTouch[1];
|
223 |
|
|
mDiff[2] -= mTouch[2];
|
224 |
|
|
|
225 |
9f1f377d
|
Leszek Koltunski
|
float offset = fillUpRotationVectAndOffset(mDiff, mTouch, mPossible);
|
226 |
dd65ead3
|
Leszek Koltunski
|
return new Static2D(mRotationVect,offset);
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230 |
|
|
|
231 |
|
|
public float continueRotation(Static4D rotatedTouchPoint)
|
232 |
|
|
{
|
233 |
bef47287
|
Leszek Koltunski
|
mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[0];
|
234 |
|
|
mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[1];
|
235 |
|
|
mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[2];
|
236 |
dd65ead3
|
Leszek Koltunski
|
|
237 |
|
|
return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible);
|
238 |
|
|
}
|
239 |
27a70eae
|
Leszek Koltunski
|
}
|