Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControl.java @ 369e6176

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

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

    
16
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
  // each face is split into several parts by lines coming from its center to the midpoints of each edge,
25
  // and also it has an inscribed circle (see coin tetrahedron!)
26
  public static final int TYPE_SPLIT_EDGE_COIN= 3;
27

    
28
  static final float D_TRIANGLE = 0.95f;
29
  static final float D_SQUARE   = 0.90f;
30
  static final float D_PENTA    = 0.55f;
31

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

    
43
  float mObjectRatio;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  public TouchControl(float ratio)
48
    {
49
    mObjectRatio = ratio;
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  public void setObjectRatio(float ratio)
55
    {
56
    mObjectRatio = ratio;
57
    }
58

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

    
70
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
71
    {
72
    float y0,y1,y2; // base Y vector of the 2D coord system
73

    
74
    if( ax==0.0f && az==0.0f )
75
      {
76
      y0=0; y1=0; y2=-ay;
77
      }
78
    else if( ay==0.0f )
79
      {
80
      y0=0; y1=1; y2=0;
81
      }
82
    else
83
      {
84
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
85
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
86
      }
87

    
88
    float x0 = y1*az - y2*ay;  //
89
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
90
    float x2 = y0*ay - y1*ax;  //
91

    
92
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
93

    
94
    float origin0 = originAlpha*ax; // coords of the point where axis
95
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
96
    float origin2 = originAlpha*az; // the origin of our 2D coord system
97

    
98
    float v0 = point3D[0] - origin0;
99
    float v1 = point3D[1] - origin1;
100
    float v2 = point3D[2] - origin2;
101

    
102
    output[0] = v0*x0 + v1*x1 + v2*x2;
103
    output[1] = v0*y0 + v1*y1 + v2*y2;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
108

    
109
  int computeRotationIndex(float[][] rotAxis, float[] move2D, int[] enabled)
110
    {
111
    float cosAngle, minCosAngle = Float.MAX_VALUE;
112
    int minIndex=0, index;
113
    float m0 = move2D[0];
114
    float m1 = move2D[1];
115
    int numAxis = enabled[0];
116

    
117
    for(int axis=1; axis<=numAxis; axis++)
118
      {
119
      index = enabled[axis];
120
      cosAngle = m0*rotAxis[index][0] + m1*rotAxis[index][1];
121
      if( cosAngle<0 ) cosAngle = -cosAngle;
122

    
123
      if( cosAngle<minCosAngle )
124
        {
125
        minCosAngle=cosAngle;
126
        minIndex = index;
127
        }
128
      }
129

    
130
    return minIndex;
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  public abstract boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera);
136
  public abstract void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat);
137
  public abstract void getCastedRotAxis(float[] output, Static4D quat, int rotIndex);
138
  public abstract int getTouchedCubitFace();
139
  public abstract int getTouchedCubit();
140
  public abstract float[] getTouchedPuzzleCenter();
141
  public abstract float returnRotationFactor(int[] numLayers, int row);
142
  }
(1-1/13)