Skip to content

Commit 9f5cf0d

Browse files
patowenRalith
authored andcommitted
Document and simplify math module
1 parent 971dc9d commit 9f5cf0d

File tree

17 files changed

+462
-317
lines changed

17 files changed

+462
-317
lines changed

client/src/graphics/draw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl Draw {
289289
let draw_started = Instant::now();
290290
let view = sim.as_ref().map_or_else(Position::origin, |sim| sim.view());
291291
let projection = frustum.projection(1.0e-4);
292-
let view_projection = projection.matrix() * na::Matrix4::from(view.local.mtranspose());
292+
let view_projection = projection.matrix() * na::Matrix4::from(view.local.inverse());
293293
self.loader.drive();
294294

295295
let device = &*self.gfx.device;

client/src/graphics/frustum.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ impl FrustumPlanes {
9393
#[cfg(test)]
9494
mod tests {
9595
use super::*;
96-
use common::math::translate_along;
96+
97+
use common::math::MIsometry;
9798
use std::f32;
9899

99100
#[test]
@@ -102,28 +103,27 @@ mod tests {
102103
let planes = Frustum::from_vfov(f32::consts::FRAC_PI_4, 1.0).planes();
103104
assert!(planes.contain(&MVector::origin(), 0.1));
104105
assert!(planes.contain(
105-
&(translate_along(&-na::Vector3::z()) * MVector::origin()),
106+
&(MIsometry::translation_along(&-na::Vector3::z()) * MVector::origin()),
106107
0.0
107108
));
108109
assert!(!planes.contain(
109-
&(translate_along(&na::Vector3::z()) * MVector::origin()),
110+
&(MIsometry::translation_along(&na::Vector3::z()) * MVector::origin()),
110111
0.0
111112
));
112-
113113
assert!(!planes.contain(
114-
&(translate_along(&na::Vector3::x()) * MVector::origin()),
114+
&(MIsometry::translation_along(&na::Vector3::x()) * MVector::origin()),
115115
0.0
116116
));
117117
assert!(!planes.contain(
118-
&(translate_along(&na::Vector3::y()) * MVector::origin()),
118+
&(MIsometry::translation_along(&na::Vector3::y()) * MVector::origin()),
119119
0.0
120120
));
121121
assert!(!planes.contain(
122-
&(translate_along(&-na::Vector3::x()) * MVector::origin()),
122+
&(MIsometry::translation_along(&-na::Vector3::x()) * MVector::origin()),
123123
0.0
124124
));
125125
assert!(!planes.contain(
126-
&(translate_along(&-na::Vector3::y()) * MVector::origin()),
126+
&(MIsometry::translation_along(&-na::Vector3::y()) * MVector::origin()),
127127
0.0
128128
));
129129
}

client/src/graphics/voxels/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Voxels {
122122
}
123123
let node_scan_started = Instant::now();
124124
let frustum_planes = frustum.planes();
125-
let local_to_view = view.local.mtranspose();
125+
let local_to_view = view.local.inverse();
126126
let mut extractions = Vec::new();
127127
let mut workqueue_is_full = false;
128128
for &(node, ref node_transform) in nearby_nodes {

client/src/local_character_controller.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ impl LocalCharacterController {
2525
pub fn oriented_position(&self) -> Position {
2626
Position {
2727
node: self.position.node,
28-
local: self.position.local
29-
* MIsometry::unit_quaternion_to_homogeneous(self.orientation),
28+
local: self.position.local * MIsometry::from(self.orientation),
3029
}
3130
}
3231

client/src/sim.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use common::{
1111
character_controller,
1212
collision_math::Ray,
1313
graph::{Graph, NodeId},
14-
graph_ray_casting, math,
14+
graph_ray_casting,
1515
math::{MIsometry, MVector},
1616
node::{populate_fresh_nodes, ChunkId, VoxelData},
1717
proto::{
@@ -510,8 +510,9 @@ impl Sim {
510510
pub fn view(&self) -> Position {
511511
let mut pos = self.local_character_controller.oriented_position();
512512
let up = self.graph.get_relative_up(&pos).unwrap();
513-
pos.local *=
514-
math::translate_along(&(up.as_ref() * (self.cfg.character.character_radius - 1e-3)));
513+
pos.local *= MIsometry::translation_along(
514+
&(up.as_ref() * (self.cfg.character.character_radius - 1e-3)),
515+
);
515516
pos
516517
}
517518

common/src/character_controller/collision.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tracing::error;
55
use crate::{
66
collision_math::Ray,
77
graph::Graph,
8-
graph_collision, math,
8+
graph_collision,
99
math::{MIsometry, MVector},
1010
proto::Position,
1111
};
@@ -56,7 +56,7 @@ pub fn check_collision(
5656
.atanh();
5757

5858
let displacement_vector = displacement_normalized.xyz() * distance;
59-
let displacement_transform = math::translate_along(&displacement_vector);
59+
let displacement_transform = MIsometry::translation_along(&displacement_vector);
6060

6161
CollisionCheckingResult {
6262
displacement_vector,
@@ -67,7 +67,7 @@ pub fn check_collision(
6767
// This normal now represents a contact point at the origin, so we omit the w-coordinate
6868
// to ensure that it's orthogonal to the origin.
6969
normal: na::UnitVector3::new_normalize(
70-
(displacement_transform.mtranspose() * hit.normal).xyz(),
70+
(displacement_transform.inverse() * hit.normal).xyz(),
7171
),
7272
}),
7373
}

common/src/character_controller/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
vector_bounds::{BoundedVectors, VectorBound},
1212
},
1313
graph::Graph,
14-
math,
14+
math::{self, MIsometry},
1515
proto::{CharacterInput, Position},
1616
sanitize_motion_input,
1717
sim_config::CharacterConfig,
@@ -47,7 +47,7 @@ pub fn run_character_step(
4747
}
4848

4949
// Renormalize
50-
position.local = position.local.renormalize_isometry();
50+
position.local = position.local.renormalized();
5151
let (next_node, transition_xf) = graph.normalize_transform(position.node, &position.local);
5252
if next_node != position.node {
5353
position.node = next_node;
@@ -120,7 +120,7 @@ fn run_no_clip_character_step(
120120
) {
121121
*velocity = ctx.movement_input * ctx.cfg.no_clip_movement_speed;
122122
*on_ground = false;
123-
position.local *= math::translate_along(&(*velocity * ctx.dt_seconds));
123+
position.local *= MIsometry::translation_along(&(*velocity * ctx.dt_seconds));
124124
}
125125

126126
/// Returns the normal corresponding to the ground below the character, up to the `allowed_distance`. If

common/src/chunk_collision.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn find_face_collision(
9292
// Find a normal to the grid plane. Note that (t, 0, 0, x) is a normal of the plane whose closest point
9393
// to the origin is (x, 0, 0, t), and we use that fact here.
9494
let normal = math::tuv_to_xyz(t_axis, MVector::new(1.0, 0.0, 0.0, layout.grid_to_dual(t)))
95-
.lorentz_normalize();
95+
.normalized();
9696

9797
let Some(new_tanh_distance) =
9898
ray.solve_sphere_plane_intersection(&normal, collider_radius.sinh())
@@ -170,12 +170,12 @@ fn find_edge_collision(
170170
// Compute vectors Lorentz-orthogonal to the edge and to each other
171171
let edge_normal0 =
172172
math::tuv_to_xyz(t_axis, MVector::new(0.0, 1.0, 0.0, layout.grid_to_dual(u)))
173-
.lorentz_normalize();
173+
.normalized();
174174

175175
let edge_normal1 =
176176
math::tuv_to_xyz(t_axis, MVector::new(0.0, 0.0, 1.0, layout.grid_to_dual(v)));
177177
let edge_normal1 =
178-
(edge_normal1 - edge_normal0 * edge_normal0.mip(&edge_normal1)).lorentz_normalize();
178+
(edge_normal1 - edge_normal0 * edge_normal0.mip(&edge_normal1)).normalized();
179179

180180
let Some(new_tanh_distance) = ray.solve_sphere_line_intersection(
181181
&edge_normal0,
@@ -248,19 +248,17 @@ fn find_vertex_collision(
248248
}
249249

250250
// Compute vectors Lorentz-orthogonal to the vertex and to each other
251-
let vertex_normal0 =
252-
MVector::new(1.0, 0.0, 0.0, layout.grid_to_dual(x)).lorentz_normalize();
251+
let vertex_normal0 = MVector::new(1.0, 0.0, 0.0, layout.grid_to_dual(x)).normalized();
253252

254253
let vertex_normal1 = MVector::new(0.0, 1.0, 0.0, layout.grid_to_dual(y));
255-
let vertex_normal1 = (vertex_normal1
256-
- vertex_normal0 * vertex_normal0.mip(&vertex_normal1))
257-
.lorentz_normalize();
254+
let vertex_normal1 =
255+
(vertex_normal1 - vertex_normal0 * vertex_normal0.mip(&vertex_normal1)).normalized();
258256

259257
let vertex_normal2 = MVector::new(0.0, 0.0, 1.0, layout.grid_to_dual(z));
260258
let vertex_normal2 = (vertex_normal2
261259
- vertex_normal0 * vertex_normal0.mip(&vertex_normal2)
262260
- vertex_normal1 * vertex_normal1.mip(&vertex_normal2))
263-
.lorentz_normalize();
261+
.normalized();
264262

265263
let Some(new_tanh_distance) = ray.solve_sphere_point_intersection(
266264
&vertex_normal0,
@@ -283,7 +281,7 @@ fn find_vertex_collision(
283281
layout.grid_to_dual(z),
284282
1.0,
285283
)
286-
.lorentz_normalize();
284+
.normalized();
287285

288286
// A collision was found. Update the hit.
289287
let ray_endpoint = ray.ray_point(new_tanh_distance);
@@ -306,7 +304,7 @@ fn voxel_is_solid(voxel_data: &VoxelData, layout: &ChunkLayout, coords: [u8; 3])
306304

307305
#[cfg(test)]
308306
mod tests {
309-
use crate::node::VoxelData;
307+
use crate::{math::MIsometry, node::VoxelData};
310308

311309
use super::*;
312310

@@ -357,20 +355,20 @@ mod tests {
357355
ray_start_grid_coords[2] / ctx.layout.dual_to_grid_factor(),
358356
1.0,
359357
)
360-
.lorentz_normalize();
358+
.normalized();
361359

362360
let ray_end = MVector::new(
363361
ray_end_grid_coords[0] / ctx.layout.dual_to_grid_factor(),
364362
ray_end_grid_coords[1] / ctx.layout.dual_to_grid_factor(),
365363
ray_end_grid_coords[2] / ctx.layout.dual_to_grid_factor(),
366364
1.0,
367365
)
368-
.lorentz_normalize();
366+
.normalized();
369367

370368
let ray = Ray::new(
371369
ray_start,
372370
((ray_end - ray_start) + ray_start * ray_start.mip(&(ray_end - ray_start)))
373-
.lorentz_normalize(),
371+
.normalized(),
374372
);
375373

376374
let tanh_distance = (-ray_start.mip(&ray_end)).acosh();
@@ -511,14 +509,14 @@ mod tests {
511509
/// Ensures that the normal is pointing outward, opposite the ray direction.
512510
fn sanity_check_normal(ray: &Ray, hit: &ChunkCastHit) {
513511
// The ray we care about is after its start point has moved to the contact point.
514-
let ray = math::translate(
512+
let ray = MIsometry::translation(
515513
&ray.position,
516-
&ray.ray_point(hit.tanh_distance).lorentz_normalize(),
514+
&ray.ray_point(hit.tanh_distance).normalized(),
517515
) * ray;
518516

519517
// Project normal to be perpendicular to the ray's position
520518
let corrected_normal =
521-
(hit.normal + ray.position * hit.normal.mip(&ray.position)).lorentz_normalize();
519+
(hit.normal + ray.position * hit.normal.mip(&ray.position)).normalized();
522520

523521
// Check that the normal and ray are pointing opposite directions
524522
assert!(corrected_normal.mip(&ray.direction) < 0.0);

common/src/chunk_ray_casting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn find_face_collision(
7070
// Find a normal to the grid plane. Note that (t, 0, 0, x) is a normal of the plane whose closest point
7171
// to the origin is (x, 0, 0, t), and we use that fact here.
7272
let normal = math::tuv_to_xyz(t_axis, MVector::new(1.0, 0.0, 0.0, layout.grid_to_dual(t)))
73-
.lorentz_normalize();
73+
.normalized();
7474

7575
let Some(new_tanh_distance) = ray.solve_point_plane_intersection(&normal) else {
7676
continue;
@@ -186,20 +186,20 @@ mod tests {
186186
ray_start_grid_coords[2] / ctx.layout.dual_to_grid_factor(),
187187
1.0,
188188
)
189-
.lorentz_normalize();
189+
.normalized();
190190

191191
let ray_end = MVector::new(
192192
ray_end_grid_coords[0] / ctx.layout.dual_to_grid_factor(),
193193
ray_end_grid_coords[1] / ctx.layout.dual_to_grid_factor(),
194194
ray_end_grid_coords[2] / ctx.layout.dual_to_grid_factor(),
195195
1.0,
196196
)
197-
.lorentz_normalize();
197+
.normalized();
198198

199199
let ray = Ray::new(
200200
ray_start,
201201
((ray_end - ray_start) + ray_start * ray_start.mip(&(ray_end - ray_start)))
202-
.lorentz_normalize(),
202+
.normalized(),
203203
);
204204

205205
let tanh_distance = (-(ray_start.mip(&ray_end))).acosh();

0 commit comments

Comments
 (0)