1 |
903041cd
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2022 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
44fec653
|
Leszek Koltunski
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7 |
|
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8 |
903041cd
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
|
|
package org.distorted.bandaged;
|
11 |
|
|
|
12 |
f3b3c087
|
Leszek Koltunski
|
import org.distorted.library.helpers.QuatHelper;
|
13 |
903041cd
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
14 |
|
|
import org.distorted.library.type.Static4D;
|
15 |
|
|
|
16 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
17 |
|
|
|
18 |
ed32e32d
|
Leszek Koltunski
|
public class BandagedCreatorTouchControl
|
19 |
903041cd
|
Leszek Koltunski
|
{
|
20 |
f404152d
|
Leszek Koltunski
|
private final BandagedObject mObject;
|
21 |
903041cd
|
Leszek Koltunski
|
private final Static4D CAMERA_POINT;
|
22 |
f404152d
|
Leszek Koltunski
|
private final float[] mPoint, mCamera, mTouch;
|
23 |
903041cd
|
Leszek Koltunski
|
private final float[] mPoint2D;
|
24 |
|
|
private float mObjectRatio;
|
25 |
|
|
private final Static3D[] mFaceAxis;
|
26 |
|
|
|
27 |
f404152d
|
Leszek Koltunski
|
private float[] mDist3D;
|
28 |
5d5ed376
|
Leszek Koltunski
|
private int mLastTouchedFace;
|
29 |
903041cd
|
Leszek Koltunski
|
|
30 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31 |
|
|
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
|
32 |
|
|
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
|
33 |
|
|
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
|
34 |
|
|
// original 3D Y axis and our 2D in-plane origin.
|
35 |
|
|
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
|
36 |
|
|
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
|
37 |
|
|
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
|
38 |
|
|
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
|
39 |
|
|
// (ax,ay,az) - vector normal to the face surface.
|
40 |
|
|
|
41 |
|
|
void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
|
42 |
|
|
{
|
43 |
|
|
float y0,y1,y2; // base Y vector of the 2D coord system
|
44 |
|
|
|
45 |
|
|
if( ax==0.0f && az==0.0f )
|
46 |
|
|
{
|
47 |
|
|
y0=0; y1=0; y2=-ay;
|
48 |
|
|
}
|
49 |
|
|
else if( ay==0.0f )
|
50 |
|
|
{
|
51 |
|
|
y0=0; y1=1; y2=0;
|
52 |
|
|
}
|
53 |
|
|
else
|
54 |
|
|
{
|
55 |
|
|
float norm = (float)(-ay/Math.sqrt(1-ay*ay));
|
56 |
|
|
y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
float x0 = y1*az - y2*ay; //
|
60 |
|
|
float x1 = y2*ax - y0*az; // (2D coord baseY) x (axis) = 2D coord baseX
|
61 |
|
|
float x2 = y0*ay - y1*ax; //
|
62 |
|
|
|
63 |
|
|
float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
|
64 |
|
|
|
65 |
|
|
float origin0 = originAlpha*ax; // coords of the point where axis
|
66 |
|
|
float origin1 = originAlpha*ay; // intersects surface plane i.e.
|
67 |
|
|
float origin2 = originAlpha*az; // the origin of our 2D coord system
|
68 |
|
|
|
69 |
|
|
float v0 = point3D[0] - origin0;
|
70 |
|
|
float v1 = point3D[1] - origin1;
|
71 |
|
|
float v2 = point3D[2] - origin2;
|
72 |
|
|
|
73 |
|
|
output[0] = v0*x0 + v1*x1 + v2*x2;
|
74 |
|
|
output[1] = v0*y0 + v1*y1 + v2*y2;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78 |
|
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
79 |
|
|
// compute point 'output[]' which:
|
80 |
|
|
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
|
81 |
|
|
// 2) is co-linear with mCamera and mPoint
|
82 |
|
|
//
|
83 |
|
|
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
|
84 |
|
|
|
85 |
213c15de
|
Leszek Koltunski
|
private void castTouchPointOntoFace(int face, float[] output)
|
86 |
903041cd
|
Leszek Koltunski
|
{
|
87 |
213c15de
|
Leszek Koltunski
|
Static3D faceAxis = mFaceAxis[face];
|
88 |
903041cd
|
Leszek Koltunski
|
|
89 |
|
|
float d0 = mPoint[0]-mCamera[0];
|
90 |
|
|
float d1 = mPoint[1]-mCamera[1];
|
91 |
|
|
float d2 = mPoint[2]-mCamera[2];
|
92 |
|
|
float a0 = faceAxis.get0();
|
93 |
|
|
float a1 = faceAxis.get1();
|
94 |
|
|
float a2 = faceAxis.get2();
|
95 |
|
|
|
96 |
|
|
float denom = a0*d0 + a1*d1 + a2*d2;
|
97 |
|
|
|
98 |
|
|
if( denom != 0.0f )
|
99 |
|
|
{
|
100 |
|
|
float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
|
101 |
213c15de
|
Leszek Koltunski
|
float alpha = (mDist3D[face]-axisCam)/denom;
|
102 |
903041cd
|
Leszek Koltunski
|
|
103 |
|
|
output[0] = mCamera[0] + d0*alpha;
|
104 |
|
|
output[1] = mCamera[1] + d1*alpha;
|
105 |
|
|
output[2] = mCamera[2] + d2*alpha;
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
213c15de
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110 |
|
|
|
111 |
|
|
private boolean faceIsVisible(int face)
|
112 |
|
|
{
|
113 |
|
|
Static3D faceAxis = mFaceAxis[face];
|
114 |
903041cd
|
Leszek Koltunski
|
float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
|
115 |
213c15de
|
Leszek Koltunski
|
return castCameraOnAxis > mDist3D[face];
|
116 |
903041cd
|
Leszek Koltunski
|
}
|
117 |
|
|
|
118 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119 |
|
|
|
120 |
|
|
private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
|
121 |
|
|
{
|
122 |
|
|
mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
123 |
|
|
mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
124 |
|
|
mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
125 |
|
|
|
126 |
|
|
mCamera[0] = rotatedCamera.get0()/mObjectRatio;
|
127 |
|
|
mCamera[1] = rotatedCamera.get1()/mObjectRatio;
|
128 |
|
|
mCamera[2] = rotatedCamera.get2()/mObjectRatio;
|
129 |
|
|
|
130 |
|
|
for( mLastTouchedFace=0; mLastTouchedFace<6; mLastTouchedFace++)
|
131 |
|
|
{
|
132 |
|
|
if( faceIsVisible(mLastTouchedFace) )
|
133 |
|
|
{
|
134 |
|
|
castTouchPointOntoFace(mLastTouchedFace, mTouch);
|
135 |
|
|
|
136 |
|
|
float ax = mFaceAxis[mLastTouchedFace].get0();
|
137 |
|
|
float ay = mFaceAxis[mLastTouchedFace].get1();
|
138 |
|
|
float az = mFaceAxis[mLastTouchedFace].get2();
|
139 |
|
|
|
140 |
|
|
convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
|
141 |
f404152d
|
Leszek Koltunski
|
mObject.stretchPoint(mLastTouchedFace,mPoint2D);
|
142 |
|
|
if( mObject.isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
|
143 |
903041cd
|
Leszek Koltunski
|
}
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
return false;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150 |
|
|
// PUBLIC API
|
151 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
|
|
|
153 |
f404152d
|
Leszek Koltunski
|
public BandagedCreatorTouchControl(float ratio, float fov, BandagedObject object)
|
154 |
903041cd
|
Leszek Koltunski
|
{
|
155 |
f404152d
|
Leszek Koltunski
|
mObject = object;
|
156 |
903041cd
|
Leszek Koltunski
|
mPoint = new float[3];
|
157 |
|
|
mCamera= new float[3];
|
158 |
|
|
mTouch = new float[3];
|
159 |
|
|
mPoint2D = new float[2];
|
160 |
213c15de
|
Leszek Koltunski
|
mDist3D = new float[6];
|
161 |
f404152d
|
Leszek Koltunski
|
mFaceAxis = mObject.getFaceAxis();
|
162 |
903041cd
|
Leszek Koltunski
|
mObjectRatio = ratio;
|
163 |
|
|
|
164 |
|
|
double halfFOV = fov * (Math.PI/360);
|
165 |
|
|
float tanHalf = (float)Math.tan(halfFOV);
|
166 |
|
|
float dist = 0.5f/tanHalf;
|
167 |
|
|
|
168 |
|
|
CAMERA_POINT = new Static4D(0,0,dist,0);
|
169 |
|
|
}
|
170 |
5d5ed376
|
Leszek Koltunski
|
|
171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172 |
|
|
|
173 |
f404152d
|
Leszek Koltunski
|
public void setDist3D(float[] dist3d)
|
174 |
5d5ed376
|
Leszek Koltunski
|
{
|
175 |
f404152d
|
Leszek Koltunski
|
mDist3D = dist3d;
|
176 |
5d5ed376
|
Leszek Koltunski
|
}
|
177 |
903041cd
|
Leszek Koltunski
|
|
178 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179 |
|
|
|
180 |
|
|
public void setObjectRatio(float ratio)
|
181 |
|
|
{
|
182 |
|
|
mObjectRatio = ratio;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186 |
|
|
// return the index of the cubit touched; if none, return -1.
|
187 |
|
|
|
188 |
|
|
public int cubitTouched(float x, float y, Static4D quat)
|
189 |
|
|
{
|
190 |
|
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
191 |
|
|
Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
|
192 |
|
|
Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
|
193 |
|
|
|
194 |
|
|
boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
|
195 |
|
|
|
196 |
|
|
if( !touched ) return -1;
|
197 |
|
|
|
198 |
f404152d
|
Leszek Koltunski
|
return mObject.whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
|
199 |
903041cd
|
Leszek Koltunski
|
}
|
200 |
|
|
}
|