unbanished_core/math.rs
1//! Engine math helpers (`Runtime-steam-x64.dll` `Math::Scalar`, `Math::Vector2/3`) with the
2//! original's precision: the C runtime's `sinf`/`cosf`/`atan2f` ([`crate::crt`]), `sqrtf`
3//! (correctly rounded), and the operations in the original's order.
4
5// DAT_1801647ec
6/// Degrees to radians as an f32.
7pub const DEG_TO_RAD: f32 = 0.017453292;
8// DAT_180164878
9/// Radians to degrees.
10pub const RAD_TO_DEG: f32 = 57.29578;
11
12/// The sine of `x` (radians), with the C runtime's rounding ([`crate::crt::sinf`]).
13///
14/// Original: `FUN_140137160`.
15pub fn sinf(x: f32) -> f32 {
16 crate::crt::sinf(x)
17}
18
19/// The cosine of `x` (radians), with the C runtime's rounding ([`crate::crt::cosf`]).
20///
21/// Original: `FUN_140136560`.
22pub fn cosf(x: f32) -> f32 {
23 crate::crt::cosf(x)
24}
25
26/// The length of a 3D vector: `sqrtf((x·x + y·y) + z·z)`, added in that order.
27///
28/// Original: `Math::Vector3::Length`, Runtime `0x180022280`.
29pub fn length3(v: [f32; 3]) -> f32 {
30 (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
31}
32
33/// A 3D vector scaled to length 1 (times `1 / length`), and its length. A zero vector is returned
34/// as it is.
35///
36/// Measured: `tools/emu/normalize.py` (4,000 vectors, tiny, huge and denormal parts among them).
37///
38/// Original: `Math::Vector3::Normalize`, Runtime `0x180022300`.
39pub fn normalize3_len(v: [f32; 3]) -> ([f32; 3], f32) {
40 let l = length3(v);
41 if l == 0.0 {
42 return (v, l);
43 }
44 let inv = 1.0 / l;
45 ([v[0] * inv, v[1] * inv, v[2] * inv], l)
46}
47
48/// A 3D vector scaled to length 1. A zero vector is returned as it is. The original's version
49/// without the length calls the one with it.
50///
51/// Original: `Math::Vector3::Normalize`, Runtime `0x1800223c0`.
52pub fn normalize3(v: [f32; 3]) -> [f32; 3] {
53 normalize3_len(v).0
54}
55
56// The version with the length is Runtime 0x18006d280; both are measured.
57/// A 2D vector scaled to length 1. A zero vector is returned as it is.
58///
59/// Measured: `tools/emu/normalize.py` (with and without the length).
60///
61/// Original: `Math::Vector2::Normalize`, Runtime `0x18006d0d0`.
62pub fn normalize2(v: [f32; 2]) -> [f32; 2] {
63 let l = (v[0] * v[0] + v[1] * v[1]).sqrt();
64 if l == 0.0 {
65 return v;
66 }
67 let inv = 1.0 / l;
68 [v[0] * inv, v[1] * inv]
69}
70
71/// The direction of a yaw and a pitch, in degrees: `(−cos p · sin y, cos p · cos y, sin p)`.
72///
73/// Original: `Math::Vector3::FromYawPitch`, Runtime `0x180022990`.
74pub fn from_yaw_pitch(yaw: f32, pitch: f32) -> [f32; 3] {
75 let (ry, rp) = (yaw * DEG_TO_RAD, pitch * DEG_TO_RAD);
76 let (sy, sp, cy, cp) = (sinf(ry), sinf(rp), cosf(ry), cosf(rp));
77 [-cp * sy, cp * cy, sp]
78}
79
80/// The heading of a 2D direction, in degrees: `atan2f(−x, y)`.
81///
82/// Original: `Math::Vector3::Heading`, Runtime `0x18007d3d0`.
83pub fn heading(x: f32, y: f32) -> f32 {
84 crate::crt::atan2f(-x, y) * RAD_TO_DEG
85}
86
87/// The sine and cosine of an angle in degrees.
88///
89/// Original: `Math::Scalar::SinCos`.
90pub fn sin_cos_deg(degrees: f32) -> (f32, f32) {
91 let r = degrees * DEG_TO_RAD;
92 (sinf(r), cosf(r))
93}
94
95// The entity transform: ModelComponent +0x148 (rotation), +0x158 (offset).
96/// The engine's transform: a rotation (a quaternion) and an offset. A placed model keeps its
97/// position this way.
98///
99/// Original: `Math::Transform`.
100#[derive(Debug, Clone, Copy, PartialEq)]
101pub struct Transform {
102 /// The rotation, a unit quaternion `(x, y, z, w)`.
103 pub q: [f32; 4],
104 /// The offset, applied after the rotation.
105 pub p: [f32; 3],
106}
107
108impl Default for Transform {
109 /// The identity.
110 fn default() -> Transform {
111 Transform {
112 q: [0.0, 0.0, 0.0, 1.0],
113 p: [0.0; 3],
114 }
115 }
116}
117
118/// The product of two quaternions, its terms in the original's order.
119///
120/// Original: Runtime `0x18001eb60`.
121fn qmul(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
122 let [ax, ay, az, aw] = a;
123 let [bx, by, bz, bw] = b;
124 [
125 ((ax * bw + bx * aw) + az * by) - ay * bz,
126 ((ay * bw - az * bx) + by * aw) + bz * ax,
127 ((az * bw + ay * bx) - by * ax) + bz * aw,
128 ((bw * aw - ax * bx) - ay * by) - bz * az,
129 ]
130}
131
132impl Transform {
133 /// A transform from a yaw, a pitch (degrees) and an offset. The rotation uses the sines and
134 /// cosines of the half angles: `q = (−cy·sp, −sy·sp, −cp·sy, cp·cy)`.
135 ///
136 /// Original: `Math::Transform::FromYawPitchOffset`, Runtime `0x18007e540`.
137 pub fn from_yaw_pitch_offset(yaw: f32, pitch: f32, offset: [f32; 3]) -> Transform {
138 let hy = (yaw * 0.5) * DEG_TO_RAD;
139 let hp = (pitch * 0.5) * DEG_TO_RAD;
140 let (sy, sp, cy, cp) = (sinf(hy), sinf(hp), cosf(hy), cosf(hp));
141 Transform {
142 q: [-(cy * sp), -(sy * sp), -(cp * sy), cp * cy],
143 p: offset,
144 }
145 }
146
147 /// The rotation a CharacterComponent's update writes: `(+0, +0, −sinf(h), cosf(h))` with `h =
148 /// (yaw × DEG_TO_RAD) × 0.5`, rounded in that order (a model's `FromYawPitchOffset` halves
149 /// first, and its zeros are negative).
150 ///
151 /// Original: `FUN_14010e990`.
152 pub fn character_rotation(yaw: f32) -> [f32; 4] {
153 let h = (yaw * DEG_TO_RAD) * 0.5;
154 [0.0, 0.0, -sinf(h), cosf(h)]
155 }
156
157 /// A placed entity's model transform: its yaw, pitch 0, its position.
158 pub fn of_model(yaw: f32, position: [f32; 3]) -> Transform {
159 Transform::from_yaw_pitch_offset(yaw, 0.0, position)
160 }
161
162 /// `v` turned by the rotation: `q · (v, 0) · q̄`.
163 ///
164 /// Original: `Math::Quaternion::Rotate`, Runtime `0x18001ea20`.
165 pub fn rotate(&self, v: [f32; 3]) -> [f32; 3] {
166 let q = self.q;
167 let conj = [-q[0], -q[1], -q[2], q[3]];
168 let t = qmul(q, [v[0], v[1], v[2], 0.0]);
169 let r = qmul(t, conj);
170 [r[0], r[1], r[2]]
171 }
172
173 /// A point moved by the transform: rotated, then offset.
174 ///
175 /// Original: `Math::Transform::operator*(Vector3)`, Runtime `0x18001eae0`.
176 pub fn apply(&self, v: [f32; 3]) -> [f32; 3] {
177 let r = self.rotate(v);
178 [r[0] + self.p[0], self.p[1] + r[1], r[2] + self.p[2]]
179 }
180
181 /// The inverse transform: the conjugate rotation, and the negated offset turned by it.
182 ///
183 /// Original: `Math::Transform::Inverse`, Runtime `0x18005e620`.
184 pub fn inverse(&self) -> Transform {
185 let q = self.q;
186 let conj = Transform {
187 q: [-q[0], -q[1], -q[2], q[3]],
188 p: [0.0; 3],
189 };
190 Transform {
191 q: conj.q,
192 p: conj.rotate([-self.p[0], -self.p[1], -self.p[2]]),
193 }
194 }
195
196 /// A heading in the model's own space, turned into the world: the heading of
197 /// `from_yaw_pitch(local_yaw, 0)` after the rotation.
198 ///
199 /// Original: `FUN_140128d50`.
200 pub fn heading_of(&self, local_yaw: f32) -> f32 {
201 let v = self.rotate(from_yaw_pitch(local_yaw, 0.0));
202 heading(v[0], v[1])
203 }
204}