Skip to content

Commit a3f015e

Browse files
committed
Nuke some nalgebra types in the pipeline
1 parent 97bb4e0 commit a3f015e

14 files changed

Lines changed: 233 additions & 413 deletions

File tree

common/src/main/rust/rapier/benches/collision_benchmark.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use criterion::{Criterion, criterion_group, criterion_main};
22
use marten::Real;
33
use marten::octree::SubLevelOctree;
4+
use rapier3d::glamx::{DVec3, IVec3};
45
use rapier3d::math::Pose3;
5-
use rapier3d::na::Vector3;
66
use rapier3d::prelude::ColliderHandle;
77
use sable_rapier::ActiveLevelColliderInfo;
88
use sable_rapier::algo::{DEFAULT_COLLISION_PARALLEL_CUTOFF, find_collision_pairs};
@@ -15,9 +15,9 @@ fn setup_dummy_sable_handle_a() -> ActiveLevelColliderInfo {
1515
ActiveLevelColliderInfo {
1616
static_mount: None,
1717
collider: ColliderHandle::default(),
18-
local_bounds_min: Some(Vector3::<i32>::new(0, 0, 0)),
19-
local_bounds_max: Some(Vector3::<i32>::new(128, 128, 128)),
20-
center_of_mass: Some(Vector3::<f64>::new(62.5, 62.5, 62.5)),
18+
local_bounds_min: Some(IVec3::new(0, 0, 0)),
19+
local_bounds_max: Some(IVec3::new(128, 128, 128)),
20+
center_of_mass: Some(DVec3::new(62.5, 62.5, 62.5)),
2121
octree: Some(octree),
2222
chunk_map: None,
2323
scene_id: 0,
@@ -32,9 +32,9 @@ fn setup_dummy_sable_handle_b() -> ActiveLevelColliderInfo {
3232
ActiveLevelColliderInfo {
3333
static_mount: None,
3434
collider: ColliderHandle::default(),
35-
local_bounds_min: Some(Vector3::<i32>::new(128, 0, 0)),
36-
local_bounds_max: Some(Vector3::<i32>::new(256, 128, 128)),
37-
center_of_mass: Some(Vector3::<f64>::new(128.0 + 64.5, 64.5, 64.5)),
35+
local_bounds_min: Some(IVec3::new(128, 0, 0)),
36+
local_bounds_max: Some(IVec3::new(256, 128, 128)),
37+
center_of_mass: Some(DVec3::new(128.0 + 64.5, 64.5, 64.5)),
3838
octree: Some(octree),
3939
chunk_map: None,
4040
scene_id: 0,

common/src/main/rust/rapier/src/algo.rs

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::cmp::min;
22

33
use marten::Real;
44
use marten::level::OCTREE_CHUNK_SHIFT;
5-
use rapier3d::glamx::Pose3;
6-
use rapier3d::math::Vector;
7-
use rapier3d::na::{SimdComplexField, Vector3};
5+
use rapier3d::glamx::{DVec3, IVec3, Pose3};
6+
use rapier3d::math::Vec3;
7+
use rapier3d::na::SimdComplexField;
88
use rayon::iter::ParallelIterator;
99
use rayon::prelude::{IntoParallelRefIterator, ParallelExtend};
1010

@@ -21,11 +21,11 @@ pub fn find_collision_pairs(
2121
prediction: Real,
2222
cutoff: usize,
2323
liquid: bool,
24-
) -> Vec<(Vector3<i32>, Vector3<i32>)> {
24+
) -> Vec<(IVec3, IVec3)> {
2525
struct StackObject {
2626
index: u32,
2727
depth: u32,
28-
min: Vector3<i32>,
28+
min: IVec3,
2929
}
3030

3131
let Some(octree) = &sable_body.octree else {
@@ -36,39 +36,36 @@ pub fn find_collision_pairs(
3636

3737
let center_of_mass = sable_body.center_of_mass.unwrap();
3838

39-
let offset = Vector3::new(
39+
let offset = DVec3::new(
4040
local_bounds_min.x as f64 - center_of_mass.x,
4141
local_bounds_min.y as f64 - center_of_mass.y,
4242
local_bounds_min.z as f64 - center_of_mass.z,
4343
);
44-
let offset = Vector3::new(offset.x as Real, offset.y as Real, offset.z as Real);
44+
let offset = Vec3::new(offset.x as Real, offset.y as Real, offset.z as Real);
4545

4646
let offset = isometry.rotation.mul_vec3(offset.into());
4747
let translation = isometry.translation + offset;
4848

4949
// start with the root node
5050
let mut current_level = Vec::with_capacity(128);
5151

52-
let com_offset: Vector3<f64> = if let Some(other_handle) = other_sable_body {
52+
let com_offset: DVec3 = if let Some(other_handle) = other_sable_body {
5353
let com = other_handle.center_of_mass.unwrap();
54-
Vector3::new(com.x, com.y, com.z)
54+
DVec3::new(com.x, com.y, com.z)
5555
} else {
56-
Vector3::new(0.0, 0.0, 0.0)
56+
DVec3::ZERO
5757
};
5858

5959
current_level.push(StackObject {
6060
index: 0,
6161
depth: 0,
62-
min: Vector3::new(0, 0, 0),
62+
min: IVec3::ZERO,
6363
});
6464

6565
let mut pairs = Vec::with_capacity(16);
6666
// process nodes level by level to maintain some structure while parallelizing
6767
while !current_level.is_empty() {
68-
type LevelData = (
69-
Option<Vec<StackObject>>,
70-
Option<Vec<(Vector3<i32>, Vector3<i32>)>>,
71-
);
68+
type LevelData = (Option<Vec<StackObject>>, Option<Vec<(IVec3, IVec3)>>);
7269
let mut next_level_data = Vec::<LevelData>::with_capacity(8);
7370

7471
let do_level_parallel = current_level.len() >= cutoff;
@@ -78,12 +75,7 @@ pub fn find_collision_pairs(
7875
let node_size = 1 << (octree.log_size as u32 - entry.depth);
7976

8077
// Calculate the center and radius for this node
81-
let node_center = Vector3::new(
82-
entry.min.x as Real + node_size as Real / 2.0,
83-
entry.min.y as Real + node_size as Real / 2.0,
84-
entry.min.z as Real + node_size as Real / 2.0,
85-
);
86-
let node_center = Vector::new(node_center.x, node_center.y, node_center.z);
78+
let node_center = entry.min.as_vec3() + node_size as f32 / 2.0;
8779
let transformed_center = isometry.rotation.mul_vec3(node_center) + translation;
8880
let radius = node_size as Real / 2.0 * 1.7321 + prediction;
8981

@@ -121,7 +113,7 @@ pub fn find_collision_pairs(
121113
index: (node + i) as u32,
122114
depth: entry.depth + 1,
123115
min: entry.min
124-
+ Vector3::new(
116+
+ IVec3::new(
125117
(i & 1) * node_size / 2,
126118
((i >> 1) & 1) * node_size / 2,
127119
((i >> 2) & 1) * node_size / 2,
@@ -156,13 +148,13 @@ pub fn find_collision_pairs(
156148

157149
fn get_overlapping_nodes(
158150
other_handle: Option<&ActiveLevelColliderInfo>,
159-
com_offset: Vector3<f64>,
160-
pos: Vector3<Real>,
151+
com_offset: DVec3,
152+
pos: Vec3,
161153
dist: Real,
162154
scene: &PhysicsScene,
163155
cancel_early: bool,
164156
liquid: bool,
165-
) -> (bool, Option<Vec<Vector3<i32>>>) {
157+
) -> (bool, Option<Vec<IVec3>>) {
166158
// biggest power of two that doesn't go over radius
167159
let log2 = ((dist * 2.0).simd_ln() / 2.0f32.simd_ln()).floor() as i32;
168160

@@ -175,32 +167,14 @@ fn get_overlapping_nodes(
175167
min(log2, OCTREE_CHUNK_SHIFT)
176168
};
177169

178-
let min_block_pos = Vector3::new(
179-
((pos.x - dist) as f64 + com_offset.x).floor() as i32,
180-
((pos.y - dist) as f64 + com_offset.y).floor() as i32,
181-
((pos.z - dist) as f64 + com_offset.z).floor() as i32,
182-
);
183-
let max_block_pos = Vector3::new(
184-
((pos.x + dist) as f64 + com_offset.x).floor() as i32,
185-
((pos.y + dist) as f64 + com_offset.y).floor() as i32,
186-
((pos.z + dist) as f64 + com_offset.z).floor() as i32,
187-
);
170+
let min_block_pos = ((pos - dist).as_dvec3() + com_offset).floor().as_ivec3();
171+
let max_block_pos = ((pos + dist).as_dvec3() + com_offset).floor().as_ivec3();
188172

189173
if let Some(other_handle) = other_handle {
190174
let other_min = other_handle.local_bounds_min.unwrap();
191175

192-
let min_pos = Vector3::new(
193-
(min_block_pos.x - other_min.x) >> log2,
194-
(min_block_pos.y - other_min.y) >> log2,
195-
(min_block_pos.z - other_min.z) >> log2,
196-
)
197-
.map(|x| x.max(0));
198-
199-
let max_pos = Vector3::new(
200-
(max_block_pos.x - other_min.x) >> log2,
201-
(max_block_pos.y - other_min.y) >> log2,
202-
(max_block_pos.z - other_min.z) >> log2,
203-
);
176+
let min_pos = ((min_block_pos - other_min) >> log2).map(|x| x.max(0));
177+
let max_pos = (max_block_pos - other_min) >> log2;
204178

205179
let Some(oct) = &other_handle.octree else {
206180
panic!("No octree!")
@@ -211,14 +185,15 @@ fn get_overlapping_nodes(
211185
} else {
212186
Some(Vec::with_capacity(16))
213187
};
188+
214189
for x in min_pos.x..=max_pos.x {
215190
for y in min_pos.y..=max_pos.y {
216191
for z in min_pos.z..=max_pos.z {
217192
if oct.query(x << log2, y << log2, z << log2, log2) > -2 {
218193
if cancel_early {
219194
return (true, None);
220195
} else {
221-
blocks.as_mut().unwrap().push(Vector3::new(
196+
blocks.as_mut().unwrap().push(IVec3::new(
222197
(x << log2) + other_min.x,
223198
(y << log2) + other_min.y,
224199
(z << log2) + other_min.z,
@@ -237,16 +212,8 @@ fn get_overlapping_nodes(
237212
}
238213

239214
// find all the octrees
240-
let min_octree_pos = Vector3::new(
241-
min_block_pos.x >> OCTREE_CHUNK_SHIFT,
242-
min_block_pos.y >> OCTREE_CHUNK_SHIFT,
243-
min_block_pos.z >> OCTREE_CHUNK_SHIFT,
244-
);
245-
let max_octree_pos = Vector3::new(
246-
max_block_pos.x >> OCTREE_CHUNK_SHIFT,
247-
max_block_pos.y >> OCTREE_CHUNK_SHIFT,
248-
max_block_pos.z >> OCTREE_CHUNK_SHIFT,
249-
);
215+
let min_octree_pos = min_block_pos >> OCTREE_CHUNK_SHIFT;
216+
let max_octree_pos = max_block_pos >> OCTREE_CHUNK_SHIFT;
250217

251218
let mut blocks = if cancel_early {
252219
None
@@ -286,11 +253,7 @@ fn get_overlapping_nodes(
286253
if cancel_early {
287254
return (true, None);
288255
} else {
289-
blocks.as_mut().unwrap().push(Vector3::new(
290-
x << log2,
291-
y << log2,
292-
z << log2,
293-
));
256+
blocks.as_mut().unwrap().push(IVec3::new(x, y, z) << log2);
294257
}
295258
}
296259
}

common/src/main/rust/rapier/src/boxes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use marten::Real;
55
use rapier3d::dynamics::RigidBodyBuilder;
66
use rapier3d::geometry::{ColliderBuilder, SharedShape};
77
use rapier3d::glamx::Quat;
8-
use rapier3d::math::Vector;
8+
use rapier3d::math::Vec3;
99

1010
use crate::get_scene_mut;
1111
use crate::scene::LevelColliderID;
@@ -34,7 +34,7 @@ pub extern "system" fn Java_dev_ryanhcode_sable_physics_impl_rapier_Rapier3D_cre
3434

3535
let mut rigid_body = RigidBodyBuilder::dynamic()
3636
.ccd_enabled(true)
37-
.translation(Vector::new(
37+
.translation(Vec3::new(
3838
pose_arr[0] as Real,
3939
pose_arr[1] as Real,
4040
pose_arr[2] as Real,

0 commit comments

Comments
 (0)