aboutsummaryrefslogtreecommitdiff
path: root/anim.js
blob: 341c65416b13f6f9f885205dcf6531a463e04cb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict";

var animations = new Map();

animations.set("pitchA",
    {
        name:           "pitch",
        cubies:         [4,5,1,0],
        vec:            new THREE.Vector3(1.0,0.0,0.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

animations.set("pitchB",
    {
        name:           "pitch",
        cubies:         [7,6,2,3],
        vec:            new THREE.Vector3(1.0,0.0,0.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

animations.set("rollA",
    {
        name:           "roll",
        cubies:         [3,7,4,0],
        vec:            new THREE.Vector3(0.0,0.0,1.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

animations.set("rollB",
    {
        name:           "roll",
        cubies:         [6,5,1,2],
        vec:            new THREE.Vector3(0.0,0.0,1.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

animations.set("yawA",
    {
        name:           "yaw",
        cubies:         [0,1,2,3],
        vec:            new THREE.Vector3(0.0,1.0,0.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

animations.set("yawB",
    {
        name:           "yaw",
        cubies:         [4,5,6,7],
        vec:            new THREE.Vector3(0.0,1.0,0.0),
        dir:            1,
        func:           function(x){ return Math.sin(x); },
        max_rotation:   Math.PI/2,
    });

function startAnimation(a,d)
{
    if(!animation)
    {   
        current_animation = animations.get(a);
        current_animation.dir = d;

        x = 0.0;
        animation = true;
        face = new THREE.Object3D();

        var temp = new THREE.Vector3();

        q = new THREE.Quaternion();
        q_init = new THREE.Quaternion();
        temp.copy(current_animation.vec);
        q_init.setFromAxisAngle(temp.multiplyScalar(current_animation.dir).normalize(),0.0);
        q_end = new THREE.Quaternion();
        temp.copy(current_animation.vec);
        q_end.setFromAxisAngle(temp.multiplyScalar(current_animation.dir).normalize(),current_animation.dir*current_animation.max_rotation);

        for(var i = 0; i < 4; i++)
        {
            scene.remove(cubies.get(current_animation.cubies[i]).mesh);   
            face.add(cubies.get(current_animation.cubies[i]).mesh);
        }

        scene.add(face);
    }
}

Animator.prototype.goOn = function()
{
    startAnimation(this.moves[this.index].move, this.moves[this.index].dir);
    if(! scramb)
    {
        info.innerHTML = "N. of moves: " + (this.index+1);
    }
    if(++this.index == this.moves.length)
    {
        animator = null;
        removeCounter = 5.0;
        if(scramb)
        {
            scramb = false;
        }
    }
};

function Animator(moves)
{
    this.index = 0;
    this.moves = moves;
}