Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCubeMovement.java @ 9cd7695f

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, mLastTouchedAxis, mLastTouchedLR;
42
    private int mNumAxis, mNumFacesPerAxis;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    private int retFaceRotationSign(int axis, int lr)
54
      {
55
      int face = axis*mNumFacesPerAxis + lr;
56
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
57
      }
58

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

    
61
    private int retFaceXaxis(int axis)
62
      {
63
      return axis==0 ? VECTZ : VECTX;
64
      }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
    private int retFaceYaxis(int axis)
69
      {
70
      return axis==1 ? VECTZ : VECTY;
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
    private boolean faceIsVisible(int axis, int lr)
76
      {
77
      return (lr==0 ? -1:1)*mCamera[axis] > 0.5f;
78
      }
79

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

    
85
    private void castTouchPointOntoFace(int axis, int lr, float[] output)
86
      {
87
      float diff = mPoint[axis]-mCamera[axis];
88
      float ratio =  diff!=0.0f ? ( (lr-0.5f)-mCamera[axis])/diff : 0.0f;
89

    
90
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
91
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
92
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
    RubikCubeMovement()
98
      {
99
      mPoint = new float[3];
100
      mCamera= new float[3];
101
      mDiff  = new float[3];
102
      mTouch = new float[3];
103

    
104
      mNumAxis = RubikCube.AXIS.length;
105
      mNumFacesPerAxis = RubikCube.FACE_COLORS.length / mNumAxis;
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// PUBLIC API
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
113
      {
114
      mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
115
      mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
116
      mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
117

    
118
      mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
119
      mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
120
      mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
121

    
122
      for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
123
        {
124
        for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
125
          {
126
          if( faceIsVisible(mLastTouchedAxis, mLastTouchedLR) )
127
            {
128
            castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mTouch);
129

    
130
            if( mTouch[0]<=0.5f && mTouch[0]>=-0.5f &&
131
                mTouch[1]<=0.5f && mTouch[1]>=-0.5f &&
132
                mTouch[2]<=0.5f && mTouch[2]>=-0.5f  )  return true;
133
            }
134
          }
135
        }
136

    
137
      return false;
138
      }
139

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

    
142
    public Static2D newRotation(Static4D rotatedTouchPoint)
143
      {
144
      mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
145
      mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
146
      mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
147

    
148
      castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
149

    
150
      mDiff[0] -= mTouch[0];
151
      mDiff[1] -= mTouch[1];
152
      mDiff[2] -= mTouch[2];
153

    
154
      int xAxis = retFaceXaxis(mLastTouchedAxis);
155
      int yAxis = retFaceYaxis(mLastTouchedAxis);
156
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
157
      float offset= mTouch[mRotationVect]+0.5f;
158

    
159
      mTouch[0] = mPoint[0];
160
      mTouch[1] = mPoint[1];
161
      mTouch[2] = mPoint[2];
162

    
163
      return new Static2D(mRotationVect,offset);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public float continueRotation(Static4D rotatedTouchPoint)
169
      {
170
      mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[0];
171
      mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[1];
172
      mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[2];
173

    
174
      int xAxis= retFaceXaxis(mLastTouchedAxis);
175
      int yAxis= retFaceYaxis(mLastTouchedAxis);
176
      int sign = retFaceRotationSign(mLastTouchedAxis, mLastTouchedLR);
177
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
178

    
179
      return sign*angle*0.5f;
180
      }
181
}
(3-3/8)