diff options
| author | Ben Harper <github@tukom.org> | 2024-08-06 01:19:52 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-05 17:19:52 +0200 |
| commit | af3a26c99d0303f684eb24dbd0a6be003740adba (patch) | |
| tree | e1562dd7cfa0336ac35d3f45e9729e8f5f85d7b2 /src | |
| parent | 7ff92b1cf5d019a0d6482eeb2438de38c1d5972e (diff) | |
| download | rapier-af3a26c99d0303f684eb24dbd0a6be003740adba.tar.gz rapier-af3a26c99d0303f684eb24dbd0a6be003740adba.tar.bz2 rapier-af3a26c99d0303f684eb24dbd0a6be003740adba.zip | |
Improve distant object panics in broad phase (#128)
Co-authored-by: Thierry Berger <contact@thierryberger.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/geometry/broad_phase_multi_sap/sap_layer.rs | 8 | ||||
| -rw-r--r-- | src/geometry/broad_phase_multi_sap/sap_utils.rs | 16 |
2 files changed, 17 insertions, 7 deletions
diff --git a/src/geometry/broad_phase_multi_sap/sap_layer.rs b/src/geometry/broad_phase_multi_sap/sap_layer.rs index f1a3a31..80d920a 100644 --- a/src/geometry/broad_phase_multi_sap/sap_layer.rs +++ b/src/geometry/broad_phase_multi_sap/sap_layer.rs @@ -1,4 +1,4 @@ -use super::{SAPProxies, SAPProxy, SAPRegion, SAPRegionPool}; +use super::{RegionKey, SAPProxies, SAPProxy, SAPRegion, SAPRegionPool}; use crate::geometry::broad_phase_multi_sap::DELETED_AABB_VALUE; use crate::geometry::{Aabb, BroadPhaseProxyIndex}; use crate::math::{Point, Real}; @@ -13,9 +13,9 @@ pub(crate) struct SAPLayer { pub smaller_layer: Option<u8>, pub larger_layer: Option<u8>, region_width: Real, - pub regions: HashMap<Point<i32>, BroadPhaseProxyIndex>, + pub regions: HashMap<Point<RegionKey>, BroadPhaseProxyIndex>, #[cfg_attr(feature = "serde-serialize", serde(skip))] - regions_to_potentially_remove: Vec<Point<i32>>, // Workspace + regions_to_potentially_remove: Vec<Point<RegionKey>>, // Workspace #[cfg_attr(feature = "serde-serialize", serde(skip))] pub created_regions: Vec<BroadPhaseProxyIndex>, } @@ -188,7 +188,7 @@ impl SAPLayer { /// of the new region if it did not exist and has been created by this method. pub fn ensure_region_exists( &mut self, - region_key: Point<i32>, + region_key: Point<RegionKey>, proxies: &mut SAPProxies, pool: &mut SAPRegionPool, ) -> BroadPhaseProxyIndex { diff --git a/src/geometry/broad_phase_multi_sap/sap_utils.rs b/src/geometry/broad_phase_multi_sap/sap_utils.rs index a8356ad..77edcc0 100644 --- a/src/geometry/broad_phase_multi_sap/sap_utils.rs +++ b/src/geometry/broad_phase_multi_sap/sap_utils.rs @@ -1,6 +1,11 @@ use crate::math::{Point, Real, Vector}; use parry::bounding_volume::Aabb; +#[cfg(feature = "f32")] +pub type RegionKey = i32; +#[cfg(feature = "f64")] +pub type RegionKey = i64; + pub(crate) const NUM_SENTINELS: usize = 1; pub(crate) const NEXT_FREE_SENTINEL: u32 = u32::MAX; pub(crate) const SENTINEL_VALUE: Real = Real::MAX; @@ -23,14 +28,19 @@ pub(crate) fn clamp_point(point: Point<Real>) -> Point<Real> { point.map(|e| na::clamp(e, -MAX_AABB_EXTENT, MAX_AABB_EXTENT)) } -pub(crate) fn point_key(point: Point<Real>, region_width: Real) -> Point<i32> { +pub(crate) fn point_key(point: Point<Real>, region_width: Real) -> Point<RegionKey> { (point / region_width) .coords - .map(|e| e.floor() as i32) + .map(|e| { + // If the region is outside this range, the region keys will overlap + assert!(e.floor() < RegionKey::MAX as Real); + assert!(e.floor() > RegionKey::MIN as Real); + e.floor() as RegionKey + }) .into() } -pub(crate) fn region_aabb(index: Point<i32>, region_width: Real) -> Aabb { +pub(crate) fn region_aabb(index: Point<RegionKey>, region_width: Real) -> Aabb { let mins = index.coords.map(|i| i as Real * region_width).into(); let maxs = mins + Vector::repeat(region_width); Aabb::new(mins, maxs) |
