Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.helpers.QuatHelper;
13
import org.distorted.library.type.Static3D;
14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.main.TwistyObject;
16

    
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

    
19
public class TouchControlSquare extends TouchControlShapeChanging
20
  {
21
  private static final float NOT_TOUCHED = 100000.0f;
22
  private static final float DIST3D = 0.5f;
23
  private static final float DIST2D = 0.5f;
24
  private static final float[][] mCastedRotAxis = { {0,1,0,0}, {1,0,0,0} };
25
  private static final int[] mEnabled = {2,0,1};
26
  private final int mNumFaces;
27
  private final Static3D[] mFaceAxis;
28
  private final float[][] mTmp4;
29
  private final float[][] mTmp2;
30
  private final float[] mTmp2D, mMove2D;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
  public TouchControlSquare(TwistyObject object)
35
    {
36
    super(object);
37

    
38
    mNumFaces = object.getNumPuzzleFaces();
39
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
40
    mTmp4  = new float[mNumFaces][4];
41
    mTmp2  = new float[mNumFaces][2];
42
    mTmp2D = new float[2];
43
    mMove2D= new float[2];
44
    }
45

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

    
48
  private boolean faceIsVisible(int index)
49
    {
50
    Static3D faceAxis = mFaceAxis[index];
51
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
52
    return castCameraOnAxis > DIST3D;
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private boolean isInsideFace(float[] p)
58
    {
59
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  private int figureOutTouchedFace()
65
    {
66
    for( int face=0; face<mNumFaces; face++)
67
      {
68
      if( faceIsVisible(face) )
69
        {
70
        float nx = mFaceAxis[face].get0();
71
        float ny = mFaceAxis[face].get1();
72
        float nz = mFaceAxis[face].get2();
73

    
74
        castTouchPointOntoFace(nx,ny,nz, DIST3D, mTmp4[face]);
75
        convertTo2Dcoords(mTmp4[face], nx,ny,nz, mTmp2[face]);
76

    
77
        if( isInsideFace(mTmp2[face]) ) return face;
78
        }
79
      else mTmp4[face][0] = NOT_TOUCHED;
80
      }
81

    
82
    float maxDist = 0.0f;
83
    int faceTouched=-1;
84

    
85
    for( int face=0; face<mNumFaces; face++)
86
      {
87
      float[] point = mTmp4[face];
88

    
89
      if( point[0] != NOT_TOUCHED )
90
        {
91
        float cx = point[0]-mCamera[0];
92
        float cy = point[1]-mCamera[1];
93
        float cz = point[2]-mCamera[2];
94
        float dist = cx*cx + cy*cy + cz*cz;
95

    
96
        if( dist>maxDist )
97
          {
98
          maxDist = dist;
99
          faceTouched = face;
100
          }
101
        }
102
      }
103

    
104
    return faceTouched;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// PUBLIC API
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
112
    {
113
    int rotIndex,face = figureOutTouchedFace();
114

    
115
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
116
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
117
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
118

    
119
    float nx = mFaceAxis[face].get0();
120
    float ny = mFaceAxis[face].get1();
121
    float nz = mFaceAxis[face].get2();
122

    
123
    castTouchPointOntoFace(nx,ny,nz, DIST3D, mTmp);
124
    convertTo2Dcoords(mTmp, nx,ny,nz, mTmp2D);
125

    
126
    switch(face)
127
      {
128
      case 0: case 1: rotIndex = 0;
129
                      break;
130
      case 2: case 3: rotIndex = 1;
131
                      break;
132
      default       : mMove2D[0] = mTmp2D[0]-mTmp2[face][0]; // mTmp2D contains an in-face-plane 2D 'new' point of touch;
133
                      mMove2D[1] = mTmp2D[1]-mTmp2[face][1]; // mTmp2 contains such 'old' point. Old as in - at the time
134
                                                             // of touchdown; 'new' as in - now, after moving the minimal
135
                                                             // distance.
136
                      rotIndex = computeRotationIndex( mCastedRotAxis, mMove2D, mEnabled);
137
      }
138

    
139
    output[0] = rotIndex;
140
    output[1] = rotIndex==0 ? (mTmp2D[1]<0 ? 0:2) : computeRow(mTouchedCubit,rotIndex);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
146
    {
147
    float[] axis = mCastedRotAxis[rotIndex];
148
    Static4D result = QuatHelper.rotateVectorByQuat(axis[0],axis[1],axis[2],axis[3],quat);
149

    
150
    float cx =result.get0();
151
    float cy =result.get1();
152

    
153
    float len = (float)Math.sqrt(cx*cx+cy*cy);
154

    
155
    if( len!=0 )
156
      {
157
      output[0] = cx/len;
158
      output[1] = cy/len;
159
      }
160
    else
161
      {
162
      output[0] = 1;
163
      output[1] = 0;
164
      }
165
    }
166
  }
(12-12/13)