-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.js
More file actions
188 lines (143 loc) · 3.38 KB
/
math.js
File metadata and controls
188 lines (143 loc) · 3.38 KB
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
function getNormal(obj) {
let MAX = -Infinity;
for (let i in obj) MAX = abs(obj[i]) > MAX ? abs(obj[i]) : MAX;
for (let i in obj) obj[i] /= MAX;
return obj;
}
function vectorTo(x1, y1, x2, y2) {
return getNormal({ x: x2 - x1, y: y2 - y1 });
}
function vectorToXY(p1, p2) {
return getNormal({ x: p2.x - p1.x, y: p2.y - p1.y });
}
function vectorToXZ(p1, p2) {
return getNormal({ x: p2.x - p1.x, z: p2.z - p1.z });
}
function vectorToXYZ(p1, p2) {
return getNormal({ x: p2.x - p1.x, y: p2.y - p1.y, z: p2.z - p1.z });
}
function vDot(A, B) {
return A.x * B.x + A.y * B.y;
}
function v3Dot(A, B) {
return A.x * B.x + A.y * B.y + A.z * B.z;
}
function v2Mag(v) {
return sqrt(v.x ** 2 + v.y ** 2);
}
function v3Cross(U, V) {
return {
x: U.y * V.z - U.z * V.y,
y: U.z * V.x - U.x * V.z,
z: U.x * V.y - U.y * V.x,
};
}
function v3Mult(V, n) {
return {
x: V.x * n,
y: V.y * n,
z: V.z * n,
};
}
function v3Add(U, V) {
return {
x: U.x + V.x,
y: U.y + V.y,
z: U.z + V.z,
};
}
function v3Neg(V) {
return {
x: -V.x,
y: -V.y,
z: -V.z,
};
}
function vAngleBetween(A, B) {
return acos(vDot(A, B) / (v2Mag(A) * v2Mag(B)));
}
function getPlaneNormalVector(p1, p2, p3) {
let a = vectorToXYZ(p1, p2);
let b = vectorToXYZ(p1, p3);
return v3Cross(a, b);
}
function vectorPlaneIntersect(rayP, rayT, p1, p2, p3) {
let planeP = p1;
let rayD = getNormal(vectorToXYZ(rayP, rayT)); // Make sure direction is normalized
let planeN = getPlaneNormalVector(p1, p2, p3);
let denom = v3Dot(rayD, planeN);
if (abs(denom) < 0.0001) return null;
let d = v3Dot(planeP, v3Neg(planeN));
let t = -(d + v3Dot(rayP, planeN)) / denom;
return v3Add(rayP, v3Mult(rayD, t));
}
function multiplyMatrixVector(v, m) {
//v = vec3d and m = mat4x4
var o = {};
o.x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0];
o.y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1];
o.z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2];
var w = v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3];
if (w !== 0) {
o.x /= w;
o.y /= w;
o.z /= w;
}
return o;
}
function rotateObjectX(obj, theta) {
//Matrix Rotate X
var matRotX = [];
for (var i = 0; i < 4; i++) {
matRotX[i] = [];
for (var j = 0; j < 4; j++) {
matRotX[i][j] = 0;
}
}
matRotX[0][0] = 1.0;
matRotX[1][1] = cos(theta);
matRotX[1][2] = sin(theta);
matRotX[2][1] = -sin(theta);
matRotX[2][2] = cos(theta);
matRotX[3][3] = 1.0;
for (let i in obj.verts) {
let vert = obj.verts[i];
let newV = multiplyMatrixVector(vert, matRotX);
vert.x = newV.x;
vert.y = newV.y;
vert.z = newV.z;
}
}
function rotateObjectZ(obj, theta) {
//Matrix Rotate Z
var matRotZ = [];
for (var i = 0; i < 4; i++) {
matRotZ[i] = [];
for (var j = 0; j < 4; j++) {
matRotZ[i][j] = 0;
}
}
matRotZ[0][0] = cos(theta);
matRotZ[0][1] = sin(theta);
matRotZ[1][0] = -sin(theta);
matRotZ[1][1] = cos(theta);
matRotZ[2][2] = 1.0;
matRotZ[3][3] = 1.0;
for (let i in obj.verts) {
let vert = obj.verts[i];
let newV = multiplyMatrixVector(vert, matRotZ);
vert.x = newV.x;
vert.y = newV.y;
vert.z = newV.z;
}
}
function translateObject(obj, x, y, z) {
for (let i in obj.verts) {
let vert = obj.verts[i];
vert.x += x;
vert.y += y;
vert.z += z;
}
}
/*
*/