Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCubeMovement.java @ 93594b95

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
class RubikCubeMovement extends RubikObjectMovement
28
{
29
    private final static int LEFT   = 0;  // axisX left
30
    private final static int RIGHT  = 1;  // axisX right
31
    private final static int BOTTOM = 2;  // axisY left
32
    private final static int TOP    = 3;  // axisY right
33
    private final static int BACK   = 4;  // axisZ left
34
    private final static int FRONT  = 5;  // axisZ right
35

    
36
    private static final int VECTX  = 0;  //
37
    private static final int VECTY  = 1;  // don't change this
38
    private static final int VECTZ  = 2;  //
39

    
40
    private float[] mPoint, mCamera, mDiff, mTouch;
41
    private int mRotationVect, mLastTouchedFace;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
    private boolean isVertical(float x, float y)
46
      {
47
      return (y>x) ? (y>=-x) : (y< -x);
48
      }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
    private int retFaceSign(int face)
53
      {
54
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
55
      }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    private int retFaceRotationSign(int face)
60
      {
61
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
62
      }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
66
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
67
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceXaxis(RIGHT) = VECTZ.
68

    
69
    private int retFaceXaxis(int face)
70
      {
71
      return face==LEFT || face==RIGHT ? VECTZ : VECTX;
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
    private int retFaceYaxis(int face)
77
      {
78
      return face==TOP || face==BOTTOM ? VECTZ : VECTY;
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    private int retFaceAxis(int face)
84
      {
85
      switch(face)
86
        {
87
        case FRONT :
88
        case BACK  : return VECTZ;
89
        case LEFT  :
90
        case RIGHT : return VECTX;
91
        case TOP   :
92
        case BOTTOM: return VECTY;
93
        }
94

    
95
      return -1;
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    private boolean faceIsVisible(int face)
101
      {
102
      int sign = retFaceSign(face);
103
      int zAxis= retFaceAxis(face);
104

    
105
      return sign*mCamera[zAxis] > 0.5f;
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
110
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
111
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
112

    
113
    private void castTouchPointOntoFace(int face, float[] output)
114
      {
115
      int sign = retFaceSign(face);
116
      int zAxis= retFaceAxis(face);
117
      float diff = mPoint[zAxis]-mCamera[zAxis];
118

    
119
      float ratio =  diff!=0.0f ? (sign*0.5f-mCamera[zAxis])/diff : 0.0f;
120

    
121
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
122
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
123
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    RubikCubeMovement()
129
      {
130
      mPoint = new float[3];
131
      mCamera= new float[3];
132
      mDiff  = new float[3];
133
      mTouch = new float[3];
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// PUBLIC API
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
141
      {
142
      mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
143
      mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
144
      mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
145

    
146
      mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
147
      mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
148
      mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
149

    
150
      for( mLastTouchedFace=LEFT; mLastTouchedFace<=FRONT; mLastTouchedFace++)
151
        {
152
        if( faceIsVisible(mLastTouchedFace) )
153
          {
154
          castTouchPointOntoFace(mLastTouchedFace, mTouch);
155

    
156
          if( mTouch[0]<=0.5f && mTouch[0]>=-0.5f &&
157
              mTouch[1]<=0.5f && mTouch[1]>=-0.5f &&
158
              mTouch[2]<=0.5f && mTouch[2]>=-0.5f  ) return true;
159
          }
160
        }
161

    
162
      return false;
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    public Static2D newRotation(Static4D rotatedTouchPoint)
168
      {
169
      mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
170
      mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
171
      mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
172

    
173
      castTouchPointOntoFace(mLastTouchedFace,mDiff);
174

    
175
      mDiff[0] -= mTouch[0];
176
      mDiff[1] -= mTouch[1];
177
      mDiff[2] -= mTouch[2];
178

    
179
      int xAxis = retFaceXaxis(mLastTouchedFace);
180
      int yAxis = retFaceYaxis(mLastTouchedFace);
181
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
182
      float offset= mTouch[mRotationVect]+0.5f;
183

    
184
      mTouch[0] = mPoint[0];
185
      mTouch[1] = mPoint[1];
186
      mTouch[2] = mPoint[2];
187

    
188
      return new Static2D(mRotationVect,offset);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public float continueRotation(Static4D rotatedTouchPoint)
194
      {
195
      mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[0];
196
      mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[1];
197
      mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO-mTouch[2];
198

    
199
      int xAxis= retFaceXaxis(mLastTouchedFace);
200
      int yAxis= retFaceYaxis(mLastTouchedFace);
201
      int sign = retFaceRotationSign(mLastTouchedFace);
202
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
203

    
204
      return sign*angle*0.5f;
205
      }
206
}
(3-3/8)