Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCubeMovement.java @ 775e675d

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
import org.distorted.magic.RubikSurfaceView;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class RubikCubeMovement
29
{
30
    public  final static int NONE   =-1;
31
    private final static int FRONT  = 0;  // has to be 6 consecutive ints
32
    private final static int BACK   = 1;  // FRONT ... BOTTOM
33
    private final static int LEFT   = 2;  //
34
    private final static int RIGHT  = 3;  //
35
    private final static int TOP    = 4;  //
36
    private final static int BOTTOM = 5;  //
37

    
38
    private static final int[] VECT = {RubikCube.VECTX,RubikCube.VECTY,RubikCube.VECTZ};
39

    
40
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint;
41
    private int mRotationVect;
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 retFaceZaxis(RIGHT) = VECTX.
68

    
69
    private int retFaceXaxis(int face)
70
      {
71
      switch(face)
72
        {
73
        case FRONT :
74
        case BACK  : return RubikCube.VECTX;
75
        case LEFT  :
76
        case RIGHT : return RubikCube.VECTZ;
77
        case TOP   :
78
        case BOTTOM: return RubikCube.VECTX;
79
        }
80

    
81
      return -1;
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
    private int retFaceYaxis(int face)
87
      {
88
      switch(face)
89
        {
90
        case FRONT :
91
        case BACK  : return RubikCube.VECTY;
92
        case LEFT  :
93
        case RIGHT : return RubikCube.VECTY;
94
        case TOP   :
95
        case BOTTOM: return RubikCube.VECTZ;
96
        }
97

    
98
      return -1;
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
    private int retFaceZaxis(int face)
104
      {
105
      switch(face)
106
        {
107
        case FRONT :
108
        case BACK  : return RubikCube.VECTZ;
109
        case LEFT  :
110
        case RIGHT : return RubikCube.VECTX;
111
        case TOP   :
112
        case BOTTOM: return RubikCube.VECTY;
113
        }
114

    
115
      return -1;
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    private boolean faceIsVisible(int face, float cubeHalfSize)
121
      {
122
      int sign = retFaceSign(face);
123
      int zAxis= retFaceZaxis(face);
124

    
125
      return sign*mCamera[zAxis] > cubeHalfSize;
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    private void convertTouchPointToScreenSpace(Static4D accumulated, float x, float y)
131
      {
132
      Static4D touchPoint = new Static4D(x, y, 0, 0);
133
      Static4D rotatedTouchPoint= RubikSurfaceView.rotateVectorByInvertedQuat(touchPoint, accumulated);
134

    
135
      mPoint[0] = rotatedTouchPoint.get1();
136
      mPoint[1] = rotatedTouchPoint.get2();
137
      mPoint[2] = rotatedTouchPoint.get3();
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    private void convertCameraPointToScreenSpace(Static4D accumulated, float cameraDistance)
143
      {
144
      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0);
145
      Static4D rotatedCamera= RubikSurfaceView.rotateVectorByInvertedQuat(cameraPoint, accumulated);
146

    
147
      mCamera[0] = rotatedCamera.get1();
148
      mCamera[1] = rotatedCamera.get2();
149
      mCamera[2] = rotatedCamera.get3();
150
      }
151

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

    
157
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
158
      {
159
      int sign = retFaceSign(face);
160
      int zAxis= retFaceZaxis(face);
161
      float diff = mPoint[zAxis]-mCamera[zAxis];
162

    
163
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
164

    
165
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
166
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
167
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
// PUBLIC API
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    public RubikCubeMovement()
175
      {
176
      mRotationVect = VECT[0];
177

    
178
      mPoint = new float[3];
179
      mCamera= new float[3];
180
      mDiff  = new float[3];
181
      mTouchPoint = new float[3];
182
      mTouchPointCastOntoFace = new float[3];
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public int faceTouched(Static4D accumulated, float cameraDistance, float x, float y)
188
      {
189
      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
190

    
191
      convertTouchPointToScreenSpace(accumulated,x,y);
192
      convertCameraPointToScreenSpace(accumulated, cameraDistance);
193

    
194
      for(int face=FRONT; face<=BOTTOM; face++)
195
        {
196
        if( faceIsVisible(face,cubeHalfSize) )
197
          {
198
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
199

    
200
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
201
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
202
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
203

    
204
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
205
          }
206
        }
207

    
208
      return NONE;
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    public Static2D newRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
214
      {
215
      float cubeHalfSize= RubikCube.CUBE_SCREEN_RATIO*0.5f;
216

    
217
      convertTouchPointToScreenSpace(accumulated,x,y);
218
      castTouchPointOntoFace(lastTouchedFace,cubeHalfSize,mDiff);
219

    
220
      mDiff[0] -= mTouchPointCastOntoFace[0];
221
      mDiff[1] -= mTouchPointCastOntoFace[1];
222
      mDiff[2] -= mTouchPointCastOntoFace[2];
223

    
224
      int xAxis = retFaceXaxis(lastTouchedFace);
225
      int yAxis = retFaceYaxis(lastTouchedFace);
226
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
227
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
228

    
229
      mTouchPoint[0] = mPoint[0];
230
      mTouchPoint[1] = mPoint[1];
231
      mTouchPoint[2] = mPoint[2];
232

    
233
      return new Static2D(mRotationVect,offset);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public float continueRotation(Static4D accumulated, int lastTouchedFace, float x, float y)
239
      {
240
      convertTouchPointToScreenSpace(accumulated,x,y);
241

    
242
      mDiff[0] = mPoint[0]-mTouchPoint[0];
243
      mDiff[1] = mPoint[1]-mTouchPoint[1];
244
      mDiff[2] = mPoint[2]-mTouchPoint[2];
245

    
246
      int xAxis= retFaceXaxis(lastTouchedFace);
247
      int yAxis= retFaceYaxis(lastTouchedFace);
248
      int sign = retFaceRotationSign(lastTouchedFace);
249
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
250

    
251
      return sign*angle;
252
      }
253
}
(2-2/2)