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
}
src/main/java/org/distorted/object/RubikObjectMovement.java
26 26

  
27 27
public abstract class RubikObjectMovement
28 28
  {
29
  public abstract boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera);
30
  public abstract Static2D newRotation(Static4D rotatedTouchPoint);
31
  public abstract float continueRotation(Static4D rotatedTouchPoint);
29
  float[] mPoint, mCamera, mTouch;
30
  int mRotationVect, mLastTouchedAxis;
31

  
32
  private float[] mDiff;
33
  private int mLastTouchedLR;
34
  private int mNumAxis, mNumFacesPerAxis;
35
  private int[] mPossible;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
  abstract boolean faceIsVisible(int axis, int lr);
40
  abstract void castTouchPointOntoFace(int axis, int lr, float[] output);
41
  abstract boolean isInsideFace(float[] point);
42
  abstract void fillPossibleRotations(int axis, int[] output);
43
  abstract float fillUpRotationVectAndOffset(float[] vect, int[] possible);
44
  abstract float returnAngle(float[] vect, int[] possible);
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  RubikObjectMovement(int numAxis, int numFacesPerAxis)
49
    {
50
    mPoint = new float[3];
51
    mCamera= new float[3];
52
    mDiff  = new float[3];
53
    mTouch = new float[3];
54

  
55
    mNumAxis = numAxis;
56
    mNumFacesPerAxis = numFacesPerAxis;
57
    mPossible = new int[mNumAxis-1];
58
    }
59

  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
65
    {
66
    mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
67
    mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
68
    mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
69

  
70
    mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
71
    mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
72
    mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
73

  
74
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
75
      {
76
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
77
        {
78
        if( faceIsVisible(mLastTouchedAxis, mLastTouchedLR) )
79
          {
80
          castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mTouch);
81

  
82
          if( isInsideFace(mTouch) )
83
            {
84
            fillPossibleRotations(mLastTouchedAxis, mPossible);
85
            return true;
86
            }
87
          }
88
        }
89
      }
90

  
91
    return false;
92
    }
93

  
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

  
96
  public Static2D newRotation(Static4D rotatedTouchPoint)
97
    {
98
    mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
99
    mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
100
    mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
101

  
102
    castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
103

  
104
    mDiff[0] -= mTouch[0];
105
    mDiff[1] -= mTouch[1];
106
    mDiff[2] -= mTouch[2];
107

  
108
    float offset = fillUpRotationVectAndOffset(mDiff, mPossible);
109

  
110
    mTouch[0] = mPoint[0];
111
    mTouch[1] = mPoint[1];
112
    mTouch[2] = mPoint[2];
113

  
114
    return new Static2D(mRotationVect,offset);
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  public float continueRotation(Static4D rotatedTouchPoint)
120
    {
121
    mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[0];
122
    mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[1];
123
    mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[2];
124

  
125
    return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible);
126
    }
32 127
  }
src/main/java/org/distorted/object/RubikPyraminxMovement.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 RubikPyraminxMovement extends RubikObjectMovement
28 25
{
26
  private static final float SQ2 = (float)Math.sqrt(2);
27
  private static final float SQ3 = (float)Math.sqrt(3);
29 28

  
30 29
///////////////////////////////////////////////////////////////////////////////////////////////////
31 30

  
32
    RubikPyraminxMovement()
33
      {
31
  RubikPyraminxMovement()
32
    {
33
    super(4,1);
34
    }
34 35

  
35
      }
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
  boolean faceIsVisible(int axis, int lr)
39
    {
40
    return mCamera[axis] < -SQ2*SQ3/12;
41
    }
36 42

  
37 43
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// PUBLIC API
44
// TODO
45

  
46
  void castTouchPointOntoFace(int axis, int lr, float[] output)
47
    {
48
    /*
49
    float diff = mPoint[axis]-mCamera[axis];
50
    float ratio= diff!=0.0f ? ((lr-0.5f)-mCamera[axis])/diff : 0.0f;
51

  
52
    output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
53
    output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
54
    output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
55
    */
56
    }
57

  
39 58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// (         0,        1,       0 )  BOTTOM
60
// (         0,  -1.0f/3, 2*SQ2/3 )  BACK
61
// (-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 )  LEFT
62
// ( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 )  RIGHT
40 63

  
41
    public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
64
  void fillPossibleRotations(int axis, int[] output)
65
    {
66
    switch(axis)
42 67
      {
43
      return false;
68
      case 0: output[0]=3; output[1]=1; output[2]=2; break; // (RIGHT,BACK,LEFT) when looking at BOTTOM
69
      case 1: output[0]=2; output[1]=0; output[2]=3; break; // (LEFT,BOTTOM,RIGHT) when looking at BACK
70
      case 2: output[0]=3; output[1]=0; output[2]=1; break; // (RIGHT,BOTTOM,BACK) when looking at LEFT
71
      case 3: output[0]=1; output[1]=0; output[2]=2; break; // (BACK,BOTTOM,LEFT) when looking at RIGHT
44 72
      }
73
    }
45 74

  
46 75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// TODO
47 77

  
48
    public Static2D newRotation(Static4D rotatedTouchPoint)
49
      {
50
      return null;
51
      }
78
  boolean isInsideFace(float[] p)
79
    {
80
    return false;//( p[0]<=0.5f && p[0]>=-0.5f && p[1]<=0.5f && p[1]>=-0.5f && p[2]<=0.5f && p[2]>=-0.5f );
81
    }
52 82

  
53 83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// TODO
54 85

  
55
    public float continueRotation(Static4D rotatedTouchPoint)
56
      {
57
      return 0.0f;
58
      }
86
  float fillUpRotationVectAndOffset(float[] v, int[] possible)
87
    {
88
    //mRotationVect = isVertical(v[possible[0]],v[possible[1]]) ? possible[0] : possible[1];
89
    //return mTouch[mRotationVect]+0.5f;
90

  
91
    return 0;
92
    }
93

  
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// TODO
96

  
97
  float returnAngle(float[] v, int[] possible)
98
    {
99
    /*
100
    float angle= (mRotationVect==possible[0] ? v[possible[1]] : -v[possible[0]]);
101
    if( mLastTouchedAxis==2 ) angle = -angle;
102
    return angle;
103
     */
104

  
105
    return 0;
106
    }
59 107
}

Also available in: Unified diff