Project

General

Profile

Download (7.95 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControl.java @ 09d5cf2b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.touchcontrol;
11

    
12
import org.distorted.library.type.Static4D;
13
import org.distorted.objectlib.main.TwistyObject;
14

    
15
///////////////////////////////////////////////////////////////////////////////////////////////////
16

    
17
public abstract class TouchControl
18
  {
19
  // it doesn't matter where we touch a face - the list of enabled rotAxis will always be the same
20
  public static final int TYPE_NOT_SPLIT      = 0;
21
  // each face is split into several parts by lines coming from its center to the midpoints of each edge
22
  public static final int TYPE_SPLIT_EDGE     = 1;
23
  // each face is split into several parts by lines coming from its center to the vertices
24
  public static final int TYPE_SPLIT_CORNER   = 2;
25
  // each face is split into several parts by lines coming from its center to the midpoints of each edge,
26
  // and also it has an inscribed circle (see coin tetrahedron!)
27
  public static final int TYPE_SPLIT_EDGE_COIN= 3;
28

    
29
  static final float D_TRIANGLE = 0.95f;
30
  static final float D_SQUARE   = 0.75f;
31
  static final float D_PENTA    = 0.65f;
32

    
33
  public static final int TC_HEXAHEDRON        =   6;
34
  public static final int TC_TETRAHEDRON       =   4;
35
  public static final int TC_OCTAHEDRON        =   8;
36
  public static final int TC_DODECAHEDRON      =  12;
37
  public static final int TC_ICOSAHEDRON       =  20;
38
  public static final int TC_CUBOID            =   0;
39
  public static final int TC_BALL              =   1;
40
  public static final int TC_BARREL            =   2;
41
  public static final int TC_CHANGING_MIRROR   = 100;
42
  public static final int TC_CHANGING_SQUARE   = 101;
43
  public static final int TC_CHANGING_SHAPEMOD = 102;
44

    
45
  float mObjectRatio;
46
  int mGhostAxisEnabled;
47
  float[][] mTouchBorders;
48

    
49
  private final float[][] mRotationFactor;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public TouchControl(TwistyObject object)
54
    {
55
    mObjectRatio = (object!=null ? object.getObjectRatio() : 1.0f);
56
    mRotationFactor = (object!=null ? object.returnRotationFactor() : null);
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public void setObjectRatio(float ratio)
62
    {
63
    mObjectRatio = ratio;
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
68
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
69
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
70
// original 3D Y axis and our 2D in-plane origin.
71
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
72
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
73
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
74
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
75
// (ax,ay,az) - vector normal to the face surface.
76

    
77
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
78
    {
79
    float y0,y1,y2; // base Y vector of the 2D coord system
80

    
81
    if( ax==0.0f && az==0.0f )
82
      {
83
      y0=0; y1=0; y2=-ay;
84
      }
85
    else if( ay==0.0f )
86
      {
87
      y0=0; y1=1; y2=0;
88
      }
89
    else
90
      {
91
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
92
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
93
      }
94

    
95
    float x0 = y1*az - y2*ay;  //
96
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
97
    float x2 = y0*ay - y1*ax;  //
98

    
99
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
100

    
101
    float origin0 = originAlpha*ax; // coords of the point where axis
102
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
103
    float origin2 = originAlpha*az; // the origin of our 2D coord system
104

    
105
    float v0 = point3D[0] - origin0;
106
    float v1 = point3D[1] - origin1;
107
    float v2 = point3D[2] - origin2;
108

    
109
    output[0] = v0*x0 + v1*x1 + v2*x2;
110
    output[1] = v0*y0 + v1*y1 + v2*y2;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
115

    
116
  int computeRotationIndex(float[][] rotAxis, float[] move2D, int[] enabled)
117
    {
118
    float cosAngle, minCosAngle = Float.MAX_VALUE;
119
    int minIndex=0, index;
120
    float m0 = move2D[0];
121
    float m1 = move2D[1];
122
    int numAxis = enabled[0];
123

    
124
    for(int axis=1; axis<=numAxis; axis++)
125
      {
126
      index = enabled[axis];
127
      cosAngle = m0*rotAxis[index][0] + m1*rotAxis[index][1];
128
      if( cosAngle<0 ) cosAngle = -cosAngle;
129

    
130
      if( cosAngle<minCosAngle )
131
        {
132
        minCosAngle=cosAngle;
133
        minIndex = index;
134
        }
135
      }
136

    
137
    return minIndex;
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  public float returnRotationFactor(int axis, int row)
143
    {
144
    return mRotationFactor==null ? 1.0f : mRotationFactor[axis][row];
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public void enableGhostAxis(int axNum, boolean enable)
150
    {
151
    mGhostAxisEnabled = enable ? -1 : axNum;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
157
    {
158
    if( cuts==null ) return null;
159

    
160
    int len = cuts.length;
161
    float[] border = new float[len];
162

    
163
    for(int i=0; i<len; i++)
164
      {
165
      if( !rotatable[i] )
166
        {
167
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
168
        }
169
      else
170
        {
171
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
172
        else
173
          {
174
          int found = -1;
175

    
176
          for(int j=i+2; j<=len; j++)
177
            {
178
            if( rotatable[j] )
179
              {
180
              found=j;
181
              break;
182
              }
183
            }
184

    
185
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
186
          }
187
        }
188
      }
189

    
190
    return border;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
195

    
196
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
197
    {
198
    int numCuts = cuts.length;
199
    mTouchBorders = new float[numCuts][];
200

    
201
    for(int axis=0; axis<numCuts; axis++)
202
      {
203
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public abstract boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera);
210
  public abstract void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat);
211
  public abstract void getCastedRotAxis(float[] output, Static4D quat, int rotIndex);
212
  public abstract boolean axisAndFaceAgree(int rotIndex);
213
  public abstract int getTouchedCubitFace();
214
  public abstract int getTouchedCubit();
215
  public abstract float[] getTouchedPuzzleCenter();
216
  }
(1-1/14)