-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotations.js
237 lines (193 loc) · 6.08 KB
/
rotations.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Te3ds
* by Bipeen Acharya, Olivier Mahame, Suraj Bajracharya
* Submitted to: Dr. Joshua Stough
* Date: April 9th, 2014
*/
var ROT_AMT =5.0;
var mouseDown = false;
var lastMouseX = null;
var lastMouseY = null;
//handle mouse down event call back: see what it is attached to in mouseEvents.js
function handleMouseDown(event) {
mouseDown = true;
lastMouseX = event.clientX / (canvas.width / 2) - 1.0;
lastMouseY = (canvas.height - event.clientY) / (canvas.height / 2) - 1.0;
}
//handle mouse up event call back: see what it is attached to in mouseEvents.js
function handleMouseUp(event) {
mouseDown = false;
}
//handle mouse move event call back: see what it is attached to in mouseEvents.js
function handleMouseMove(event) {
if (!mouseDown) {
return;
}
var newX = event.clientX;
var newY = event.clientY;
trackBallRotation(newX, newY);
}
function trackBallRotation(newX, newY){
var cur_x = (newX / (canvas.width / 2)) - 1.0;
var cur_y = ((canvas.height - newY) / (canvas.height / 2)) - 1.0;
var deltaX = (canvas.width * (cur_x - lastMouseX));
var deltaY = (canvas.height * (cur_y - lastMouseY));
var eye_x = modelView[0];
var eye_y = modelView[1];
var v = add(
vec4(-deltaY * eye_x[0], -deltaY * eye_x[1], -deltaY * eye_x[2], 0),
vec4(eye_y[0] * deltaX, eye_y[1] * deltaX, eye_y[2] * deltaX, 0));
v = normalize(v);
for(var j = 0; j < 4; j++)
if (isNaN(v[j]))
return;
var d = Math.sqrt((v[1] * v[1]) + (v[2] * v[2]));
var rotx = mat4(1.0, 0.0, 0.0, 0.0,
0.0, v[2]/d, -v[1]/d, 0.0,
0.0, v[1]/d, v[2]/d, 0.0,
0.0, 0.0, 0.0, 1.0);
var roty = mat4(d, 0.0, -v[0], 0.0,
0, 1.0, 0.0, 0.0,
v[0], 0.0, d, 0.0,
0.0, 0.0, 0.0, 1.0);
var amt = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
lastMouseX = cur_x;
lastMouseY = cur_y;
var currentRot = mult(transpose(rotx),
mult(transpose(roty),
mult(rotate(amt, [0,0,1]),
mult(roty, rotx))));
modelView = mult(modelView, currentRot);
}
function rotateAboutAxis (angle, axis) {
if (IS_PLAYING) {
var tempListVec = [];
for (var i=LAST_PIECE_START_INDEX; i<table.length;i++){
var x = table[i].ctm[0][3];
var y = table[i].ctm[1][3];
var z = table[i].ctm[2][3];
tempListVec.push(vec3(x,y,z));
}
var avgVec;
if (table[LAST_PIECE_START_INDEX].forPiece == "L") {
avgVec = table[LAST_PIECE_START_INDEX + 2].pos;
} else if (table[LAST_PIECE_START_INDEX].forPiece == "T" || table[LAST_PIECE_START_INDEX].forPiece == "S") {
avgVec = table[LAST_PIECE_START_INDEX].pos;
} else {
avgVec = avgOfVec(tempListVec);
}
if (table[LAST_PIECE_START_INDEX].forPiece == "I") {
avgVec[0] = Math.floor(avgVec[0]);
avgVec[1] = Math.floor(avgVec[1]);
avgVec[2] = Math.floor(avgVec[2]);
}
var rot = rotate(angle, axis);
var tempListCtm = [];
var canRotate = true;
var tempPos = [];
for (var i=LAST_PIECE_START_INDEX; i<table.length;i++){
var curInd = i - LAST_PIECE_START_INDEX;
tempListCtm.push(mult(table[i].ctm,translate(-avgVec[0], -avgVec[1], -avgVec[2])));
tempListCtm[curInd] = mult(rot,tempListCtm[curInd]);
tempListCtm[curInd][0][3] += avgVec[0];
tempListCtm[curInd][1][3] += avgVec[1];
tempListCtm[curInd][2][3] += avgVec[2];
tempListCtm[curInd][0] = [1, 0, 0, tempListCtm[curInd][0][3]];
tempListCtm[curInd][1] = [0, 1, 0, tempListCtm[curInd][1][3]];
tempListCtm[curInd][2] = [0, 0, 1, tempListCtm[curInd][2][3]];
tempPos.push(vec3(tempListCtm[curInd][0][3], tempListCtm[curInd][1][3], tempListCtm[curInd][2][3]));
if (equalVectors(axis,[1,0,0])) {
if (tempPos[curInd][1] < -5 || tempPos[curInd][1] > 4 || tempPos[curInd][3] < 0) {
canRotate = false;
}
} else if (equalVectors(axis,[0,1,0])) {
if (tempPos[curInd][0] < -4.5 || tempPos[curInd][0] > 4 || tempPos[curInd][3] < 0) {
canRotate = false;
}
} else if (equalVectors(axis,[0,0,1])) {
if (tempPos[curInd][0] < -4.5 || tempPos[curInd][0] > 4 || tempPos[curInd][1] < -5 || tempPos[curInd][1] > 4) {
canRotate = false;
}
}
for (var j = FIRST_PIECE_START_INDEX; j < LAST_PIECE_START_INDEX; j++) {
for (var k = 0; k < tempPos.length; k++) {
if (checkDist(table[j].pos, tempPos[k]) < 1) {
canRotate = false;
break;
}
}
if (!canRotate) {
break;
}
}
}
if (canRotate) {
for (var i=LAST_PIECE_START_INDEX; i<table.length;i++){
var curInd = i - LAST_PIECE_START_INDEX;
table[i].ctm = tempListCtm[curInd];
table[i].pos = tempPos[curInd];
}
}
}
}
function equalVectors(u,v) {
if (u.length != v.length) {
return false;
}
for (var i = 0; i < u.length; i++) {
if (u[i] != v[i]) {
return false;
}
}
return true;
}
function avgOfVec(vecList) {
var sum = vec3(0,0,0);
for (var i = 0; i < vecList.length; i++) {
sum = add(sum,vecList[i]);
}
return vec3(sum[0]/vecList.length, sum[1]/vecList.length, sum[2]/vecList.length);
}
Math.median = function() {
var ary, numA, i;
ary = Array.prototype.slice.call(arguments);
for (i = ary.length-1; i >= 0; i--) {if (ary[i] !== +ary[i]) ary[i] = Number.NEGATIVE_INFINITY;}
numA = function(a, b){return (a-b);};
ary.sort(numA);
while (ary.length > 1 && !isFinite(ary[0])) ary.shift();
return ary[Math.floor(ary.length/2)];
}
function magnitude(u) {
return Math.sqrt(Math.pow(u[0], 2) + Math.pow(u[1], 2) + Math.pow(u[2], 2));
}
//TODO: rotate the last piece about Y-axis
//TODO: rotate the last pience about Z-axis
//rotate camera left
function rotateCameraLeft(){
var rot = rotate(-ROT_AMT, [0, 0, 1]);
modelView = mult(modelView,rot);
}
//rotate camera right
function rotateCameraRight(){
var rot = rotate(ROT_AMT, [0, 0, 1]);
modelView = mult(modelView,rot);
}
//rotate camera up
function rotateCameraUp(){
var rotX = rotate(ROT_AMT, [1, 0, 0]);
var rotY = rotate(-ROT_AMT, [0, 1, 0]);
modelView = mult(modelView,
mult(rotX, rotY));
}
//rotate camera down
function rotateCameraDown(){
var rotX = rotate(-ROT_AMT, [1, 0, 0]);
var rotY = rotate(ROT_AMT, [0, 1, 0]);
modelView = mult(modelView,
mult(rotX, rotY));
}
function resetCamera() {
modelView = defaultModelView;
projection = defaultProjection;
fovy = defaultFovy;
}