Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlSquare.java @ a72cd106

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.main.QuatHelper;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25
import org.distorted.objectlib.main.TwistyObject;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public class TouchControlSquare extends TouchControlShapeChanging
30
  {
31
  private static final float NOT_TOUCHED = 100000.0f;
32
  private static final float DIST3D = 0.5f;
33
  private static final float DIST2D = 0.5f;
34
  private static final float[][] mCastedRotAxis = { {0,1,0,0}, {1,0,0,0} };
35
  private static final int[] mEnabled = {2,0,1};
36
  private final int mNumFaces;
37
  private final Static3D[] mFaceAxis;
38
  private final float[][] mTmp4;
39
  private final float[][] mTmp2;
40
  private final float[] mTmp2D, mMove2D;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  public TouchControlSquare(TwistyObject object)
45
    {
46
    super(object);
47

    
48
    mNumFaces = object.getNumFaces();
49
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
50
    mTmp4  = new float[mNumFaces][4];
51
    mTmp2  = new float[mNumFaces][2];
52
    mTmp2D = new float[2];
53
    mMove2D= new float[2];
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  private boolean faceIsVisible(int index)
59
    {
60
    Static3D faceAxis = mFaceAxis[index];
61
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
62
    return castCameraOnAxis > DIST3D;
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  private boolean isInsideFace(float[] p)
68
    {
69
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  private int figureOutTouchedFace()
75
    {
76
    for( int face=0; face<mNumFaces; face++)
77
      {
78
      if( faceIsVisible(face) )
79
        {
80
        float nx = mFaceAxis[face].get0();
81
        float ny = mFaceAxis[face].get1();
82
        float nz = mFaceAxis[face].get2();
83

    
84
        castTouchPointOntoFace(nx,ny,nz, DIST3D, mTmp4[face]);
85
        convertTo2Dcoords(mTmp4[face], nx,ny,nz, mTmp2[face]);
86

    
87
        if( isInsideFace(mTmp2[face]) ) return face;
88
        }
89
      else mTmp4[face][0] = NOT_TOUCHED;
90
      }
91

    
92
    float maxDist = 0.0f;
93
    int faceTouched=-1;
94

    
95
    for( int face=0; face<mNumFaces; face++)
96
      {
97
      float[] point = mTmp4[face];
98

    
99
      if( point[0] != NOT_TOUCHED )
100
        {
101
        float cx = point[0]-mCamera[0];
102
        float cy = point[1]-mCamera[1];
103
        float cz = point[2]-mCamera[2];
104
        float dist = cx*cx + cy*cy + cz*cz;
105

    
106
        if( dist>maxDist )
107
          {
108
          maxDist = dist;
109
          faceTouched = face;
110
          }
111
        }
112
      }
113

    
114
    return faceTouched;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// PUBLIC API
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public void newRotation(int[] output, Static4D rotatedTouchPoint)
122
    {
123
    int rotIndex,face = figureOutTouchedFace();
124

    
125
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
126
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
127
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
128

    
129
    float nx = mFaceAxis[face].get0();
130
    float ny = mFaceAxis[face].get1();
131
    float nz = mFaceAxis[face].get2();
132

    
133
    castTouchPointOntoFace(nx,ny,nz, DIST3D, mTmp);
134
    convertTo2Dcoords(mTmp, nx,ny,nz, mTmp2D);
135

    
136
    switch(face)
137
      {
138
      case 0: case 1: rotIndex = 0;
139
                      break;
140
      case 2: case 3: rotIndex = 1;
141
                      break;
142
      default       : mMove2D[0] = mTmp2D[0]-mTmp2[face][0]; // mTmp2D contains an in-face-plane 2D 'new' point of touch;
143
                      mMove2D[1] = mTmp2D[1]-mTmp2[face][1]; // mTmp2 contains such 'old' point. Old as in - at the time
144
                                                             // of touchdown; 'new' as in - now, after moving the minimal
145
                                                             // distance.
146
                      rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabled);
147
      }
148

    
149
    output[0] = rotIndex;
150
    if( rotIndex==0 ) output[1] = mTmp2D[1]<0 ? 0:2;
151
    else              output[1] = computeRow(mTouchedCubit,rotIndex);
152

    
153
    //android.util.Log.e("D", "face touched="+face+" rotIndex="+rotIndex+" row="+output[1]+" in-plane Y="+mTmp2D[1]);
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
159
    {
160
    float[] axis = mCastedRotAxis[rotIndex];
161
    Static4D result = QuatHelper.rotateVectorByQuat(axis[0],axis[1],axis[2],axis[3],quat);
162

    
163
    float cx =result.get0();
164
    float cy =result.get1();
165

    
166
    float len = (float)Math.sqrt(cx*cx+cy*cy);
167

    
168
    if( len!=0 )
169
      {
170
      output[0] = cx/len;
171
      output[1] = cy/len;
172
      }
173
    else
174
      {
175
      output[0] = 1;
176
      output[1] = 0;
177
      }
178
    }
179
  }
(9-9/10)