Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControlRotate.java @ 7aa4c349

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.control;
21

    
22
import org.distorted.helpers.QuatHelper;
23
import org.distorted.library.effect.MatrixEffectScale;
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedNode;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshQuad;
29
import org.distorted.library.type.Dynamic;
30
import org.distorted.library.type.Dynamic3D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.objects.TwistyObject;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
class RubikControlRotate
38
  {
39
  private static final int NUM_SCREEN = 0;
40
  private static final int NUM_OBJECT = 1;
41

    
42
  private static Static4D INIT_QUAT;
43

    
44
  private final RubikControl mControl;
45
  private DistortedEffects[] mScreenEffects, mObjectEffects;
46
  private DistortedNode[] mScreenNodes, mObjectNodes;
47
  private long mScreenEffectID, mObjectEffectID;
48
  private Static4D mObjRotQuat;
49

    
50
  private Dynamic3D mDynamic;
51
  private MatrixEffectScale mScale;
52
  private MeshQuad mScreenQuad, mObjectQuad;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  private void computeInitQuat()
57
    {
58
    double alphaZ = -Math.PI* 0.1250f;
59
    double alphaY = -Math.PI* 0.0625f;
60

    
61
    alphaY /= 2;
62
    alphaZ /= 2;
63

    
64
    float sinZ = (float)Math.sin(alphaZ);
65
    float cosZ = (float)Math.cos(alphaZ);
66
    float sinY = (float)Math.sin(alphaY);
67
    float cosY = (float)Math.cos(alphaY);
68

    
69
    Static4D qZ = new Static4D(0,0,sinZ,cosZ);
70
    Static4D qY = new Static4D(0,sinY,0,cosY);
71

    
72
    INIT_QUAT = QuatHelper.quatMultiply(qY,qZ);
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// Take 3D vector 'ax', rotate it by quaternion 'objQuat' to get vector V1.
77
// Take 3D vector (1,0,0) and rotate it by INIT_QUAT to get vector V2.
78
// Return a quaternion Q such that if we rotate V1 by Q, we get V2.
79

    
80
  private Static4D computeQuat(Static3D ax, Static4D objQuat, float x, float y, float z)
81
    {
82
    Static4D ax4D = new Static4D( ax.get0(), ax.get1(), ax.get2(), 0);
83
    Static4D axRo = QuatHelper.rotateVectorByQuat(ax4D,objQuat);
84
    return QuatHelper.retRotationQuat(axRo.get0(),axRo.get1(),axRo.get2(),x,y,z);
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  private void computeRotQuat()
90
    {
91
    TwistyObject object = mControl.getObject();
92
    Static3D[] axis = object.getRotationAxis();
93
    int chosen=-1,numAxis = axis.length;
94
    float cos,maxCos = -1.0f;
95
    Static4D quat;
96

    
97
    Static4D objCurrQuat = mControl.getCurrQuat();
98
    Static4D axisX = new Static4D(1,0,0,0);
99
    Static4D rotAxisX = QuatHelper.rotateVectorByQuat(axisX,INIT_QUAT);
100

    
101
    float axX = rotAxisX.get0();
102
    float axY = rotAxisX.get1();
103
    float axZ = rotAxisX.get2();
104

    
105
    for (int a=0; a<numAxis; a++)
106
      {
107
      quat = computeQuat(axis[a],objCurrQuat,axX,axY,axZ);
108
      cos = quat.get3();
109

    
110
      if (cos > maxCos)
111
        {
112
        maxCos = cos;
113
        chosen = a;
114
        mObjRotQuat = quat;
115
        }
116
      }
117

    
118
    android.util.Log.e("D", "axis chosen: "+chosen);
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void setScreenEffectsStage1()
124
    {
125
    mDynamic.resetToBeginning();
126
    mScale.notifyWhenFinished(mControl);
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private void setObjectEffectsStage1()
132
    {
133
    mDynamic.resetToBeginning();
134
    mScale.notifyWhenFinished(mControl);
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void createScreenEffects()
140
    {
141
    mScreenEffects   = new DistortedEffects[NUM_SCREEN];
142
    mScreenEffects[0]= new DistortedEffects();
143

    
144
    DistortedScreen screen = mControl.getScreen();
145
    int wid = screen.getWidth();
146

    
147
    Static3D scaleStart= new Static3D(1,1,1);
148
    Static3D scaleEnd  = new Static3D(wid,wid,wid);
149

    
150
    mDynamic = new Dynamic3D(10000,0.5f);
151
    mDynamic.add(scaleStart);
152
    mDynamic.add(scaleEnd  );
153
    mDynamic.add(scaleStart);
154
    mDynamic.setMode(Dynamic.MODE_PATH);
155

    
156
    mScale = new MatrixEffectScale(mDynamic);
157
    mScreenEffectID = mScale.getID();
158
    mScreenEffects[0].apply(mScale);
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void createObjectEffects()
164
    {
165
    mObjectEffects   = new DistortedEffects[NUM_OBJECT];
166
    mObjectEffects[0]= new DistortedEffects();
167

    
168
    DistortedScreen screen = mControl.getScreen();
169
    int wid = screen.getWidth();
170

    
171
    Static3D scaleStart= new Static3D(1,1,1);
172
    Static3D scaleEnd  = new Static3D(wid,wid,wid);
173

    
174
    mDynamic = new Dynamic3D(10000,0.5f);
175
    mDynamic.add(scaleStart);
176
    mDynamic.add(scaleEnd  );
177
    mDynamic.add(scaleStart);
178
    mDynamic.setMode(Dynamic.MODE_PATH);
179

    
180
    mScale = new MatrixEffectScale(mDynamic);
181
    mObjectEffectID = mScale.getID();
182
    mObjectEffects[0].apply(mScale);
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  private void createScreenNodes()
188
    {
189
    if( mScreenNodes==null )
190
      {
191
      mScreenNodes= new DistortedNode[NUM_SCREEN];
192
      mScreenQuad = new MeshQuad();
193
      }
194

    
195
    DistortedTexture texture = new DistortedTexture();
196
    texture.setColorARGB(0xff00ff00);
197
    mScreenNodes[0] = new DistortedNode(texture, mScreenEffects[0], mScreenQuad);
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void createObjectNodes()
203
    {
204
    if( mObjectNodes==null )
205
      {
206
      mObjectNodes= new DistortedNode[NUM_OBJECT];
207
      mObjectQuad = new MeshQuad();
208
      }
209

    
210
    if( INIT_QUAT==null ) computeInitQuat();
211

    
212
    computeRotQuat();
213

    
214
    DistortedTexture texture = new DistortedTexture();
215
    texture.setColorARGB(0xff00ff00);
216
    mObjectNodes[0] = new DistortedNode(texture, mObjectEffects[0], mObjectQuad);
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
// ??
221

    
222
  long getEffectID()
223
    {
224
    return mObjectEffectID;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  long getScreenEffectID()
230
    {
231
    return mScreenEffectID;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  long getObjectEffectID()
237
    {
238
    return mObjectEffectID;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  DistortedNode[] getScreenNodes()
244
    {
245
    if( NUM_SCREEN>0 )
246
      {
247
      if( mScreenEffects==null ) createScreenEffects();
248
      createScreenNodes();
249
      setScreenEffectsStage1();
250
      }
251

    
252
    return mScreenNodes;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  DistortedNode[] returnScreenNodes()
258
    {
259
    return mScreenNodes;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
   DistortedNode[] getObjectNodes()
265
    {
266
    if( NUM_OBJECT>0 )
267
      {
268
      if( mObjectEffects==null ) createObjectEffects();
269
      createObjectNodes();
270
      setObjectEffectsStage1();
271
      }
272

    
273
    return mObjectNodes;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
   DistortedNode[] returnObjectNodes()
279
    {
280
    return mObjectNodes;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  RubikControlRotate(RubikControl control)
286
    {
287
    mControl = control;
288
    }
289
  }
(2-2/3)