Project

General

Profile

« Previous | Next » 

Revision dd65ead3

Added by Leszek Koltunski about 4 years ago

Big progress with generalizing the Movement classes.

View differences:

src/main/java/org/distorted/object/RubikCubeMovement.java
19 19

  
20 20
package org.distorted.object;
21 21

  
22
import org.distorted.library.type.Static2D;
23
import org.distorted.library.type.Static4D;
24

  
25 22
///////////////////////////////////////////////////////////////////////////////////////////////////
26 23

  
27 24
class RubikCubeMovement extends RubikObjectMovement
28 25
{
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
      }
26
  RubikCubeMovement()
27
    {
28
    super(3,2);
29
    }
58 30

  
59 31
///////////////////////////////////////////////////////////////////////////////////////////////////
60 32

  
61
    private int retFaceXaxis(int axis)
62
      {
63
      return axis==0 ? VECTZ : VECTX;
64
      }
33
  private boolean isVertical(float x, float y)
34
    {
35
    return (y>x) ? (y>=-x) : (y< -x);
36
    }
65 37

  
66 38
///////////////////////////////////////////////////////////////////////////////////////////////////
67 39

  
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
      }
40
  boolean faceIsVisible(int axis, int lr)
41
    {
42
    return (2*lr-1)*mCamera[axis] > 0.5f;
43
    }
79 44

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

  
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;
50
  void castTouchPointOntoFace(int axis, int lr, float[] output)
51
    {
52
    float diff = mPoint[axis]-mCamera[axis];
53
    float ratio= diff!=0.0f ? ((lr-0.5f)-mCamera[axis])/diff : 0.0f;
89 54

  
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
      }
55
    output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
56
    output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
57
    output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
58
    }
94 59

  
95 60
///////////////////////////////////////////////////////////////////////////////////////////////////
96 61

  
97
    RubikCubeMovement()
62
  void fillPossibleRotations(int axis, int[] output)
63
    {
64
    switch(axis)
98 65
      {
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;
66
      case 0: output[0]=2; output[1]=1; break; // (Z,Y) when looking at LEFT or RIGHT
67
      case 1: output[0]=0; output[1]=2; break; // (X,Z) when looking at BOTTOM or TOP
68
      case 2: output[0]=0; output[1]=1; break; // (X,Y) when looking at FRONT or BACK
106 69
      }
70
    }
107 71

  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// PUBLIC API
110 72
///////////////////////////////////////////////////////////////////////////////////////////////////
111 73

  
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
      }
74
  boolean isInsideFace(float[] p)
75
    {
76
    return ( p[0]<=0.5f && p[0]>=-0.5f && p[1]<=0.5f && p[1]>=-0.5f && p[2]<=0.5f && p[2]>=-0.5f );
77
    }
139 78

  
140 79
///////////////////////////////////////////////////////////////////////////////////////////////////
141 80

  
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
      }
81
  float fillUpRotationVectAndOffset(float[] v, int[] possible)
82
    {
83
    mRotationVect = isVertical(v[possible[0]],v[possible[1]]) ? possible[0] : possible[1];
84
    return mTouch[mRotationVect]+0.5f;
85
    }
165 86

  
166 87
///////////////////////////////////////////////////////////////////////////////////////////////////
167 88

  
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
      }
89
  float returnAngle(float[] v, int[] possible)
90
    {
91
    float angle= (mRotationVect==possible[0] ? v[possible[1]] : -v[possible[0]]);
92
    if( mLastTouchedAxis==2 ) angle = -angle;
93
    return angle;
94
    }
181 95
}

Also available in: Unified diff