Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlMirror.java @ 2f7b42cf

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.Static4D;
24
import org.distorted.objectlib.main.TwistyObject;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class TouchControlMirror extends TouchControlShapeChanging
29
  {
30
  public TouchControlMirror(TwistyObject object)
31
    {
32
    super(object);
33
    }
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
// We want to avoid the situation when we grab by one of the inside, black faces and that makes us
37
// compute an incorrect 'disabled' ax.
38
// If the touched face is black, return one of the 'not black' faces of the current touched cubit
39
// - the one which is the most flat with the screen, i.e. the one whose normal makes the smallest
40
// angle with the camera vector.
41

    
42
  private int correctTouchedFace(int cubit, int face, float[] quat)
43
    {
44
    int[] numLayers = mObject.getNumLayers();
45
    int variant = mObject.getCubitVariant(cubit,numLayers);
46
    int color = mObject.getVariantFaceColor(variant,face);
47

    
48
    if( color>=0 ) return face;
49

    
50
    float cx = mCamera[0];
51
    float cy = mCamera[1];
52
    float cz = mCamera[2];
53

    
54
    float len = (float)Math.sqrt(cx*cx+cy*cy+cz*cz);
55

    
56
    cx /= len;
57
    cy /= len;
58
    cz /= len;
59

    
60
    float[] normal = mInfos[mTouchedCubit][face].getNormal();
61
    QuatHelper.rotateVectorByQuat(mTmp,normal,quat);
62

    
63
    int maxFace= 0;
64
    float maxAngle = -1.0f;
65

    
66
    for(int i=0; i<6; i++)
67
      if( i!=face )
68
        {
69
        color = mObject.getVariantFaceColor(variant,i);
70

    
71
        if( color>=0 )
72
          {
73
          normal = mInfos[mTouchedCubit][i].getNormal();
74
          QuatHelper.rotateVectorByQuat(mTmp,normal,quat);
75
          float angle = cx*mTmp[0] + cy*mTmp[1] + cz*mTmp[2];
76

    
77
          if( angle>maxAngle )
78
            {
79
            maxAngle= angle;
80
            maxFace = i;
81
            }
82
          }
83
        }
84

    
85
    return maxFace;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  private int computeDisabledAxis()
91
    {
92
    int quatIndex  = mObject.getCubitQuatIndex(mTouchedCubit);
93
    float[] quat   = mQuats[quatIndex];
94
    int face = correctTouchedFace(mTouchedCubit,mTouchedFace,quat);
95
    float[] normal = mInfos[mTouchedCubit][face].getNormal();
96
    QuatHelper.rotateVectorByQuat(mTmp,normal,quat);
97

    
98
    float cx = mTmp[0];
99
    float cy = mTmp[1];
100
    float cz = mTmp[2];
101

    
102
    float max = -1.0f;
103
    int maxAxis = -1;
104

    
105
    for(int axis=0; axis<mNumAxis; axis++)
106
      {
107
      float ax = mRotAxis[axis].get0();
108
      float ay = mRotAxis[axis].get1();
109
      float az = mRotAxis[axis].get2();
110

    
111
      float angle = ax*cx + ay*cy + az*cz;
112
      if( angle<0 ) angle = -angle;
113

    
114
      if( angle>max )
115
        {
116
        max=angle;
117
        maxAxis = axis;
118
        }
119
      }
120

    
121
    return maxAxis;
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  private int computeRotationIndex(int disabled, float dx, float dy, float dz)
127
    {
128
    float min = Float.MAX_VALUE;
129
    int minAxis = -1;
130

    
131
    for(int axis=0; axis<mNumAxis; axis++)
132
      if( axis!=disabled )
133
        {
134
        float ax = mRotAxis[axis].get0();
135
        float ay = mRotAxis[axis].get1();
136
        float az = mRotAxis[axis].get2();
137

    
138
        float angle = dx*ax + dy*ay + dz*az;
139
        if( angle<0 ) angle=-angle;
140

    
141
        if( angle<min )
142
          {
143
          min=angle;
144
          minAxis = axis;
145
          }
146
        }
147

    
148
    return minAxis;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// PUBLIC API
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
156
    {
157
    float dx = mPoint[0] - rotatedTouchPoint.get0()/mObjectRatio;
158
    float dy = mPoint[1] - rotatedTouchPoint.get1()/mObjectRatio;
159
    float dz = mPoint[2] - rotatedTouchPoint.get2()/mObjectRatio;
160

    
161
    int disabledAxis = computeDisabledAxis();
162
    int rotIndex     = computeRotationIndex(disabledAxis,dx,dy,dz);
163
    int row          = computeRow(mTouchedCubit,rotIndex);
164

    
165
    output[0] = rotIndex;
166
    output[1] = row;
167
    }
168
  }
(5-5/11)