Project

General

Profile

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

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

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

    
16
///////////////////////////////////////////////////////////////////////////////////////////////////
17

    
18
public class TouchControlShapemod extends TouchControlShapeChanging
19
  {
20
  public TouchControlShapemod(TwistyObject object)
21
    {
22
    super(object);
23
    }
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
  private int computeDisabledAxis(Static4D quat)
28
    {
29
    final float D = 0.2f;
30
    float minLen  = 1.0f;
31
    float sumLens = 0.0f;
32
    int minIndex = -1;
33

    
34
    for(int axis=0; axis<mNumAxis; axis++)
35
      {
36
      float x = mRotAxis[axis].get0();;
37
      float y = mRotAxis[axis].get1();
38
      float z = mRotAxis[axis].get2();
39
      float w = 0.0f;
40

    
41
      QuatHelper.rotateVectorByQuat(mTmp,x,y,z,w,quat);
42

    
43
      float rx = mTmp[0];
44
      float ry = mTmp[1];
45
      float rz = mTmp[2];
46

    
47
      float len = rx*rx + ry*ry;
48
      sumLens += len;
49

    
50
      if( len<minLen )
51
        {
52
        minLen = len;
53
        minIndex = axis;
54
        }
55
      }
56

    
57
    return ( minLen < D*sumLens ) ? minIndex : -1;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private int computeRotationIndex(int disabled, float dx, float dy, float dz)
63
    {
64
    float min = Float.MAX_VALUE;
65
    int minAxis = -1;
66

    
67
    for(int axis=0; axis<mNumAxis; axis++)
68
      if( axis!=disabled )
69
        {
70
        float ax = mRotAxis[axis].get0();
71
        float ay = mRotAxis[axis].get1();
72
        float az = mRotAxis[axis].get2();
73

    
74
        float angle = dx*ax + dy*ay + dz*az;
75
        if( angle<0 ) angle=-angle;
76

    
77
        if( angle<min )
78
          {
79
          min=angle;
80
          minAxis = axis;
81
          }
82
        }
83

    
84
    return minAxis;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// PUBLIC API
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
92
    {
93
    float dx = mPoint[0] - rotatedTouchPoint.get0()/mObjectRatio;
94
    float dy = mPoint[1] - rotatedTouchPoint.get1()/mObjectRatio;
95
    float dz = mPoint[2] - rotatedTouchPoint.get2()/mObjectRatio;
96

    
97
    int disabledAxis = computeDisabledAxis(quat);
98
    int rotIndex     = computeRotationIndex(disabledAxis,dx,dy,dz);
99
    int row          = computeRow(mTouchedCubit,rotIndex);
100

    
101
    output[0] = rotIndex;
102
    output[1] = row;
103
    }
104
  }
(11-11/13)