1 |
903041cd
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2022 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.bandaged;
|
21 |
|
|
|
22 |
|
|
import org.distorted.library.main.QuatHelper;
|
23 |
|
|
import org.distorted.library.type.Static3D;
|
24 |
|
|
import org.distorted.library.type.Static4D;
|
25 |
|
|
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
|
26 |
|
|
|
27 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
|
|
|
29 |
ed32e32d
|
Leszek Koltunski
|
public class BandagedCreatorTouchControl
|
30 |
903041cd
|
Leszek Koltunski
|
{
|
31 |
|
|
private static final float DIST3D = 0.5f;
|
32 |
|
|
private static final float DIST2D = 0.5f;
|
33 |
|
|
|
34 |
|
|
private final Static4D CAMERA_POINT;
|
35 |
|
|
private final BandagedCubit[] mCubits;
|
36 |
|
|
private final int mNumCubits;
|
37 |
|
|
private final float[] mPoint, mCamera, mTouch, mPos;
|
38 |
|
|
private final float[] mPoint2D;
|
39 |
|
|
private float mObjectRatio;
|
40 |
|
|
private int mLastTouchedFace;
|
41 |
|
|
private final Static3D[] mFaceAxis;
|
42 |
|
|
|
43 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
|
45 |
|
|
private boolean isInsideFace(float[] p)
|
46 |
|
|
{
|
47 |
|
|
return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51 |
|
|
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
|
52 |
|
|
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
|
53 |
|
|
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
|
54 |
|
|
// original 3D Y axis and our 2D in-plane origin.
|
55 |
|
|
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
|
56 |
|
|
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
|
57 |
|
|
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
|
58 |
|
|
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
|
59 |
|
|
// (ax,ay,az) - vector normal to the face surface.
|
60 |
|
|
|
61 |
|
|
void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
|
62 |
|
|
{
|
63 |
|
|
float y0,y1,y2; // base Y vector of the 2D coord system
|
64 |
|
|
|
65 |
|
|
if( ax==0.0f && az==0.0f )
|
66 |
|
|
{
|
67 |
|
|
y0=0; y1=0; y2=-ay;
|
68 |
|
|
}
|
69 |
|
|
else if( ay==0.0f )
|
70 |
|
|
{
|
71 |
|
|
y0=0; y1=1; y2=0;
|
72 |
|
|
}
|
73 |
|
|
else
|
74 |
|
|
{
|
75 |
|
|
float norm = (float)(-ay/Math.sqrt(1-ay*ay));
|
76 |
|
|
y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
float x0 = y1*az - y2*ay; //
|
80 |
|
|
float x1 = y2*ax - y0*az; // (2D coord baseY) x (axis) = 2D coord baseX
|
81 |
|
|
float x2 = y0*ay - y1*ax; //
|
82 |
|
|
|
83 |
|
|
float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
|
84 |
|
|
|
85 |
|
|
float origin0 = originAlpha*ax; // coords of the point where axis
|
86 |
|
|
float origin1 = originAlpha*ay; // intersects surface plane i.e.
|
87 |
|
|
float origin2 = originAlpha*az; // the origin of our 2D coord system
|
88 |
|
|
|
89 |
|
|
float v0 = point3D[0] - origin0;
|
90 |
|
|
float v1 = point3D[1] - origin1;
|
91 |
|
|
float v2 = point3D[2] - origin2;
|
92 |
|
|
|
93 |
|
|
output[0] = v0*x0 + v1*x1 + v2*x2;
|
94 |
|
|
output[1] = v0*y0 + v1*y1 + v2*y2;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98 |
|
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
99 |
|
|
// compute point 'output[]' which:
|
100 |
|
|
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
|
101 |
|
|
// 2) is co-linear with mCamera and mPoint
|
102 |
|
|
//
|
103 |
|
|
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
|
104 |
|
|
|
105 |
|
|
private void castTouchPointOntoFace(int index, float[] output)
|
106 |
|
|
{
|
107 |
|
|
Static3D faceAxis = mFaceAxis[index];
|
108 |
|
|
|
109 |
|
|
float d0 = mPoint[0]-mCamera[0];
|
110 |
|
|
float d1 = mPoint[1]-mCamera[1];
|
111 |
|
|
float d2 = mPoint[2]-mCamera[2];
|
112 |
|
|
float a0 = faceAxis.get0();
|
113 |
|
|
float a1 = faceAxis.get1();
|
114 |
|
|
float a2 = faceAxis.get2();
|
115 |
|
|
|
116 |
|
|
float denom = a0*d0 + a1*d1 + a2*d2;
|
117 |
|
|
|
118 |
|
|
if( denom != 0.0f )
|
119 |
|
|
{
|
120 |
|
|
float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
|
121 |
|
|
float alpha = (DIST3D-axisCam)/denom;
|
122 |
|
|
|
123 |
|
|
output[0] = mCamera[0] + d0*alpha;
|
124 |
|
|
output[1] = mCamera[1] + d1*alpha;
|
125 |
|
|
output[2] = mCamera[2] + d2*alpha;
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130 |
|
|
|
131 |
|
|
private boolean faceIsVisible(int index)
|
132 |
|
|
{
|
133 |
|
|
Static3D faceAxis = mFaceAxis[index];
|
134 |
|
|
float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
|
135 |
|
|
return castCameraOnAxis > DIST3D;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139 |
|
|
|
140 |
|
|
private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
|
141 |
|
|
{
|
142 |
|
|
mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
143 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
144 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
145 |
|
|
|
146 |
|
|
mCamera[0] = rotatedCamera.get0()/mObjectRatio;
|
147 |
|
|
mCamera[1] = rotatedCamera.get1()/mObjectRatio;
|
148 |
|
|
mCamera[2] = rotatedCamera.get2()/mObjectRatio;
|
149 |
|
|
|
150 |
|
|
for( mLastTouchedFace=0; mLastTouchedFace<6; mLastTouchedFace++)
|
151 |
|
|
{
|
152 |
|
|
if( faceIsVisible(mLastTouchedFace) )
|
153 |
|
|
{
|
154 |
|
|
castTouchPointOntoFace(mLastTouchedFace, mTouch);
|
155 |
|
|
|
156 |
|
|
float ax = mFaceAxis[mLastTouchedFace].get0();
|
157 |
|
|
float ay = mFaceAxis[mLastTouchedFace].get1();
|
158 |
|
|
float az = mFaceAxis[mLastTouchedFace].get2();
|
159 |
|
|
|
160 |
|
|
convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
|
161 |
|
|
if( isInsideFace(mPoint2D) ) return true;
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
return false;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
169 |
|
|
|
170 |
|
|
private void computePosition(int face, float pointX, float pointY)
|
171 |
|
|
{
|
172 |
|
|
int x = (int)(3*pointX+1.5f) -1;
|
173 |
|
|
int y = (int)(3*pointY+1.5f) -1;
|
174 |
|
|
|
175 |
|
|
switch(face)
|
176 |
|
|
{
|
177 |
|
|
case 0: mPos[0] = 1.0f; mPos[1] = y; mPos[2] = -x; break;
|
178 |
|
|
case 1: mPos[0] =-1.0f; mPos[1] = y; mPos[2] = x; break;
|
179 |
|
|
case 2: mPos[0] = x; mPos[1] = 1.0f; mPos[2] = -y; break;
|
180 |
|
|
case 3: mPos[0] = x; mPos[1] =-1.0f; mPos[2] = y; break;
|
181 |
|
|
case 4: mPos[0] = x; mPos[1] = y; mPos[2] = 1.0f; break;
|
182 |
|
|
case 5: mPos[0] = -x; mPos[1] = y; mPos[2] =-1.0f; break;
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187 |
|
|
|
188 |
|
|
private int whichCubitTouched(int face, float pointX, float pointY)
|
189 |
|
|
{
|
190 |
|
|
computePosition(face,pointX,pointY);
|
191 |
|
|
|
192 |
|
|
for(int c=0; c<mNumCubits; c++)
|
193 |
|
|
if( mCubits[c].isAttached() )
|
194 |
|
|
{
|
195 |
|
|
float[] pos = mCubits[c].getPosition();
|
196 |
|
|
int len = pos.length/3;
|
197 |
|
|
|
198 |
|
|
for(int p=0; p<len; p++)
|
199 |
|
|
if( pos[3*p]==mPos[0] && pos[3*p+1]==mPos[1] && pos[3*p+2]==mPos[2] ) return c;
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
|
203 |
|
|
return -1;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207 |
|
|
// PUBLIC API
|
208 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209 |
|
|
|
210 |
ed32e32d
|
Leszek Koltunski
|
public BandagedCreatorTouchControl(BandagedCubit[] cubits, float ratio, float fov)
|
211 |
903041cd
|
Leszek Koltunski
|
{
|
212 |
|
|
mCubits = cubits;
|
213 |
|
|
mNumCubits = cubits.length;
|
214 |
|
|
mPoint = new float[3];
|
215 |
|
|
mCamera= new float[3];
|
216 |
|
|
mTouch = new float[3];
|
217 |
|
|
mPos = new float[3];
|
218 |
|
|
mPoint2D = new float[2];
|
219 |
|
|
mFaceAxis = TouchControlHexahedron.FACE_AXIS;
|
220 |
|
|
mObjectRatio = ratio;
|
221 |
|
|
|
222 |
|
|
double halfFOV = fov * (Math.PI/360);
|
223 |
|
|
float tanHalf = (float)Math.tan(halfFOV);
|
224 |
|
|
float dist = 0.5f/tanHalf;
|
225 |
|
|
|
226 |
|
|
CAMERA_POINT = new Static4D(0,0,dist,0);
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230 |
|
|
|
231 |
|
|
public void setObjectRatio(float ratio)
|
232 |
|
|
{
|
233 |
|
|
mObjectRatio = ratio;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237 |
|
|
// return the index of the cubit touched; if none, return -1.
|
238 |
|
|
|
239 |
|
|
public int cubitTouched(float x, float y, Static4D quat)
|
240 |
|
|
{
|
241 |
|
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
242 |
|
|
Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
|
243 |
|
|
Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
|
244 |
|
|
|
245 |
|
|
boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
|
246 |
|
|
|
247 |
|
|
if( !touched ) return -1;
|
248 |
|
|
|
249 |
|
|
return whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253 |
|
|
|
254 |
|
|
public void markCubit(int index, int color)
|
255 |
|
|
{
|
256 |
|
|
mCubits[index].setTexture(color);
|
257 |
|
|
}
|
258 |
|
|
}
|