From 55945ee97524cb8a6fbfb5d1873a7d86b773f846 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:34:01 +0200 Subject: [PATCH] Collision scans: skip blocks with no shape, build the entity shape lazily BlockCollisionScan asked every cursor cell for its collision shape, moved it into place and intersected it with Shapes.create(box), even for air. Mob movement and every other Entity.collide spent most of their time there. Now: - CollisionBlock carries empty_shape, set by the registry for states with no contextual rule and an empty cached shape (air, plants): the scan skips them without asking. Providers that build CollisionBlock themselves (the query-order fixture) leave it false, so their query logs are unchanged. - A shape whose bounds miss the box is skipped before moving it: joinIsNotEmpty(AND) of the two is false anyway. - Shapes.create(box) is built the first time a shape is not the unit cube (finite bounds always make one), not for every scan. - SceneCollisions hands the scan the resident chunk itself, so a cell costs one section read instead of a chunk-map lookup. Results are unchanged: running both paths side by side over 900,000 mob moves in a 600-mob village gave bit-identical movement. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/collisions.rs | 23 ++++++++------- .../examples/block_collision_scan_fixture.rs | 1 + crates/horizon-world/src/block_collision.rs | 11 +++++-- .../horizon-world/src/block_collision_scan.rs | 29 +++++++++++++++++-- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index 9a034d18b..39fde56b5 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -209,21 +209,22 @@ where ) .unwrap_or(false) } -impl BlockCollisionWorld for SceneCollisions<'_, '_> { +impl<'w> BlockCollisionWorld for SceneCollisions<'w, '_> { type Error = io::Error; - type Chunk = ChunkPos; - fn collision_chunk(&mut self, p: ChunkPos) -> io::Result> { - Ok(self.world.chunks.contains_key(&(p.x, p.z)).then_some(p)) + /// The resident column, looked up once per chunk the scan enters. + type Chunk = &'w crate::chunk::Chunk; + fn collision_chunk(&mut self, p: ChunkPos) -> io::Result> { + Ok(self.world.chunks.get(&(p.x, p.z)).map(|chunk| &**chunk)) } - fn collision_block(&mut self, chunk: ChunkPos, p: BlockPos) -> io::Result { - let chunk = self - .world - .chunks - .get(&(chunk.x, chunk.z)) - .ok_or_else(|| io::Error::other("resident collision chunk disappeared"))?; + fn collision_block(&mut self, chunk: Self::Chunk, p: BlockPos) -> io::Result { self.binding.collision_block(chunk.block(p.x, p.y, p.z)) } - fn is_suffocating(&mut self, _: ChunkPos, _: CollisionBlock, _: BlockPos) -> io::Result { + fn is_suffocating( + &mut self, + _: Self::Chunk, + _: CollisionBlock, + _: BlockPos, + ) -> io::Result { Ok(false) } fn context_shape( diff --git a/crates/horizon-world/examples/block_collision_scan_fixture.rs b/crates/horizon-world/examples/block_collision_scan_fixture.rs index 40fbd2182..4c4764c5f 100644 --- a/crates/horizon-world/examples/block_collision_scan_fixture.rs +++ b/crates/horizon-world/examples/block_collision_scan_fixture.rs @@ -192,6 +192,7 @@ fn main() -> Result<(), Box> { state: BlockStateId(i as u32), large_collision_shape: v[1].as_bool().unwrap(), moving_piston: v[2].as_bool().unwrap(), + empty_shape: false, }, shape: shape(v[0].as_u64().unwrap()), suffocating: v[3].as_bool().unwrap(), diff --git a/crates/horizon-world/src/block_collision.rs b/crates/horizon-world/src/block_collision.rs index 73617de71..71c324cdf 100644 --- a/crates/horizon-world/src/block_collision.rs +++ b/crates/horizon-world/src/block_collision.rs @@ -98,8 +98,6 @@ impl<'r> BlockCollisionRegistry<'r> { .into_iter() .any(|axis| shape.min(axis) < 0.0 || shape.max(axis) > 1.0) }); - scan_flags - .push(u8::from(large) | (u8::from(state.name == "minecraft:moving_piston") << 1)); if let Some(rule) = ScaffoldingCollision::from_state(state)? { contextual.insert(id, ContextualRule::Scaffolding(rule)); } else if let Some(rule) = LiquidCollision::from_state(state)? { @@ -107,6 +105,14 @@ impl<'r> BlockCollisionRegistry<'r> { } else if state.name == "minecraft:powder_snow" { contextual.insert(id, ContextualRule::PowderSnow); } + // No contextual rule and an empty cached shape: empty everywhere. + let empty = !contextual.contains_key(&id) + && cached.cached_shape(id)?.is_some_and(VoxelShape::is_empty); + scan_flags.push( + u8::from(large) + | (u8::from(state.name == "minecraft:moving_piston") << 1) + | (u8::from(empty) << 2), + ); if cached.cached_shape(id)?.is_none() { if let Some(rule) = DynamicSupport::from_state(cached.registry().state(id).unwrap())? @@ -138,6 +144,7 @@ impl<'r> BlockCollisionRegistry<'r> { state, large_collision_shape: flags & 1 != 0, moving_piston: flags & 2 != 0, + empty_shape: flags & 4 != 0, }) } pub fn collision_shape( diff --git a/crates/horizon-world/src/block_collision_scan.rs b/crates/horizon-world/src/block_collision_scan.rs index 3b03a64ab..1d8f75227 100644 --- a/crates/horizon-world/src/block_collision_scan.rs +++ b/crates/horizon-world/src/block_collision_scan.rs @@ -11,6 +11,10 @@ pub struct CollisionBlock { pub state: BlockStateId, pub large_collision_shape: bool, pub moving_piston: bool, + /// The shape is empty under every context (air, plants): a scan skips + /// the block without asking for its shape. Providers whose shapes come + /// from elsewhere leave it false. + pub empty_shape: bool, } pub trait BlockCollisionWorld { @@ -72,7 +76,9 @@ pub struct BlockCollisionScan<'w, 'c, W: BlockCollisionWorld> { world: &'w mut W, context: CollisionContext<'c>, bounds: Aabb, - entity_shape: VoxelShape, + /// `Shapes.create(box)`, built the first time a block's shape is not + /// the unit cube (finite bounds always make one). + entity_shape: Option, only_suffocating: bool, start: BlockPos, sizes: [usize; 3], @@ -120,7 +126,7 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { world, context, bounds, - entity_shape: VoxelShape::from_box(bounds).map_err(CollisionScanError::Shape)?, + entity_shape: None, only_suffocating, start, sizes: sizes.map(|s| s as usize), @@ -185,6 +191,9 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { if kind == 1 && !block.large_collision_shape || kind == 2 && !block.moving_piston { continue; } + if block.empty_shape { + continue; + } let shape = self .world .context_shape(block, p, &self.context) @@ -201,10 +210,24 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { } shape.moved(offset) } else { + // Shapes whose bounds miss the box share no volume with it: + // joinIsNotEmpty(AND) is false without moving them. + if !shape + .bounds() + .is_some_and(|b| b.move_by(offset).intersects(self.bounds)) + { + continue; + } let moved = shape.moved(offset); + let entity_shape = match &self.entity_shape { + Some(shape) => shape, + None => self.entity_shape.insert( + VoxelShape::from_box(self.bounds).map_err(CollisionScanError::Shape)?, + ), + }; if moved.is_empty() || !moved - .join_is_not_empty(&self.entity_shape, ShapeOp::AND) + .join_is_not_empty(entity_shape, ShapeOp::AND) .map_err(CollisionScanError::Shape)? { continue;