Project

General

Profile

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

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

1 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 efef689c Leszek Koltunski
import org.distorted.library.type.Static2D;
23 beb325a0 Leszek Koltunski
import org.distorted.library.type.Static4D;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27 27a70eae Leszek Koltunski
class RubikCubeMovement extends RubikObjectMovement
28 beb325a0 Leszek Koltunski
{
29 93594b95 Leszek Koltunski
    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 beb325a0 Leszek Koltunski
36 3c4a326c Leszek Koltunski
    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 86c73a69 Leszek Koltunski
    private float[] mPoint, mCamera, mDiff, mTouch;
41 123d6172 Leszek Koltunski
    private int mRotationVect, mLastTouchedFace;
42 beb325a0 Leszek Koltunski
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 74686c71 Leszek Koltunski
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceXaxis(RIGHT) = VECTZ.
68 beb325a0 Leszek Koltunski
69
    private int retFaceXaxis(int face)
70
      {
71 93594b95 Leszek Koltunski
      return face==LEFT || face==RIGHT ? VECTZ : VECTX;
72 beb325a0 Leszek Koltunski
      }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
    private int retFaceYaxis(int face)
77
      {
78 93594b95 Leszek Koltunski
      return face==TOP || face==BOTTOM ? VECTZ : VECTY;
79 beb325a0 Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 93594b95 Leszek Koltunski
    private int retFaceAxis(int face)
84 beb325a0 Leszek Koltunski
      {
85
      switch(face)
86
        {
87
        case FRONT :
88 27a70eae Leszek Koltunski
        case BACK  : return VECTZ;
89 beb325a0 Leszek Koltunski
        case LEFT  :
90 27a70eae Leszek Koltunski
        case RIGHT : return VECTX;
91 beb325a0 Leszek Koltunski
        case TOP   :
92 27a70eae Leszek Koltunski
        case BOTTOM: return VECTY;
93 beb325a0 Leszek Koltunski
        }
94
95
      return -1;
96
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 775e675d Leszek Koltunski
100 93594b95 Leszek Koltunski
    private boolean faceIsVisible(int face)
101 775e675d Leszek Koltunski
      {
102
      int sign = retFaceSign(face);
103 93594b95 Leszek Koltunski
      int zAxis= retFaceAxis(face);
104 775e675d Leszek Koltunski
105 93594b95 Leszek Koltunski
      return sign*mCamera[zAxis] > 0.5f;
106 775e675d Leszek Koltunski
      }
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 93594b95 Leszek Koltunski
    private void castTouchPointOntoFace(int face, float[] output)
114 775e675d Leszek Koltunski
      {
115
      int sign = retFaceSign(face);
116 93594b95 Leszek Koltunski
      int zAxis= retFaceAxis(face);
117 775e675d Leszek Koltunski
      float diff = mPoint[zAxis]-mCamera[zAxis];
118
119 93594b95 Leszek Koltunski
      float ratio =  diff!=0.0f ? (sign*0.5f-mCamera[zAxis])/diff : 0.0f;
120 775e675d Leszek Koltunski
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 27a70eae Leszek Koltunski
    RubikCubeMovement()
129 775e675d Leszek Koltunski
      {
130 beb325a0 Leszek Koltunski
      mPoint = new float[3];
131
      mCamera= new float[3];
132
      mDiff  = new float[3];
133 86c73a69 Leszek Koltunski
      mTouch = new float[3];
134 beb325a0 Leszek Koltunski
      }
135
136 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// PUBLIC API
138 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140 86c73a69 Leszek Koltunski
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
141 beb325a0 Leszek Koltunski
      {
142 93594b95 Leszek Koltunski
      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 86c73a69 Leszek Koltunski
146 93594b95 Leszek Koltunski
      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 beb325a0 Leszek Koltunski
150 93594b95 Leszek Koltunski
      for( mLastTouchedFace=LEFT; mLastTouchedFace<=FRONT; mLastTouchedFace++)
151 beb325a0 Leszek Koltunski
        {
152 93594b95 Leszek Koltunski
        if( faceIsVisible(mLastTouchedFace) )
153 beb325a0 Leszek Koltunski
          {
154 93594b95 Leszek Koltunski
          castTouchPointOntoFace(mLastTouchedFace, mTouch);
155 beb325a0 Leszek Koltunski
156 93594b95 Leszek Koltunski
          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 beb325a0 Leszek Koltunski
          }
160
        }
161
162 123d6172 Leszek Koltunski
      return false;
163 beb325a0 Leszek Koltunski
      }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 efef689c Leszek Koltunski
    public Static2D newRotation(Static4D rotatedTouchPoint)
168 beb325a0 Leszek Koltunski
      {
169 93594b95 Leszek Koltunski
      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 86c73a69 Leszek Koltunski
173 93594b95 Leszek Koltunski
      castTouchPointOntoFace(mLastTouchedFace,mDiff);
174 beb325a0 Leszek Koltunski
175 86c73a69 Leszek Koltunski
      mDiff[0] -= mTouch[0];
176
      mDiff[1] -= mTouch[1];
177
      mDiff[2] -= mTouch[2];
178 beb325a0 Leszek Koltunski
179 123d6172 Leszek Koltunski
      int xAxis = retFaceXaxis(mLastTouchedFace);
180
      int yAxis = retFaceYaxis(mLastTouchedFace);
181 93594b95 Leszek Koltunski
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
182
      float offset= mTouch[mRotationVect]+0.5f;
183 beb325a0 Leszek Koltunski
184 86c73a69 Leszek Koltunski
      mTouch[0] = mPoint[0];
185
      mTouch[1] = mPoint[1];
186
      mTouch[2] = mPoint[2];
187 beb325a0 Leszek Koltunski
188 efef689c Leszek Koltunski
      return new Static2D(mRotationVect,offset);
189 beb325a0 Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 86c73a69 Leszek Koltunski
    public float continueRotation(Static4D rotatedTouchPoint)
194 beb325a0 Leszek Koltunski
      {
195 93594b95 Leszek Koltunski
      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 beb325a0 Leszek Koltunski
199 123d6172 Leszek Koltunski
      int xAxis= retFaceXaxis(mLastTouchedFace);
200
      int yAxis= retFaceYaxis(mLastTouchedFace);
201
      int sign = retFaceRotationSign(mLastTouchedFace);
202 beb325a0 Leszek Koltunski
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
203
204 93594b95 Leszek Koltunski
      return sign*angle*0.5f;
205 beb325a0 Leszek Koltunski
      }
206
}