-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetraCube.js
116 lines (93 loc) · 3.55 KB
/
TetraCube.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
class TetraCube extends GenObject {
outline = [];
lastTransformationInvert = null; // for avoiding collisions with other objects
constructor(shapes) {
super(shapes);
let color = [0.5, 0.5, 0.5];
let colorData = [
color, // Front face: pink
color, // left face: red
color, // back face: green
color, // Bottom face: blue
color, // Right face: yellow
color, // top face: purple
];
let colors = [];
for (let i = 0; i < 6; ++i) {
for (let j = 0; j < 6; ++j) {
colors.push(colorData[i]);
}
}
for(let shape of shapes) {
let clone = new Shape(gl.LINES, shape.vertices);
clone.initData(null, colors, [shape.normals]);
this.outline.push(clone)
}
this.translationMatrix = mat4.create();
this.rotationMatrix = mat4.create();
this.globalRotation = mat4.create();
}
get invertedGlobalRotatationMatrix() {
let inverted = mat4.create();
mat4.invert(inverted, this.globalRotation);
return inverted;
}
// get maxY() {
// let min = 1;
// let shapeMax = null;
// for (let shape of this.shapes) {
// min = Math.min(min, shape.maxY);
// if (min == (shape.maxY)) {
// shapeMax = shape;
// }
// }
// return min;
// }
draw() {
this.modelMatrix = mat4.create();
mat4.mul(this.rotationMatrix, this.translationMatrix, this.rotationMatrix);
mat4.mul(this.modelMatrix, this.rotationMatrix, this.modelMatrix);
this.shapes.forEach((shape) => {
mat4.mul(shape.modelMatrix, this.modelMatrix, shape.modelMatrix);
mat4.mul(shape.modelMatrix, this.globalRotation, shape.modelMatrix);
});
super.draw();
let inverted = mat4.create();
mat4.invert(inverted, this.globalRotation);
this.outline.forEach((shape) => {
mat4.mul(shape.modelMatrix, this.modelMatrix, shape.modelMatrix);
mat4.mul(shape.modelMatrix, this.globalRotation, shape.modelMatrix);
if (showObjectGrid) {
shape.draw();
}
mat4.mul(shape.modelMatrix, inverted, shape.modelMatrix);
});
this.shapes.forEach((shape) => {
mat4.mul(shape.modelMatrix, inverted, shape.modelMatrix);
});
this.translationMatrix = mat4.create();
this.rotationMatrix = mat4.create();
}
rotateLocal(angle, axis) {
this.shapes.forEach((shape) =>{
shape.rotate(angle, axis);
});
this.outline.forEach((shape) =>{
shape.rotate(angle, axis);
});
}
translate(vector) {
mat4.translate(this.translationMatrix, this.translationMatrix, vector);
// ignore if objects just fall because of the gravity, not by user control
if (vector[1] == 0) {
this.lastTransformationInvert = mat4.create();
mat4.invert(this.lastTransformationInvert, this.translationMatrix);
}
}
rotate(angle, axis, global = true) {
let currentRotation = mat4.create();
// some magic number, I don't know why, but without it, objects rotate with different speed comparing to the grid
mat4.fromRotation(currentRotation, toRad(angle) / 10, axis);
mat4.mul(this.globalRotation, currentRotation, this.globalRotation);
}
}