Project

General

Profile

Download (5.51 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / object / RubikObjectMovement.java @ dd65ead3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.object;
21

    
22
import org.distorted.library.type.Static2D;
23
import org.distorted.library.type.Static4D;
24

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

    
27
public abstract class RubikObjectMovement
28
  {
29
  float[] mPoint, mCamera, mTouch;
30
  int mRotationVect, mLastTouchedAxis;
31

    
32
  private float[] mDiff;
33
  private int mLastTouchedLR;
34
  private int mNumAxis, mNumFacesPerAxis;
35
  private int[] mPossible;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  abstract boolean faceIsVisible(int axis, int lr);
40
  abstract void castTouchPointOntoFace(int axis, int lr, float[] output);
41
  abstract boolean isInsideFace(float[] point);
42
  abstract void fillPossibleRotations(int axis, int[] output);
43
  abstract float fillUpRotationVectAndOffset(float[] vect, int[] possible);
44
  abstract float returnAngle(float[] vect, int[] possible);
45

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

    
48
  RubikObjectMovement(int numAxis, int numFacesPerAxis)
49
    {
50
    mPoint = new float[3];
51
    mCamera= new float[3];
52
    mDiff  = new float[3];
53
    mTouch = new float[3];
54

    
55
    mNumAxis = numAxis;
56
    mNumFacesPerAxis = numFacesPerAxis;
57
    mPossible = new int[mNumAxis-1];
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
65
    {
66
    mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
67
    mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
68
    mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
69

    
70
    mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
71
    mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
72
    mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
73

    
74
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
75
      {
76
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
77
        {
78
        if( faceIsVisible(mLastTouchedAxis, mLastTouchedLR) )
79
          {
80
          castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mTouch);
81

    
82
          if( isInsideFace(mTouch) )
83
            {
84
            fillPossibleRotations(mLastTouchedAxis, mPossible);
85
            return true;
86
            }
87
          }
88
        }
89
      }
90

    
91
    return false;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  public Static2D newRotation(Static4D rotatedTouchPoint)
97
    {
98
    mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
99
    mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
100
    mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
101

    
102
    castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
103

    
104
    mDiff[0] -= mTouch[0];
105
    mDiff[1] -= mTouch[1];
106
    mDiff[2] -= mTouch[2];
107

    
108
    float offset = fillUpRotationVectAndOffset(mDiff, mPossible);
109

    
110
    mTouch[0] = mPoint[0];
111
    mTouch[1] = mPoint[1];
112
    mTouch[2] = mPoint[2];
113

    
114
    return new Static2D(mRotationVect,offset);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public float continueRotation(Static4D rotatedTouchPoint)
120
    {
121
    mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[0];
122
    mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[1];
123
    mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[2];
124

    
125
    return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible);
126
    }
127
  }
(6-6/8)