-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.ts
80 lines (63 loc) · 2.49 KB
/
vector3.ts
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
namespace threed {
export class Vector3 {
public x: Fx8;
public y: Fx8;
public z: Fx8;
constructor(x: Fx8, y: Fx8, z: Fx8) {
this.x = x;
this.y = y;
this.z = z;
}
public static FromNumbers(x: number, y: number, z: number) {
return new Vector3(Fx8(x), Fx8(y), Fx8(z));
}
public toVector4() {
return new Vector4(this.x, this.y, this.z, Fx.oneFx8);
}
public static Zero() {
return new Vector3(Fx.zeroFx8, Fx.zeroFx8, Fx.zeroFx8);
}
public static One() {
return new Vector3(Fx.oneFx8, Fx.oneFx8, Fx.oneFx8);
}
public static X() {
return new Vector3(Fx.oneFx8, Fx.zeroFx8, Fx.zeroFx8);
}
public static Y() {
return new Vector3(Fx.zeroFx8, Fx.oneFx8, Fx.zeroFx8);
}
public static Z() {
return new Vector3(Fx.zeroFx8, Fx.zeroFx8, Fx.oneFx8);
}
public static Scale(k: Fx8, vec: Vector3) {
return new Vector3(Fx.mul(k, vec.x), Fx.mul(k, vec.y), Fx.mul(k, vec.z));
}
public static Dot(v1: Vector3, v2: Vector3) {
return Fx.add(Fx.add(Fx.mul(v1.x, v2.x), Fx.mul(v1.y, v2.y)), Fx.mul(v1.z, v2.z));
}
public static Cross(v1: Vector3, v2: Vector3) {
return new Vector3(
Fx.sub(Fx.mul(v1.y, v2.z), Fx.mul(v1.z, v2.y)),
Fx.sub(Fx.mul(v1.z, v2.x), Fx.mul(v1.x, v2.z)),
Fx.sub(Fx.mul(v1.x, v2.y), Fx.mul(v1.y, v2.x)));
}
public static Add(v1: Vector3, v2: Vector3) {
return new Vector3(Fx.add(v1.x, v2.x), Fx.add(v1.y, v2.y), Fx.add(v1.z, v2.z));
}
public static Subtract(v1: Vector3, v2: Vector3) {
return new Vector3(Fx.sub(v1.x, v2.x), Fx.sub(v1.y, v2.y), Fx.sub(v1.z, v2.z));
}
public static Magnitude(vec: Vector3) {
// TODO: Make faster - don't use Math.sqrt
return Fx8(Math.sqrt(Fx.toFloat(Vector3.Dot(vec, vec))));
}
public static Normalized(vec: Vector3) {
const mag = Vector3.Magnitude(vec);
if (!mag) return vec;
return Vector3.Scale(Fx.div(Fx.oneFx8, mag), vec);
}
public static Abs(vec: Vector3) {
return new Vector3(Fx.abs(vec.x), Fx.abs(vec.y), Fx.abs(vec.z));
}
}
}