Project

General

Profile

Download (5.25 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / solve / SolveEffectSpin.java @ a5a52e8d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.objectlib.effects.solve;
21

    
22
import org.distorted.library.effect.Effect;
23
import org.distorted.library.effect.MatrixEffectRotate;
24
import org.distorted.library.type.Dynamic;
25
import org.distorted.library.type.Dynamic1D;
26
import org.distorted.library.type.Static1D;
27
import org.distorted.library.type.Static3D;
28
import org.distorted.library.type.Static4D;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public class SolveEffectSpin extends SolveEffect
33
  {
34
  private static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
35
    {
36
    float qx = quat1.get0();
37
    float qy = quat1.get1();
38
    float qz = quat1.get2();
39
    float qw = quat1.get3();
40

    
41
    float rx = quat2.get0();
42
    float ry = quat2.get1();
43
    float rz = quat2.get2();
44
    float rw = quat2.get3();
45

    
46
    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
47
    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
48
    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
49
    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
50

    
51
    return new Static4D(tx,ty,tz,tw);
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
56

    
57
  private static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
58
    {
59
    float qx = quat.get0();
60
    float qy = quat.get1();
61
    float qz = quat.get2();
62
    float qw = quat.get3();
63

    
64
    Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
65
    Static4D tmp = quatMultiply(quatInverted,vector);
66

    
67
    return quatMultiply(tmp,quat);
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  private void createEffects(int phase, int duration, int[] points)
73
    {
74
    mCubeEffectPosition[phase] = new int[] {0};
75
    mCubeEffects[phase]        = new Effect[mCubeEffectPosition[0].length];
76

    
77
    Static4D quaternion = mObject.getRotationQuat();                      // always rotate around
78
    Static4D tmpAxis    = new Static4D(0,1,0,0);                          // vert axis no matter
79
    Static4D rotated    = rotateVectorByInvertedQuat(tmpAxis,quaternion); // how cube is rotated
80

    
81
    Static3D axis  = new Static3D(rotated.get0(), rotated.get1(), rotated.get2());
82
    Static3D center= new Static3D(0,0,0);
83

    
84
    Dynamic1D d = new Dynamic1D(duration/2, 1.0f);
85
    d.setMode(Dynamic.MODE_JUMP);
86
    d.setConvexity(0.0f);   // otherwise speed of the rotation would be strangely uneven
87

    
88
    d.add( new Static1D(36*points[0]) );
89
    d.add( new Static1D(36*points[1]) );
90
    d.add( new Static1D(36*points[2]) );
91
    d.add( new Static1D(36*points[3]) );
92
    d.add( new Static1D(36*points[4]) );
93

    
94
    mCubeEffects[phase][0] = new MatrixEffectRotate(d,axis,center);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// PUBLIC API
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  public void createEffectsPhase0(int duration)
102
    {
103
    createEffects(0,duration,new int[] {0,1,3,6,10});
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  public void createEffectsPhase1(int duration)
109
    {
110
    createEffects(1,duration,new int[] {0,4,7,9,10});
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
// Enable all effects used in this Effect. Called by reflection from the parent class.
115

    
116
  @SuppressWarnings("unused")
117
  static void enable()
118
    {
119

    
120
    }
121
  }
(3-3/3)