Revision f404152d
Added by Leszek Koltunski 12 months ago
src/main/java/org/distorted/bandaged/BandagedCreatorTouchControl.java | ||
---|---|---|
12 | 12 |
import org.distorted.library.helpers.QuatHelper; |
13 | 13 |
import org.distorted.library.type.Static3D; |
14 | 14 |
import org.distorted.library.type.Static4D; |
15 |
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron; |
|
16 | 15 |
|
17 | 16 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
18 | 17 |
|
19 | 18 |
public class BandagedCreatorTouchControl |
20 | 19 |
{ |
21 |
private static final float DIST2D = 0.5f; |
|
22 |
|
|
20 |
private final BandagedObject mObject; |
|
23 | 21 |
private final Static4D CAMERA_POINT; |
24 |
private final float[] mPoint, mCamera, mTouch, mPos;
|
|
22 |
private final float[] mPoint, mCamera, mTouch; |
|
25 | 23 |
private final float[] mPoint2D; |
26 | 24 |
private float mObjectRatio; |
27 | 25 |
private final Static3D[] mFaceAxis; |
28 |
private final float[] mDist3D; |
|
29 | 26 |
|
30 |
private BandagedCubit[] mCubits; |
|
31 |
private int mNumCubits; |
|
27 |
private float[] mDist3D; |
|
32 | 28 |
private int mLastTouchedFace; |
33 |
private float mX, mY, mZ, mMax; |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
|
|
37 |
private boolean isInsideFace(int face, float[] p) |
|
38 |
{ |
|
39 |
return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D ); |
|
40 |
} |
|
41 | 29 |
|
42 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 | 31 |
// Convert the 3D point3D into a 2D point on the same face surface, but in a different |
... | ... | |
118 | 106 |
} |
119 | 107 |
} |
120 | 108 |
|
121 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
122 |
|
|
123 |
private void stretchPoint(int face, float[] output) |
|
124 |
{ |
|
125 |
switch(face/2) |
|
126 |
{ |
|
127 |
case 0: output[0] *= (mMax/mZ); output[1] *= (mMax/mY); break; |
|
128 |
case 1: output[0] *= (mMax/mX); output[1] *= (mMax/mZ); break; |
|
129 |
case 2: output[0] *= (mMax/mX); output[1] *= (mMax/mY); break; |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 | 109 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
134 | 110 |
|
135 | 111 |
private boolean faceIsVisible(int face) |
... | ... | |
162 | 138 |
float az = mFaceAxis[mLastTouchedFace].get2(); |
163 | 139 |
|
164 | 140 |
convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D); |
165 |
stretchPoint(mLastTouchedFace,mPoint2D); |
|
166 |
if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true; |
|
141 |
mObject.stretchPoint(mLastTouchedFace,mPoint2D);
|
|
142 |
if( mObject.isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
|
|
167 | 143 |
} |
168 | 144 |
} |
169 | 145 |
|
170 | 146 |
return false; |
171 | 147 |
} |
172 | 148 |
|
173 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
174 |
|
|
175 |
private void computePosition(int face, float pointX, float pointY) |
|
176 |
{ |
|
177 |
switch(face) |
|
178 |
{ |
|
179 |
case 0: mPos[0] = (mX-1)/2; |
|
180 |
mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2; |
|
181 |
mPos[2] = (int)(-mZ*pointX-mZ/2)+(mZ-1)/2; |
|
182 |
break; |
|
183 |
case 1: mPos[0] =-(mX-1)/2; |
|
184 |
mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2; |
|
185 |
mPos[2] = (int)(+mZ*pointX+mZ/2)-(mZ-1)/2; |
|
186 |
break; |
|
187 |
case 2: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2; |
|
188 |
mPos[1] = (mY-1)/2; |
|
189 |
mPos[2] = (int)(-mZ*pointY-mZ/2)+(mZ-1)/2; |
|
190 |
break; |
|
191 |
case 3: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2; |
|
192 |
mPos[1] =-(mY-1)/2; |
|
193 |
mPos[2] = (int)(+mZ*pointY+mZ/2)-(mZ-1)/2; |
|
194 |
break; |
|
195 |
case 4: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2; |
|
196 |
mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2; |
|
197 |
mPos[2] = (mZ-1)/2; |
|
198 |
break; |
|
199 |
case 5: mPos[0] = (int)(-mX*pointX-mX/2)+(mX-1)/2; |
|
200 |
mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2; |
|
201 |
mPos[2] =-(mZ-1)/2; |
|
202 |
break; |
|
203 |
} |
|
204 |
} |
|
205 |
|
|
206 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
207 |
|
|
208 |
private int whichCubitTouched(int face, float pointX, float pointY) |
|
209 |
{ |
|
210 |
computePosition(face,pointX,pointY); |
|
211 |
|
|
212 |
for(int c=0; c<mNumCubits; c++) |
|
213 |
if( mCubits[c].isAttached() ) |
|
214 |
{ |
|
215 |
float[] pos = mCubits[c].getPosition(); |
|
216 |
int len = pos.length/3; |
|
217 |
|
|
218 |
for(int p=0; p<len; p++) |
|
219 |
if( pos[3*p]==mPos[0] && pos[3*p+1]==mPos[1] && pos[3*p+2]==mPos[2] ) return c; |
|
220 |
} |
|
221 |
|
|
222 |
android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!"); |
|
223 |
return -1; |
|
224 |
} |
|
225 |
|
|
226 | 149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
227 | 150 |
// PUBLIC API |
228 | 151 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
229 | 152 |
|
230 |
public BandagedCreatorTouchControl(float ratio, float fov) |
|
153 |
public BandagedCreatorTouchControl(float ratio, float fov, BandagedObject object)
|
|
231 | 154 |
{ |
155 |
mObject = object; |
|
232 | 156 |
mPoint = new float[3]; |
233 | 157 |
mCamera= new float[3]; |
234 | 158 |
mTouch = new float[3]; |
235 |
mPos = new float[3]; |
|
236 | 159 |
mPoint2D = new float[2]; |
237 | 160 |
mDist3D = new float[6]; |
238 |
mFaceAxis = TouchControlHexahedron.FACE_AXIS;
|
|
161 |
mFaceAxis = mObject.getFaceAxis();
|
|
239 | 162 |
mObjectRatio = ratio; |
240 | 163 |
|
241 | 164 |
double halfFOV = fov * (Math.PI/360); |
... | ... | |
247 | 170 |
|
248 | 171 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
249 | 172 |
|
250 |
public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
|
|
173 |
public void setDist3D(float[] dist3d)
|
|
251 | 174 |
{ |
252 |
mCubits = cubits; |
|
253 |
mNumCubits = cubits.length; |
|
254 |
|
|
255 |
mX = x; |
|
256 |
mY = y; |
|
257 |
mZ = z; |
|
258 |
mMax = mX>mY ? Math.max(mX,mZ) : Math.max(mY,mZ); |
|
259 |
|
|
260 |
mDist3D[0] = mDist3D[1] = 0.5f*(mX/mMax); |
|
261 |
mDist3D[2] = mDist3D[3] = 0.5f*(mY/mMax); |
|
262 |
mDist3D[4] = mDist3D[5] = 0.5f*(mZ/mMax); |
|
175 |
mDist3D = dist3d; |
|
263 | 176 |
} |
264 | 177 |
|
265 | 178 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
282 | 195 |
|
283 | 196 |
if( !touched ) return -1; |
284 | 197 |
|
285 |
return whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]); |
|
198 |
return mObject.whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
|
|
286 | 199 |
} |
287 | 200 |
} |
288 | 201 |
|
Also available in: Unified diff
Introduce 'BandagedObject' - an abstraction and a step towards creator of Bandaged Pyraminxes.