-
Notifications
You must be signed in to change notification settings - Fork 7
/
domino.js
184 lines (145 loc) · 5.26 KB
/
domino.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var Consts = require('./consts.js');
var Globals = require('./globals.js');
var THREE = require('three');
var Utils = require('./utils.js');
var TWEEN = require('tween');
function Domino(position, angle, color, collisionHandler, Physijs) {
this.rotation = new THREE.Vector3(0,0,0);
this.angle = 0;
this.color = color || Consts.domino_base_color;
this.mesh;
var block_material = Physijs.createMaterial(
new THREE.MeshLambertMaterial(
{color: this.color, vertexColors: THREE.FaceColors}),
0.4, // medium friction
0.2 // medium restitution ("bounciness")
);
var block_geometry = new THREE.CubeGeometry(Consts.block_thick, Consts.block_height,Consts.block_width);
this.mesh = new Physijs.BoxMesh(block_geometry, block_material);
this.move(position);
if(angle) {
this.turn(angle);
}
this.mesh.__dirtyPosition = true;
this.position.y = 1.2 + Consts.block_height/2;
this.mesh.position.copy(this.position);
this.mesh.setCcdMotionThreshold(0.1);
this.mesh.setCcdSweptSphereRadius(0.2);
this.mesh.addEventListener('collision', collisionHandler, false);
// store a reference to this domino inside the mesh.
this.mesh.domino = this;
}
Domino.prototype.move = function(position) {
this.mesh.position.copy(position);
this.position = position.clone()
}
Domino.prototype.turn = function(angle) {
// this.mesh.__dirtyPosition = true;
this.mesh.__dirtyRotation = true;
this.mesh.rotateY(angle);
// this.mesh.geometry.applyMatrix( new THREE.Matrix4().makeRotationY( angle ) );
this.rotation.copy(this.mesh.rotation);
this.angle += angle;
}
Domino.prototype.push = function(face) {
// Get face normal, take rotation into consideration
var mat = new THREE.Matrix4().extractRotation( this.mesh.matrixWorld );
var normal = face.normal.clone();
normal.applyMatrix4(mat);
// compute force
var f = new THREE.Vector3().copy(normal);
// apply force in oposite direction.
f.multiplyScalar(-3.5);
this.mesh.setLinearVelocity(f);
// domino.applyCentralImpulse(f);
}
Domino.prototype.highlight = function() {
this.mesh.material.color.r += 0.1;
this.mesh.material.color.g += 0.1;
this.mesh.material.color.b += 0.1;
}
Domino.prototype.restoreColor = function() {
this.mesh.material.color.r -= 0.1;
this.mesh.material.color.g -= 0.1;
this.mesh.material.color.b -= 0.1;
}
Domino.prototype.select = function() {
this.color = Globals.selected_color.color;
this.mesh.material.color = new THREE.Color(Globals.selected_color.color);
this.highlight();
this.highlight();
// Add arrows.
// scene.add(rightArrow);
// scene.add(leftArrow);
// this.mesh.add(rightArrow);
// this.mesh.add(leftArrow);
}
Domino.prototype.deSelect = function() {
this.restoreColor();
this.restoreColor();
// Remove arrows.
// scene.remove(rightArrow);
// scene.remove(leftArrow);
// this.mesh.remove(rightArrow);
// this.mesh.remove(leftArrow);
}
Domino.prototype.freez = function() {
if(Globals.edit_mode) {
this.mesh.setAngularVelocity(Consts.zero);
this.mesh.setLinearVelocity(Consts.zero);
} else {
this.mesh.mass = 0;
}
}
Domino.prototype.unFreez = function() {
if(Globals.edit_mode) {
this.mesh.setAngularVelocity(Consts.one);
this.mesh.setLinearVelocity(Consts.one);
} else {
this.mesh.mass = Consts.block_width * Consts.block_height * Consts.block_thick;
}
}
Domino.prototype.marshal = function() {
return { 'position':this.position, 'angle':this.angle, 'color':this.color };
}
Domino.prototype.restoreOriginalPosition = function() {
var me = this;
this.freez();
// current orientation
var from = {posX: this.mesh.position.x, posY: this.mesh.position.y, posZ: this.mesh.position.z,
rotX: this.mesh.rotation.x, rotY: this.mesh.rotation.y, rotZ: this.mesh.rotation.z};
// convert roration angle to a vector.
var xFromRotationV = new THREE.Vector2(Math.cos(this.mesh.rotation.x), Math.sin(this.mesh.rotation.x));
var xToRotationV = new THREE.Vector2(Math.cos(this.rotation.x), Math.sin(this.rotation.x));
var rotX = Utils.signedAngleBetween(xFromRotationV, xToRotationV);
rotX += this.mesh.rotation.x;
var yFromRotationV = new THREE.Vector2(Math.cos(this.mesh.rotation.y), Math.sin(this.mesh.rotation.y));
var yToRotationV = new THREE.Vector2(Math.cos(this.rotation.y), Math.sin(this.rotation.y));
var rotY = Utils.signedAngleBetween(yFromRotationV, yToRotationV);
rotY += this.mesh.rotation.y;
var zFromRotationV = new THREE.Vector2(Math.cos(this.mesh.rotation.z), Math.sin(this.mesh.rotation.z));
var zToRotationV = new THREE.Vector2(Math.cos(this.rotation.z), Math.sin(this.rotation.z));
var rotZ = Utils.signedAngleBetween(zFromRotationV, zToRotationV);
rotZ += this.mesh.rotation.z;
var to = {posX: this.position.x, posY: (Consts.block_height/2) + 0.05, posZ: this.position.z,
rotX: rotX, rotY: rotY, rotZ: rotZ};
var tween = new TWEEN.Tween(from)
.to(to, 1000)
.onUpdate(function() {
me.mesh.position.setX(this.posX);
me.mesh.position.setY(this.posY);
me.mesh.position.setZ(this.posZ);
me.mesh.rotation.x = this.rotX;
me.mesh.rotation.y = this.rotY;
me.mesh.rotation.z = this.rotZ;
})
.onComplete(function() {
me.mesh.__dirtyPosition = true;
me.mesh.__dirtyRotation = true;
me.unFreez();
me.position.copy(me.mesh.position);
me.rotation.copy(me.mesh.rotation);
})
.start();
}
module.exports = Domino;