Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikDino.java @ f6d06256

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 android.content.res.Resources;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectRotate;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectDeform;
31
import org.distorted.library.effect.VertexEffectMove;
32
import org.distorted.library.effect.VertexEffectRotate;
33
import org.distorted.library.effect.VertexEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.mesh.MeshJoined;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.mesh.MeshTriangles;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikDino extends RubikObject
48
{
49
  private static final float SQ2 = (float)Math.sqrt(2);
50
  private static final float SQ3 = (float)Math.sqrt(3);
51
  private static final float ANGLE_FACES = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
52

    
53
  // the four rotation axis of a RubikDino. Must be normalized.
54
  static final Static3D[] AXIS = new Static3D[]
55
         {
56
           new Static3D(+SQ3/3,+SQ3/3,+SQ3/3),
57
           new Static3D(+SQ3/3,+SQ3/3,-SQ3/3),
58
           new Static3D(+SQ3/3,-SQ3/3,+SQ3/3),
59
           new Static3D(+SQ3/3,-SQ3/3,-SQ3/3)
60
         };
61

    
62
  private static final int[] FACE_COLORS = new int[]
63
         {
64
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
65
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
66
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
67
         };
68

    
69
  // All legal rotation quats of a RubikDino
70
  private static final Static4D[] QUATS = new Static4D[]
71
         {
72
           new Static4D(  0.0f,  0.0f,  0.0f,  1.0f ),
73
           new Static4D(  0.5f,  0.5f,  0.5f, -0.5f ),
74
           new Static4D(  0.0f,  0.0f,  1.0f,  0.0f ),
75
           new Static4D(  0.5f, -0.5f, -0.5f, -0.5f ),
76
           new Static4D(  0.5f,  0.5f,  0.5f,  0.5f ),
77
           new Static4D(  0.5f,  0.5f, -0.5f, -0.5f ),
78
           new Static4D(  0.5f, -0.5f,  0.5f, -0.5f ),
79
           new Static4D(  0.5f, -0.5f, -0.5f,  0.5f ),
80
           new Static4D(  0.0f,  1.0f,  0.0f,  0.0f ),
81
           new Static4D(  0.5f, -0.5f,  0.5f,  0.5f ),
82
           new Static4D(  1.0f,  0.0f,  0.0f,  0.0f ),
83
           new Static4D(  0.5f,  0.5f, -0.5f,  0.5f )
84
         };
85

    
86
  // centers of the 12 edges. Must be in the same order like QUATs above.
87
  private static final Static3D[] CENTERS = new Static3D[]
88
         {
89
           new Static3D( 0.0f, 0.5f, 0.5f ),
90
           new Static3D( 0.5f, 0.0f, 0.5f ),
91
           new Static3D( 0.0f,-0.5f, 0.5f ),
92
           new Static3D(-0.5f, 0.0f, 0.5f ),
93
           new Static3D( 0.5f, 0.5f, 0.0f ),
94
           new Static3D( 0.5f,-0.5f, 0.0f ),
95
           new Static3D(-0.5f,-0.5f, 0.0f ),
96
           new Static3D(-0.5f, 0.5f, 0.0f ),
97
           new Static3D( 0.0f, 0.5f,-0.5f ),
98
           new Static3D( 0.5f, 0.0f,-0.5f ),
99
           new Static3D( 0.0f,-0.5f,-0.5f ),
100
           new Static3D(-0.5f, 0.0f,-0.5f )
101
         };
102

    
103
  private static final int[] mFaceMap = {2,4, 4,0, 3,4, 4,1,
104
                                         0,2, 0,3, 1,3, 1,2,
105
                                         2,5, 5,0, 3,5, 5,1 };
106

    
107
  private static MeshBase mMesh;
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  RubikDino(int size, Static4D quat, DistortedTexture texture,
112
            MeshRectangles mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
113
    {
114
    super(size, 60, quat, texture, mesh, effects, moves, RubikObjectList.DINO, res, scrWidth);
115
    }
116

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

    
119
  private MeshBase createTetrahedronMesh()
120
    {
121
    final float SQ2 = (float)Math.sqrt(2);
122
    final float SQ3 = (float)Math.sqrt(3);
123
    final int MESHES=4;
124

    
125
    int association = 1;
126
    MeshBase[] meshes = new MeshTriangles[MESHES];
127

    
128
    meshes[0] = new MeshTriangles(11);
129
    meshes[0].setEffectAssociation(0,association,0);
130

    
131
    for(int i=1; i<MESHES; i++)
132
      {
133
      association <<= 1;
134
      meshes[i] = meshes[0].copy(true);
135
      meshes[i].setEffectAssociation(0,association,0);
136
      }
137

    
138
    MeshBase result = new MeshJoined(meshes);
139

    
140
    Static3D a0 = new Static3D(         0,        1,       0 );
141
    Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
142
    Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
143
    Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
144

    
145
    float tetraHeight = SQ2*SQ3/3;
146
    float d1 = 0.75f*tetraHeight;
147
    float d2 =-0.10f*tetraHeight;
148
    float d3 =-0.05f*tetraHeight;
149
    float d4 = 0.15f*tetraHeight;
150

    
151
    Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
152
    Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
153
    Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
154
    Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
155

    
156
    Static3D dVec0 = new Static3D( d3*a0.get0(), d3*a0.get1(), d3*a0.get2() );
157
    Static3D dVec1 = new Static3D( d3*a1.get0(), d3*a1.get1(), d3*a1.get2() );
158
    Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
159
    Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
160

    
161
    Static4D dReg  = new Static4D(0,0,0,d4);
162
    Static1D dRad  = new Static1D(1);
163

    
164
    Static1D angle  = new Static1D(ANGLE_FACES);
165
    Static3D axis1  = new Static3D(  -1, 0,      0);
166
    Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
167
    Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
168
    Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
169
    Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
170

    
171
    Static3D center = new Static3D(0,0,0);
172
    Static4D region = new Static4D(0,0,0,0.6f);
173

    
174
    VertexEffectScale   effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
175
    VertexEffectRotate  effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
176
    VertexEffectMove    effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
177
    VertexEffectRotate  effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
178
    VertexEffectRotate  effect5 = new VertexEffectRotate( angle, axis1, center1 );
179
    VertexEffectRotate  effect6 = new VertexEffectRotate( angle, axis2, center2 );
180
    VertexEffectRotate  effect7 = new VertexEffectRotate( angle, axis3, center2 );
181

    
182
    VertexEffectDeform  effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
183
    VertexEffectDeform  effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
184
    VertexEffectDeform  effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
185
    VertexEffectDeform  effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
186

    
187
    VertexEffectSink effect12= new VertexEffectSink( new Static1D(1.3f), center, region );
188

    
189
    effect4.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
190
    effect5.setMeshAssociation( 2,-1);  // apply only to mesh[1]
191
    effect6.setMeshAssociation( 4,-1);  // apply only to mesh[2]
192
    effect7.setMeshAssociation( 8,-1);  // apply only to mesh[3]
193

    
194
    result.apply(effect1);
195
    result.apply(effect2);
196
    result.apply(effect3);
197
    result.apply(effect4);
198
    result.apply(effect5);
199
    result.apply(effect6);
200
    result.apply(effect7);
201
    result.apply(effect8);
202
    result.apply(effect9);
203
    result.apply(effect10);
204
    result.apply(effect11);
205
    result.apply(effect12);
206

    
207
    result.mergeEffComponents();
208

    
209
    return result;
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  private void createBasicMesh()
215
    {
216
    mMesh = createTetrahedronMesh();
217

    
218
    Static3D axis = new Static3D(1,0,0);
219
    Static3D cent = new Static3D(0,0,0);
220

    
221
    MatrixEffectMove   moveEffect = new MatrixEffectMove  ( new Static3D(0.0f,SQ3*SQ2/12,SQ3/6) );
222
    MatrixEffectRotate rot1Effect = new MatrixEffectRotate( new Static1D(180+ANGLE_FACES/2), axis, cent);
223
    MatrixEffectScale  scalEffect = new MatrixEffectScale ( new Static3D(1.0f, SQ2/2, 0.5f) );
224
    MatrixEffectRotate rot2Effect = new MatrixEffectRotate( new Static1D(-45), axis, cent);
225

    
226
    mMesh.apply(moveEffect, 0xffffffff, 0);
227
    mMesh.apply(rot1Effect, 0xffffffff, 0);
228
    mMesh.apply(scalEffect, 0xffffffff, 0);
229
    mMesh.apply(rot2Effect, 0xffffffff, 0);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  float getScreenRatio()
235
    {
236
    return 1.5f;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  Static4D[] getQuats()
242
    {
243
    return QUATS;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  int getNumFaces()
249
    {
250
    return FACE_COLORS.length;
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  int getNumCubitFaces()
256
    {
257
    return 4;
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  Static3D[] getCubitPositions(int size)
263
    {
264
    return CENTERS;
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  MeshBase createCubitMesh(int cubit)
270
    {
271
    if( mMesh==null ) createBasicMesh();
272

    
273
    MeshBase mesh = mMesh.copy(true);
274
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( QUATS[cubit], new Static3D(0,0,0) );
275
    mesh.apply(quat,0xffffffff,0);
276

    
277
    return mesh;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  int getFaceColor(int cubit, int cubitface, int size)
283
    {
284
    switch(cubitface)
285
      {
286
      case 0 : return mFaceMap[2*cubit];
287
      case 1 : return mFaceMap[2*cubit+1];
288
      default: return NUM_FACES;
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
295
    {
296
    float STROKE = 0.06f*side;
297
    float OFF = STROKE/2 -1;
298
    float OFF2 = 0.5f*side + OFF;
299
    float HEIGHT = side - OFF;
300
    float RADIUS = side/12.0f;
301
    float ARC1_H = 0.2f*side;
302
    float ARC1_W = side*0.5f;
303
    float ARC2_W = 0.153f*side;
304
    float ARC2_H = 0.905f*side;
305
    float ARC3_W = side-ARC2_W;
306

    
307
    paint.setAntiAlias(true);
308
    paint.setStrokeWidth(STROKE);
309
    paint.setColor(FACE_COLORS[face]);
310
    paint.setStyle(Paint.Style.FILL);
311

    
312
    canvas.drawRect(left,top,left+side,top+side,paint);
313

    
314
    paint.setColor(INTERIOR_COLOR);
315
    paint.setStyle(Paint.Style.STROKE);
316

    
317
    canvas.drawLine(           left, HEIGHT,  side       +left, HEIGHT, paint);
318
    canvas.drawLine(      OFF +left, side  ,       OFF2  +left,      0, paint);
319
    canvas.drawLine((side-OFF)+left, side  , (side-OFF2) +left,      0, paint);
320

    
321
    canvas.drawArc( ARC1_W-RADIUS+left, ARC1_H-RADIUS, ARC1_W+RADIUS+left, ARC1_H+RADIUS, 225, 90, false, paint);
322
    canvas.drawArc( ARC2_W-RADIUS+left, ARC2_H-RADIUS, ARC2_W+RADIUS+left, ARC2_H+RADIUS, 105, 90, false, paint);
323
    canvas.drawArc( ARC3_W-RADIUS+left, ARC2_H-RADIUS, ARC3_W+RADIUS+left, ARC2_H+RADIUS, 345, 90, false, paint);
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// PUBLIC API
328

    
329
  public Static3D[] getRotationAxis()
330
    {
331
    return AXIS;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  public int getBasicAngle()
337
    {
338
    return 3;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  public float returnMultiplier()
344
    {
345
    // TODO
346

    
347
    return 0;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  public float returnRotationFactor(float offset)
353
    {
354
    return 1.0f;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  public float[] getRowChances()
360
    {
361
    int size = getSize();
362
    float[] chances = new float[size];
363

    
364
    for(int i=0; i<size; i++)
365
      {
366
      chances[i] = (i+1.0f) / size;
367
      }
368

    
369
    return chances;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
// TODO  (only needed for solvers - there are no Dino solvers ATM)
374

    
375
  public String retObjectString()
376
    {
377
    return "";
378
    }
379

    
380
}
(4-4/10)