Skip to content

Commit fa9e09b

Browse files
authored
egg/math/matrix.(Matrix33f, Matrix34f)
1 parent 35f20be commit fa9e09b

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

RVLTS/egg/math/matrix.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// kiwi515/ogws:
2+
// https://github.com/kiwi515/ogws/blob/master/include/egg/math/eggMatrix.h
3+
// https://github.com/kiwi515/ogws/blob/master/src/egg/math/eggMatrix.cpp
4+
5+
import { Quatf } from "egg/math/quat";
6+
import { Vector3f } from "egg/math/vector";
7+
8+
export class Matrix33f {
9+
tbl: [
10+
[number, number, number],
11+
[number, number, number],
12+
[number, number, number]
13+
];
14+
15+
// Can't overload operator()
16+
}
17+
18+
export class Matrix34f {
19+
tbl: [
20+
[number, number, number, number],
21+
[number, number, number, number],
22+
[number, number, number, number]
23+
];
24+
25+
constructor(
26+
_00: number, _01: number, _02: number, _03: number,
27+
_10: number, _11: number, _12: number, _13: number,
28+
_20: number, _21: number, _22: number, _23: number
29+
) {
30+
this.tbl = [
31+
[_00, _01, _02, _03],
32+
[_10, _11, _12, _13],
33+
[_20, _21, _22, _23]
34+
];
35+
}
36+
37+
makeZero() {
38+
this.tbl = [
39+
[0, 0, 0, 0],
40+
[0, 0, 0, 0],
41+
[0, 0, 0, 0]
42+
];
43+
}
44+
45+
makeIdentity() {
46+
this.tbl[0][0] = 1;
47+
this.tbl[1][1] = 1;
48+
this.tbl[2][2] = 1;
49+
}
50+
51+
makeQ(quat: Quatf) {
52+
let yy = 2 * quat.y * quat.y;
53+
let zz = 2 * quat.z * quat.z;
54+
let xx = 2 * quat.x * quat.x;
55+
let xy = 2 * quat.x * quat.y;
56+
let xz = 2 * quat.x * quat.z;
57+
let yz = 2 * quat.y * quat.z;
58+
let wz = 2 * quat.w * quat.z;
59+
let wx = 2 * quat.w * quat.x;
60+
let wy = 2 * quat.w * quat.y;
61+
62+
tbl[0][0] = 1 - yy - zz;
63+
tbl[0][1] = xy - wz;
64+
tbl[0][2] = xz + wy;
65+
66+
tbl[1][0] = xy + wz;
67+
tbl[1][1] = 1 - xx - zz;
68+
tbl[1][2] = yz - wx;
69+
70+
tbl[2][0] = xz - wy;
71+
tbl[2][1] = yz + wx;
72+
tbl[2][2] = 1 - xx - yy;
73+
74+
tbl[0][3] = 0;
75+
tbl[1][3] = 0;
76+
tbl[2][3] = 0;
77+
}
78+
79+
makeS(vec: Vector3f) {
80+
tbl[0][0] = vec.x;
81+
tbl[0][1] = 0;
82+
tbl[0][2] = 0;
83+
84+
tbl[1][0] = 0;
85+
tbl[1][1] = vec.y;
86+
tbl[1][2] = 0;
87+
88+
tbl[2][0] = 0;
89+
tbl[2][1] = 0;
90+
tbl[2][2] = vec.z;
91+
92+
tbl[0][3] = 0;
93+
tbl[1][3] = 0;
94+
tbl[2][3] = 0;
95+
}
96+
97+
setAxisRotation(vec: Vector3f, angle: number) {
98+
let q: Quatf = new Quatf();
99+
q.setAxisRotation(vec, angle);
100+
makeQ(q);
101+
}
102+
103+
// TODO: loadPosMtx
104+
105+
// Can't overload operator()
106+
107+
static readonly ident: Matrix34f = new Matrix34f(
108+
1, 0, 0, 0,
109+
0, 1, 0, 0,
110+
0, 0, 1, 0
111+
);
112+
}

0 commit comments

Comments
 (0)