Project

General

Profile

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

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

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

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

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

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

    
83
    return minAxis;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// PUBLIC API
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

    
96
    if( mGhostAxisEnabled<0 )
97
      {
98
      int disabledAxis = computeDisabledAxis(quat);
99
      int rotIndex = computeRotationIndex(disabledAxis,dx,dy,dz);
100
      int row = computeRow(mTouchedCubit,rotIndex);
101
      output[0] = rotIndex;
102
      output[1] = row;
103
      }
104
    else
105
      {
106
      output[0] = mGhostAxisEnabled;
107
      output[1] = computeRow(mTouchedCubit,mGhostAxisEnabled);
108
      }
109
    }
110
  }
(12-12/14)