Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlShapemod.java @ 3a0a23bf

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 TouchControlShapemod extends TouchControlShapeChanging
29
  {
30
  public TouchControlShapemod(TwistyObject object)
31
    {
32
    super(object);
33
    }
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  private int computeDisabledAxis(Static4D quat)
38
    {
39
    final float D = 0.2f;
40
    float minLen  = 1.0f;
41
    float sumLens = 0.0f;
42
    int minIndex = -1;
43

    
44
    for(int axis=0; axis<mNumAxis; axis++)
45
      {
46
      float x = mRotAxis[axis].get0();;
47
      float y = mRotAxis[axis].get1();
48
      float z = mRotAxis[axis].get2();
49
      float w = 0.0f;
50

    
51
      QuatHelper.rotateVectorByQuat(mTmp,x,y,z,w,quat);
52

    
53
      float rx = mTmp[0];
54
      float ry = mTmp[1];
55
      float rz = mTmp[2];
56

    
57
      float len = rx*rx + ry*ry;
58
      sumLens += len;
59

    
60
      if( len<minLen )
61
        {
62
        minLen = len;
63
        minIndex = axis;
64
        }
65
      }
66

    
67
    return ( minLen < D*sumLens ) ? minIndex : -1;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  private int computeRotationIndex(int disabled, float dx, float dy, float dz)
73
    {
74
    float min = Float.MAX_VALUE;
75
    int minAxis = -1;
76

    
77
    for(int axis=0; axis<mNumAxis; axis++)
78
      if( axis!=disabled )
79
        {
80
        float ax = mRotAxis[axis].get0();
81
        float ay = mRotAxis[axis].get1();
82
        float az = mRotAxis[axis].get2();
83

    
84
        float angle = dx*ax + dy*ay + dz*az;
85
        if( angle<0 ) angle=-angle;
86

    
87
        if( angle<min )
88
          {
89
          min=angle;
90
          minAxis = axis;
91
          }
92
        }
93

    
94
    return minAxis;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// PUBLIC API
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
102
    {
103
    float dx = mPoint[0] - rotatedTouchPoint.get0()/mObjectRatio;
104
    float dy = mPoint[1] - rotatedTouchPoint.get1()/mObjectRatio;
105
    float dz = mPoint[2] - rotatedTouchPoint.get2()/mObjectRatio;
106

    
107
    int disabledAxis = computeDisabledAxis(quat);
108
    int rotIndex     = computeRotationIndex(disabledAxis,dx,dy,dz);
109
    int row          = computeRow(mTouchedCubit,rotIndex);
110

    
111
    output[0] = rotIndex;
112
    output[1] = row;
113
    }
114
  }
(9-9/11)