Project

General

Profile

« Previous | Next » 

Revision 05fa94d9

Added by Leszek Koltunski over 4 years ago

RubikCube: detect if the cube is solved.

In order to do this correctly, we also needed to keep correcting each mQuatScramble quaternions after each quatMultiplication in order to avoid multiplication errors accumulating. This turns out to be easy, because each quaternion representing a legal combination of rotations of a RubikCube must have each of the 4 to its components be equal to one of only 7 possible floats.

View differences:

src/main/java/org/distorted/magic/RubikRenderer.java
47 47
    private int mNextCubeSize, mScrambleCubeNum;
48 48
    private long mRotationFinishedID;
49 49
    private long[] mEffectID;
50
    private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
50
    private boolean mFinishRotation, mRemoveRotation, mSetQuatCurrent, mSetQuatAccumulated;
51 51
    private boolean mSizeChangeCube, mSolveCube, mScrambleCube;
52 52
    private boolean mCanRotate, mCanDrag, mCanUI;
53 53
    private RubikCube mOldCube, mNewCube;
......
68 68
      mScreenWidth = mScreenHeight = 0;
69 69
      mScrambleCubeNum = 0;
70 70

  
71
      mFinishRotation        = false;
72
      mRemoveRotation        = false;
73
      mFinishDragCurrent     = false;
74
      mFinishDragAccumulated = false;
75
      mSizeChangeCube        = false;
76
      mSolveCube             = false;
77
      mScrambleCube          = false;
71
      mFinishRotation     = false;
72
      mRemoveRotation     = false;
73
      mSetQuatCurrent     = false;
74
      mSetQuatAccumulated = false;
75
      mSizeChangeCube     = true;
76
      mSolveCube          = false;
77
      mScrambleCube       = false;
78 78

  
79 79
      mCanRotate   = true;
80 80
      mCanDrag     = true;
......
83 83
      mEffectID = new long[BaseEffect.Type.LENGTH];
84 84

  
85 85
      mMesh= new MeshFlat(20,20);
86
      mNextCubeSize =RubikActivity.getSize();
86
      mNextCubeSize = RubikActivity.getSize();
87 87
      }
88 88

  
89 89
///////////////////////////////////////////////////////////////////////////////////////////////////
......
95 95
      {
96 96
      mScreen.render( System.currentTimeMillis() );
97 97

  
98
      if( mFinishDragCurrent )
98
      if( mSetQuatCurrent )
99 99
        {
100
        mFinishDragCurrent = false;
100
        mSetQuatCurrent = false;
101 101
        mView.setQuatCurrent();
102 102
        }
103 103

  
104
      if( mFinishDragAccumulated )
104
      if( mSetQuatAccumulated )
105 105
        {
106
        mFinishDragAccumulated = false;
106
        mSetQuatAccumulated = false;
107 107
        mView.setQuatAccumulated();
108 108
        }
109 109

  
......
118 118
        {
119 119
        mRemoveRotation=false;
120 120
        mNewCube.removeRotationNow();
121

  
122
        if( mNewCube.isSolved() )
123
          {
124
          android.util.Log.e("renderer", "CUBE IS SOLVED NOW");
125
          }
126

  
121 127
        mCanRotate = true;
122 128
        }
123 129

  
......
127 133
        mCanDrag        = false;
128 134
        mCanRotate      = false;
129 135
        createCubeNow(mNextCubeSize);
130
        doEffectNow(0);
136
        doEffectNow( BaseEffect.Type.SIZECHANGE );
131 137
        }
132 138

  
133 139
      if( mSolveCube )
......
136 142
        mCanDrag     = false;
137 143
        mCanRotate   = false;
138 144
        mCanUI       = false;
139
        doEffectNow(1);
145
        doEffectNow( BaseEffect.Type.SOLVE );
140 146
        }
141 147

  
142 148
      if( mScrambleCube )
......
145 151
        mCanDrag      = false;
146 152
        mCanRotate    = false;
147 153
        mCanUI        = false;
148
        doEffectNow(2);
154
        doEffectNow( BaseEffect.Type.SCRAMBLE );
149 155
        }
150 156
      }
151 157

  
......
296 302
///////////////////////////////////////////////////////////////////////////////////////////////////
297 303
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
298 304

  
299
   private void doEffectNow(int index)
305
   private void doEffectNow(BaseEffect.Type type)
300 306
     {
301
     mEffectID[index] = BaseEffect.Type.getType(index).startEffect(this);
307
     int index = type.ordinal();
308

  
309
     mEffectID[index] = type.startEffect(this);
302 310

  
303 311
     if( mEffectID[index] == -1 )
304 312
       {
......
361 369

  
362 370
   void setQuatCurrentOnNextRender()
363 371
     {
364
     mFinishDragCurrent = true;
372
     mSetQuatCurrent = true;
365 373
     }
366 374

  
367 375
///////////////////////////////////////////////////////////////////////////////////////////////////
368 376

  
369 377
   void setQuatAccumulatedOnNextRender()
370 378
     {
371
     mFinishDragAccumulated = true;
379
     mSetQuatAccumulated = true;
372 380
     }
373 381
}

Also available in: Unified diff