-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.js
More file actions
38 lines (32 loc) · 878 Bytes
/
vector3.js
File metadata and controls
38 lines (32 loc) · 878 Bytes
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
class Vector3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
cross(v) {
let result = new Vector3(0, 0, 0);
result.x = this.y * v.z - this.z * v.y;
result.y = this.z * v.x - this.x * v.z;
result.z = this.x * v.y - this.y * v.x;
return result;
}
normalize() {
let len = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
this.x /= len; this.y /= len; this.z /= len;
}
add(v) {
let result = new Vector3(0, 0 ,0);
result.x = this.x + v.x;
result.y = this.y + v.y;
result.z = this.z + v.z;
return result;
}
sub(v) {
let result = new Vector3(0, 0 ,0);
result.x = this.x - v.x;
result.y = this.y - v.y;
result.z = this.z - v.z;
return result;
}
}