Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / MovementMinx.java @ 387b6326

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.objects;
21

    
22
import org.distorted.library.type.Static3D;
23
import static org.distorted.objects.TwistyMinx.C2;
24
import static org.distorted.objects.TwistyMinx.LEN;
25
import static org.distorted.objects.TwistyMinx.SIN54;
26
import static org.distorted.objects.TwistyMinx.COS54;
27
import static org.distorted.objects.TwistyObject.SQ5;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class MovementMinx extends Movement
32
{
33
  static final float DIST3D = (float)Math.sqrt(0.625f+0.275f*SQ5);
34
  static final float DIST2D = (SIN54/COS54)/2;
35

    
36
  static final Static3D[] FACE_AXIS = new Static3D[]
37
         {
38
           new Static3D(    C2/LEN, SIN54/LEN,    0      ),
39
           new Static3D(    C2/LEN,-SIN54/LEN,    0      ),
40
           new Static3D(   -C2/LEN, SIN54/LEN,    0      ),
41
           new Static3D(   -C2/LEN,-SIN54/LEN,    0      ),
42
           new Static3D( 0        ,    C2/LEN, SIN54/LEN ),
43
           new Static3D( 0        ,    C2/LEN,-SIN54/LEN ),
44
           new Static3D( 0        ,   -C2/LEN, SIN54/LEN ),
45
           new Static3D( 0        ,   -C2/LEN,-SIN54/LEN ),
46
           new Static3D( SIN54/LEN,    0     ,    C2/LEN ),
47
           new Static3D( SIN54/LEN,    0     ,   -C2/LEN ),
48
           new Static3D(-SIN54/LEN,    0     ,    C2/LEN ),
49
           new Static3D(-SIN54/LEN,    0     ,   -C2/LEN )
50
         };
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  MovementMinx()
55
    {
56
    super(TwistyMinx.ROT_AXIS, FACE_AXIS, DIST3D, DIST2D);
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  int computeRowFromOffset(int face, int axisIndex, int numLayers, float offset)
62
    {
63
    if( numLayers==3 )
64
      {
65
      return offset<DIST2D ? 0:2;
66
      }
67
    if( numLayers==5 )
68
      {
69
      float quot = offset / DIST2D;
70

    
71
      if( quot>0.00f && quot<=0.34f ) return 0;
72
      if( quot>0.34f && quot<=1.00f ) return 1;
73
      if( quot>1.00f && quot<=1.66f ) return 3;
74
      if( quot>1.66f && quot<=2.00f ) return 4;
75
      }
76

    
77
    return 0;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  public float returnRotationFactor(int numLayers, int row)
83
    {
84
    return 1.0f;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// return angle (in radians) that the line connecting the center C of the pentagonal face and the
89
// first vertex of the pentagon makes with a vertical line coming upwards from the center C.
90

    
91
  private float returnAngle(int face)
92
    {
93
    switch(face)
94
      {
95
      case  0:
96
      case  2:
97
      case  6:
98
      case  7: return 0.0f;
99
      case  1:
100
      case  3:
101
      case  4:
102
      case  5: return (float)(36*Math.PI/180);
103
      case  9:
104
      case 10: return (float)(54*Math.PI/180);
105
      case  8:
106
      case 11: return (float)(18*Math.PI/180);
107
      }
108

    
109
    return 0.0f;
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
// The pair (distance,angle) defines a point P in R^2 in polar coordinate system. Let V be the vector
114
// from the center of the coordinate system to P.
115
// Let P' be the point defined by polar (distance,angle+PI/2). Let Lh be the half-line starting at
116
// P' and going in the direction of V.
117
// Return true iff point 'point' lies on the left of Lh, i.e. when we rotate (using the center of
118
// the coordinate system as the center of rotation) 'point' and Lh in such a way that Lh points
119
// directly upwards, is 'point' on the left or the right of it?
120

    
121
  private boolean isOnTheLeft(float[] point, float distance, float angle)
122
    {
123
    float sin = (float)Math.sin(angle);
124
    float cos = (float)Math.cos(angle);
125

    
126
    float vx = point[0] + sin*distance;
127
    float vy = point[1] - cos*distance;
128

    
129
    return vx*sin < vy*cos;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// Return 1,2,3,4,5 - the vertex of the pentagon to which point 'point' is the closest, if the
134
// 'point' is inside the pentagon - or 0 otherwise.
135
// The 'first' vertex is the one we meet the first when we rotate clockwise starting from 12:00.
136
// This vertex makes angle 'returnAngle()' with the line coming out upwards from the center of the
137
// pentagon.
138
// Distance from the center to a vertex of the pentagon = 1/(6*COS54)
139

    
140
  private int returnPartOfThePentagon(float[] point, int face)
141
    {
142
    float angle = returnAngle(face);
143
    float A = (float)(Math.PI/5);
144

    
145
    for(int i=0; i<5; i++)
146
      {
147
      if( isOnTheLeft(point, DIST2D, (9-2*i)*A-angle) ) return 0;
148
      }
149

    
150
    if( isOnTheLeft(point, 0, 2.5f*A-angle) )
151
      {
152
      if( isOnTheLeft(point, 0, 3.5f*A-angle) )
153
        {
154
        return isOnTheLeft(point, 0, 5.5f*A-angle) ? 4 : 5;
155
        }
156
      else return 1;
157
      }
158
    else
159
      {
160
      if( isOnTheLeft(point, 0, 4.5f*A-angle) )
161
        {
162
        return 3;
163
        }
164
      else
165
        {
166
        return isOnTheLeft(point, 0, 6.5f*A-angle) ? 2 : 1;
167
        }
168
      }
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  boolean isInsideFace(int face, float[] p)
174
    {
175
    return returnPartOfThePentagon(p,face) > 0;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
181
    {
182
    int part = returnPartOfThePentagon(touchPoint,face);
183

    
184
    if( part>0 )
185
      {
186
      enabled[0] = 2;
187

    
188
      switch(face)
189
        {
190
        case  0:  switch(part)
191
                    {
192
                    case 1: enabled[1]=2; enabled[2]=3; break;
193
                    case 2: enabled[1]=3; enabled[2]=5; break;
194
                    case 3: enabled[1]=5; enabled[2]=1; break;
195
                    case 4: enabled[1]=1; enabled[2]=4; break;
196
                    case 5: enabled[1]=4; enabled[2]=2; break;
197
                    }
198
                  break;
199

    
200
        case  1:  switch(part)
201
                    {
202
                    case 1: enabled[1]=0; enabled[2]=5; break;
203
                    case 2: enabled[1]=5; enabled[2]=2; break;
204
                    case 3: enabled[1]=2; enabled[2]=3; break;
205
                    case 4: enabled[1]=3; enabled[2]=4; break;
206
                    case 5: enabled[1]=4; enabled[2]=0; break;
207
                    }
208
                  break;
209

    
210
        case  2:  switch(part)
211
                    {
212
                    case 1: enabled[1]=3; enabled[2]=2; break;
213
                    case 2: enabled[1]=2; enabled[2]=5; break;
214
                    case 3: enabled[1]=5; enabled[2]=0; break;
215
                    case 4: enabled[1]=0; enabled[2]=4; break;
216
                    case 5: enabled[1]=4; enabled[2]=3; break;
217
                    }
218
                  break;
219

    
220
        case  3:  switch(part)
221
                    {
222
                    case 1: enabled[1]=1; enabled[2]=5; break;
223
                    case 2: enabled[1]=5; enabled[2]=3; break;
224
                    case 3: enabled[1]=3; enabled[2]=2; break;
225
                    case 4: enabled[1]=2; enabled[2]=4; break;
226
                    case 5: enabled[1]=4; enabled[2]=1; break;
227
                    }
228
                  break;
229

    
230
        case  4:  switch(part)
231
                    {
232
                    case 1: enabled[1]=3; enabled[2]=0; break;
233
                    case 2: enabled[1]=0; enabled[2]=4; break;
234
                    case 3: enabled[1]=4; enabled[2]=5; break;
235
                    case 4: enabled[1]=5; enabled[2]=1; break;
236
                    case 5: enabled[1]=1; enabled[2]=3; break;
237
                    }
238
                  break;
239

    
240
        case  5:  switch(part)
241
                    {
242
                    case 1: enabled[1]=2; enabled[2]=1; break;
243
                    case 2: enabled[1]=1; enabled[2]=4; break;
244
                    case 3: enabled[1]=4; enabled[2]=5; break;
245
                    case 4: enabled[1]=5; enabled[2]=0; break;
246
                    case 5: enabled[1]=0; enabled[2]=2; break;
247
                    }
248
                  break;
249

    
250
        case  6:  switch(part)
251
                    {
252
                    case 1: enabled[1]=5; enabled[2]=4; break;
253
                    case 2: enabled[1]=4; enabled[2]=1; break;
254
                    case 3: enabled[1]=1; enabled[2]=2; break;
255
                    case 4: enabled[1]=2; enabled[2]=0; break;
256
                    case 5: enabled[1]=0; enabled[2]=5; break;
257
                    }
258
                  break;
259

    
260
        case  7:  switch(part)
261
                    {
262
                    case 1: enabled[1]=5; enabled[2]=4; break;
263
                    case 2: enabled[1]=4; enabled[2]=0; break;
264
                    case 3: enabled[1]=0; enabled[2]=3; break;
265
                    case 4: enabled[1]=3; enabled[2]=1; break;
266
                    case 5: enabled[1]=1; enabled[2]=5; break;
267
                    }
268
                  break;
269

    
270
        case  8: switch(part)
271
                    {
272
                    case 1: enabled[1]=2; enabled[2]=0; break;
273
                    case 2: enabled[1]=0; enabled[2]=1; break;
274
                    case 3: enabled[1]=1; enabled[2]=3; break;
275
                    case 4: enabled[1]=3; enabled[2]=5; break;
276
                    case 5: enabled[1]=5; enabled[2]=2; break;
277
                    }
278
                  break;
279

    
280
        case  9:  switch(part)
281
                    {
282
                    case 1: enabled[1]=3; enabled[2]=4; break;
283
                    case 2: enabled[1]=4; enabled[2]=2; break;
284
                    case 3: enabled[1]=2; enabled[2]=1; break;
285
                    case 4: enabled[1]=1; enabled[2]=0; break;
286
                    case 5: enabled[1]=0; enabled[2]=3; break;
287
                    }
288
                  break;
289

    
290
        case 10:  switch(part)
291
                    {
292
                    case 1: enabled[1]=2; enabled[2]=4; break;
293
                    case 2: enabled[1]=4; enabled[2]=3; break;
294
                    case 3: enabled[1]=3; enabled[2]=0; break;
295
                    case 4: enabled[1]=0; enabled[2]=1; break;
296
                    case 5: enabled[1]=1; enabled[2]=2; break;
297
                    }
298
                  break;
299

    
300
        case 11:  switch(part)
301
                    {
302
                    case 1: enabled[1]=3; enabled[2]=1; break;
303
                    case 2: enabled[1]=1; enabled[2]=0; break;
304
                    case 3: enabled[1]=0; enabled[2]=2; break;
305
                    case 4: enabled[1]=2; enabled[2]=5; break;
306
                    case 5: enabled[1]=5; enabled[2]=3; break;
307
                    }
308
                  break;
309
        }
310
      }
311
    else
312
      {
313
      enabled[0] = 0;
314
      }
315
    }
316
}
(9-9/43)