1 |
ab31cf6f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
71f8a172
|
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 |
ab31cf6f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
c9c71c3f
|
Leszek Koltunski
|
package org.distorted.objectlib.touchcontrol;
|
11 |
ab31cf6f
|
Leszek Koltunski
|
|
12 |
c9c71c3f
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
13 |
ab31cf6f
|
Leszek Koltunski
|
|
14 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
15 |
|
|
|
16 |
c9c71c3f
|
Leszek Koltunski
|
public abstract class TouchControl
|
17 |
|
|
{
|
18 |
|
|
// it doesn't matter where we touch a face - the list of enabled rotAxis will always be the same
|
19 |
|
|
public static final int TYPE_NOT_SPLIT = 0;
|
20 |
|
|
// each face is split into several parts by lines coming from its center to the midpoints of each edge
|
21 |
|
|
public static final int TYPE_SPLIT_EDGE = 1;
|
22 |
|
|
// each face is split into several parts by lines coming from its center to the vertices
|
23 |
|
|
public static final int TYPE_SPLIT_CORNER = 2;
|
24 |
|
|
|
25 |
cd2e8d4c
|
Leszek Koltunski
|
public static final int TC_HEXAHEDRON = 6;
|
26 |
|
|
public static final int TC_TETRAHEDRON = 4;
|
27 |
|
|
public static final int TC_OCTAHEDRON = 8;
|
28 |
|
|
public static final int TC_DODECAHEDRON = 12;
|
29 |
|
|
public static final int TC_CUBOID = 0;
|
30 |
|
|
public static final int TC_CHANGING_MIRROR = 100;
|
31 |
|
|
public static final int TC_CHANGING_SQUARE = 101;
|
32 |
|
|
public static final int TC_CHANGING_SHAPEMOD = 102;
|
33 |
c9c71c3f
|
Leszek Koltunski
|
|
34 |
57ef6378
|
Leszek Koltunski
|
float mObjectRatio;
|
35 |
|
|
|
36 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
11fa413d
|
Leszek Koltunski
|
public TouchControl(float ratio)
|
39 |
57ef6378
|
Leszek Koltunski
|
{
|
40 |
11fa413d
|
Leszek Koltunski
|
mObjectRatio = ratio;
|
41 |
57ef6378
|
Leszek Koltunski
|
}
|
42 |
|
|
|
43 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
|
45 |
|
|
public void setObjectRatio(float ratio)
|
46 |
|
|
{
|
47 |
|
|
mObjectRatio = ratio;
|
48 |
|
|
}
|
49 |
|
|
|
50 |
0c5d8bf7
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
|
|
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
|
99 |
|
|
|
100 |
|
|
int computeRotationIndex(float[][] rotAxis, float[] move2D, int[] enabled)
|
101 |
|
|
{
|
102 |
|
|
float cosAngle, minCosAngle = Float.MAX_VALUE;
|
103 |
|
|
int minIndex=0, index;
|
104 |
|
|
float m0 = move2D[0];
|
105 |
|
|
float m1 = move2D[1];
|
106 |
|
|
int numAxis = enabled[0];
|
107 |
|
|
|
108 |
|
|
for(int axis=1; axis<=numAxis; axis++)
|
109 |
|
|
{
|
110 |
|
|
index = enabled[axis];
|
111 |
|
|
cosAngle = m0*rotAxis[index][0] + m1*rotAxis[index][1];
|
112 |
|
|
if( cosAngle<0 ) cosAngle = -cosAngle;
|
113 |
|
|
|
114 |
|
|
if( cosAngle<minCosAngle )
|
115 |
|
|
{
|
116 |
|
|
minCosAngle=cosAngle;
|
117 |
|
|
minIndex = index;
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
return minIndex;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
57ef6378
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
|
126 |
|
|
public abstract boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera);
|
127 |
cd2e8d4c
|
Leszek Koltunski
|
public abstract void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat);
|
128 |
57ef6378
|
Leszek Koltunski
|
public abstract void getCastedRotAxis(float[] output, Static4D quat, int rotIndex);
|
129 |
11fa413d
|
Leszek Koltunski
|
public abstract int getTouchedCubitFace();
|
130 |
|
|
public abstract int getTouchedCubit();
|
131 |
c9c71c3f
|
Leszek Koltunski
|
public abstract float returnRotationFactor(int[] numLayers, int row);
|
132 |
|
|
}
|