Project

General

Profile

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

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

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 9cd7695f Leszek Koltunski
    private int mRotationVect, mLastTouchedAxis, mLastTouchedLR;
42
    private int mNumAxis, mNumFacesPerAxis;
43 beb325a0 Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
    private boolean isVertical(float x, float y)
47
      {
48
      return (y>x) ? (y>=-x) : (y< -x);
49
      }
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 9cd7695f Leszek Koltunski
    private int retFaceRotationSign(int axis, int lr)
54 beb325a0 Leszek Koltunski
      {
55 9cd7695f Leszek Koltunski
      int face = axis*mNumFacesPerAxis + lr;
56 beb325a0 Leszek Koltunski
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
57
      }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 9cd7695f Leszek Koltunski
    private int retFaceXaxis(int axis)
62 beb325a0 Leszek Koltunski
      {
63 9cd7695f Leszek Koltunski
      return axis==0 ? VECTZ : VECTX;
64 beb325a0 Leszek Koltunski
      }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 9cd7695f Leszek Koltunski
    private int retFaceYaxis(int axis)
69 beb325a0 Leszek Koltunski
      {
70 9cd7695f Leszek Koltunski
      return axis==1 ? VECTZ : VECTY;
71 beb325a0 Leszek Koltunski
      }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 9cd7695f Leszek Koltunski
    private boolean faceIsVisible(int axis, int lr)
76 beb325a0 Leszek Koltunski
      {
77 9cd7695f Leszek Koltunski
      return (lr==0 ? -1:1)*mCamera[axis] > 0.5f;
78 775e675d Leszek Koltunski
      }
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 9cd7695f Leszek Koltunski
    private void castTouchPointOntoFace(int axis, int lr, float[] output)
86 775e675d Leszek Koltunski
      {
87 9cd7695f Leszek Koltunski
      float diff = mPoint[axis]-mCamera[axis];
88
      float ratio =  diff!=0.0f ? ( (lr-0.5f)-mCamera[axis])/diff : 0.0f;
89 775e675d Leszek Koltunski
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 27a70eae Leszek Koltunski
    RubikCubeMovement()
98 775e675d Leszek Koltunski
      {
99 beb325a0 Leszek Koltunski
      mPoint = new float[3];
100
      mCamera= new float[3];
101
      mDiff  = new float[3];
102 86c73a69 Leszek Koltunski
      mTouch = new float[3];
103 9cd7695f Leszek Koltunski
104
      mNumAxis = RubikCube.AXIS.length;
105
      mNumFacesPerAxis = RubikCube.FACE_COLORS.length / mNumAxis;
106 beb325a0 Leszek Koltunski
      }
107
108 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// PUBLIC API
110 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 86c73a69 Leszek Koltunski
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
113 beb325a0 Leszek Koltunski
      {
114 93594b95 Leszek Koltunski
      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 86c73a69 Leszek Koltunski
118 93594b95 Leszek Koltunski
      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 beb325a0 Leszek Koltunski
122 9cd7695f Leszek Koltunski
      for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
123 beb325a0 Leszek Koltunski
        {
124 9cd7695f Leszek Koltunski
        for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
125 beb325a0 Leszek Koltunski
          {
126 9cd7695f Leszek Koltunski
          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 beb325a0 Leszek Koltunski
          }
135
        }
136
137 123d6172 Leszek Koltunski
      return false;
138 beb325a0 Leszek Koltunski
      }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 efef689c Leszek Koltunski
    public Static2D newRotation(Static4D rotatedTouchPoint)
143 beb325a0 Leszek Koltunski
      {
144 93594b95 Leszek Koltunski
      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 86c73a69 Leszek Koltunski
148 9cd7695f Leszek Koltunski
      castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
149 beb325a0 Leszek Koltunski
150 86c73a69 Leszek Koltunski
      mDiff[0] -= mTouch[0];
151
      mDiff[1] -= mTouch[1];
152
      mDiff[2] -= mTouch[2];
153 beb325a0 Leszek Koltunski
154 9cd7695f Leszek Koltunski
      int xAxis = retFaceXaxis(mLastTouchedAxis);
155
      int yAxis = retFaceYaxis(mLastTouchedAxis);
156 93594b95 Leszek Koltunski
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? xAxis : yAxis);
157
      float offset= mTouch[mRotationVect]+0.5f;
158 beb325a0 Leszek Koltunski
159 86c73a69 Leszek Koltunski
      mTouch[0] = mPoint[0];
160
      mTouch[1] = mPoint[1];
161
      mTouch[2] = mPoint[2];
162 beb325a0 Leszek Koltunski
163 efef689c Leszek Koltunski
      return new Static2D(mRotationVect,offset);
164 beb325a0 Leszek Koltunski
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 86c73a69 Leszek Koltunski
    public float continueRotation(Static4D rotatedTouchPoint)
169 beb325a0 Leszek Koltunski
      {
170 9cd7695f Leszek Koltunski
      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 beb325a0 Leszek Koltunski
174 9cd7695f Leszek Koltunski
      int xAxis= retFaceXaxis(mLastTouchedAxis);
175
      int yAxis= retFaceYaxis(mLastTouchedAxis);
176
      int sign = retFaceRotationSign(mLastTouchedAxis, mLastTouchedLR);
177 beb325a0 Leszek Koltunski
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
178
179 93594b95 Leszek Koltunski
      return sign*angle*0.5f;
180 beb325a0 Leszek Koltunski
      }
181
}