Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.touchcontrol;
21

    
22
import org.distorted.library.type.Static4D;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public abstract class TouchControl
27
  {
28
  // it doesn't matter where we touch a face - the list of enabled rotAxis will always be the same
29
  public static final int TYPE_NOT_SPLIT    = 0;
30
  // each face is split into several parts by lines coming from its center to the midpoints of each edge
31
  public static final int TYPE_SPLIT_EDGE   = 1;
32
  // each face is split into several parts by lines coming from its center to the vertices
33
  public static final int TYPE_SPLIT_CORNER = 2;
34

    
35
  public static final int TC_HEXAHEDRON        =   6;
36
  public static final int TC_TETRAHEDRON       =   4;
37
  public static final int TC_OCTAHEDRON        =   8;
38
  public static final int TC_DODECAHEDRON      =  12;
39
  public static final int TC_CUBOID            =   0;
40
  public static final int TC_CHANGING_MIRROR   = 100;
41
  public static final int TC_CHANGING_SQUARE   = 101;
42
  public static final int TC_CHANGING_SHAPEMOD = 102;
43

    
44
  float mObjectRatio;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

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

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

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

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

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

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

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

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

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

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

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

    
131
    return minIndex;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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