diff options
| author | mat <git@matdoes.dev> | 2025-09-27 18:41:36 -1030 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-09-27 20:26:41 -0845 |
| commit | 74dcb7b37d953ec4d45dd6aac4c8e598c87def7d (patch) | |
| tree | 22e6ab1849ee01d40f3d484ea0c8db52aa6649b5 /azalea-physics/src/collision | |
| parent | 63bef6486dc1f3c9ef4b4f0d71e139d7035b6432 (diff) | |
| download | azalea-drasl-74dcb7b37d953ec4d45dd6aac4c8e598c87def7d.tar.xz | |
allow picking other bots in hit results
Diffstat (limited to 'azalea-physics/src/collision')
| -rw-r--r-- | azalea-physics/src/collision/entity_collisions.rs | 53 | ||||
| -rw-r--r-- | azalea-physics/src/collision/mod.rs | 24 | ||||
| -rw-r--r-- | azalea-physics/src/collision/shape.rs | 26 | ||||
| -rw-r--r-- | azalea-physics/src/collision/world_collisions.rs | 14 |
4 files changed, 70 insertions, 47 deletions
diff --git a/azalea-physics/src/collision/entity_collisions.rs b/azalea-physics/src/collision/entity_collisions.rs index 1300cf34..af2db1f0 100644 --- a/azalea-physics/src/collision/entity_collisions.rs +++ b/azalea-physics/src/collision/entity_collisions.rs @@ -1,13 +1,14 @@ -use azalea_core::aabb::AABB; +use azalea_core::aabb::Aabb; use azalea_entity::{ - LocalEntity, Physics, + Physics, metadata::{AbstractBoat, Shulker}, }; use azalea_world::Instance; use bevy_ecs::{ + component::Component, entity::Entity, - query::{Or, With, Without}, - system::Query, + query::{Changed, Or, With}, + system::{Commands, Query}, }; use tracing::error; @@ -26,14 +27,34 @@ pub type CollidableEntityQuery<'world, 'state> = Query<'world, 'state, (), Colli /// this. pub type CollidableEntityFilter = Or<(With<AbstractBoat>, With<Shulker>)>; -pub type PhysicsQuery<'world, 'state, 'a> = - Query<'world, 'state, &'a Physics, Without<LocalEntity>>; +/// A component that mirrors the Physics::bounding_box of every entity, but is +/// updated before client-side physics is done. +#[derive(Component)] +pub struct LastBoundingBox(pub Aabb); + +pub type AabbQuery<'world, 'state, 'a> = Query<'world, 'state, &'a LastBoundingBox>; + +/// Update the [`LastBoundingBox`] for every entity. +pub fn update_last_bounding_box( + mut commands: Commands, + mut query: Query<(Entity, Option<&mut LastBoundingBox>, &Physics), Changed<Physics>>, +) { + for (entity, mut last_bounding_box, physics) in &mut query { + if let Some(last_bounding_box) = last_bounding_box.as_mut() { + last_bounding_box.0 = physics.bounding_box; + } else { + commands + .entity(entity) + .insert(LastBoundingBox(physics.bounding_box)); + } + } +} pub fn get_entity_collisions( world: &Instance, - aabb: &AABB, + aabb: &Aabb, source_entity: Option<Entity>, - physics_query: &PhysicsQuery, + aabb_query: &AabbQuery, collidable_entity_query: &CollidableEntityQuery, ) -> Vec<VoxelShape> { if aabb.size() < 1.0E-7 { @@ -47,7 +68,7 @@ pub fn get_entity_collisions( source_entity, &aabb.inflate_all(1.0E-7), &collision_predicate, - physics_query, + aabb_query, ); collidable_entities @@ -64,10 +85,10 @@ pub fn get_entity_collisions( pub fn get_entities( world: &Instance, source_entity: Option<Entity>, - aabb: &AABB, + aabb: &Aabb, predicate: &dyn Fn(Entity) -> bool, - physics_query: &PhysicsQuery, -) -> Vec<(Entity, AABB)> { + aabb_query: &AabbQuery, +) -> Vec<(Entity, Aabb)> { let mut matches = Vec::new(); super::world_collisions::for_entities_in_chunks_colliding_with( @@ -77,16 +98,16 @@ pub fn get_entities( // now check if the entity itself collides for &candidate in entities_in_chunk { if Some(candidate) != source_entity && predicate(candidate) { - let Ok(physics) = physics_query.get(candidate) else { + let Ok(candidate_aabb) = aabb_query.get(candidate) else { error!( "Entity {candidate} (found from for_entities_in_chunks_colliding_with) is missing required components." ); continue; }; + let candidate_aabb = &candidate_aabb.0; - let candidate_aabb = physics.bounding_box; - if aabb.intersects_aabb(&candidate_aabb) { - matches.push((candidate, physics.bounding_box)); + if aabb.intersects_aabb(candidate_aabb) { + matches.push((candidate, candidate_aabb.to_owned())); } } } diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs index d8ac3a95..3702c8c1 100644 --- a/azalea-physics/src/collision/mod.rs +++ b/azalea-physics/src/collision/mod.rs @@ -9,7 +9,7 @@ use std::{ops::Add, sync::LazyLock}; use azalea_block::{BlockState, fluid_state::FluidState}; use azalea_core::{ - aabb::AABB, + aabb::Aabb, direction::Axis, math::{self, EPSILON}, position::{BlockPos, Vec3}, @@ -22,12 +22,14 @@ use azalea_world::{ChunkStorage, Instance, MoveEntityError}; use bevy_ecs::{entity::Entity, world::Mut}; pub use blocks::BlockWithShape; pub use discrete_voxel_shape::*; -use entity_collisions::{CollidableEntityQuery, PhysicsQuery, get_entity_collisions}; +use entity_collisions::{CollidableEntityQuery, get_entity_collisions}; pub use shape::*; use tracing::warn; use self::world_collisions::get_block_collisions; -use crate::{local_player::PhysicsState, travel::no_collision}; +use crate::{ + collision::entity_collisions::AabbQuery, local_player::PhysicsState, travel::no_collision, +}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum MoverType { @@ -45,7 +47,7 @@ fn collide(ctx: &MoveCtx, movement: Vec3) -> Vec3 { ctx.world, &entity_bounding_box.expand_towards(movement), Some(ctx.source_entity), - ctx.physics_query, + ctx.aabb_query, ctx.collidable_entity_query, ); let world = ctx.world; @@ -111,7 +113,7 @@ pub struct MoveCtx<'world, 'state, 'a, 'b> { pub position: Mut<'a, Position>, pub physics: &'a mut Physics, pub source_entity: Entity, - pub physics_query: &'a PhysicsQuery<'world, 'state, 'b>, + pub aabb_query: &'a AabbQuery<'world, 'state, 'b>, pub collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>, pub physics_state: Option<&'a PhysicsState>, pub attributes: &'a Attributes, @@ -269,7 +271,7 @@ fn maybe_back_off_from_edge(move_ctx: &mut MoveCtx, mut movement: Vec3) -> Vec3 physics: move_ctx.physics, world: move_ctx.world, source_entity: move_ctx.source_entity, - physics_query: move_ctx.physics_query, + aabb_query: move_ctx.aabb_query, collidable_entity_query: move_ctx.collidable_entity_query, }; @@ -340,7 +342,7 @@ pub struct CanFallAtLeastCtx<'world, 'state, 'a, 'b> { physics: &'a Physics, world: &'a Instance, source_entity: Entity, - physics_query: &'a PhysicsQuery<'world, 'state, 'b>, + aabb_query: &'a AabbQuery<'world, 'state, 'b>, collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>, } @@ -351,7 +353,7 @@ fn can_fall_at_least( max_up_step: f64, ) -> bool { let aabb = ctx.physics.bounding_box; - let aabb = AABB { + let aabb = Aabb { min: Vec3 { x: aabb.min.x + EPSILON + delta_x, y: aabb.min.y - max_up_step - EPSILON, @@ -366,7 +368,7 @@ fn can_fall_at_least( no_collision( ctx.world, Some(ctx.source_entity), - ctx.physics_query, + ctx.aabb_query, ctx.collidable_entity_query, ctx.physics, &aabb, @@ -376,7 +378,7 @@ fn can_fall_at_least( fn collide_bounding_box( movement: Vec3, - entity_bounding_box: &AABB, + entity_bounding_box: &Aabb, world: &Instance, entity_collisions: &[VoxelShape], ) -> Vec3 { @@ -396,7 +398,7 @@ fn collide_bounding_box( fn collide_with_shapes( mut movement: Vec3, - mut entity_box: AABB, + mut entity_box: Aabb, collision_boxes: &[VoxelShape], ) -> Vec3 { if collision_boxes.is_empty() { diff --git a/azalea-physics/src/collision/shape.rs b/azalea-physics/src/collision/shape.rs index 9caae590..96506922 100644 --- a/azalea-physics/src/collision/shape.rs +++ b/azalea-physics/src/collision/shape.rs @@ -8,7 +8,7 @@ use azalea_core::{ }; use super::mergers::IndexMerger; -use crate::collision::{AABB, BitSetDiscreteVoxelShape, DiscreteVoxelShape}; +use crate::collision::{Aabb, BitSetDiscreteVoxelShape, DiscreteVoxelShape}; pub struct Shapes; @@ -99,7 +99,7 @@ impl Shapes { pub fn collide( axis: Axis, - entity_box: &AABB, + entity_box: &Aabb, collision_boxes: &[VoxelShape], mut movement: f64, ) -> f64 { @@ -432,14 +432,14 @@ impl VoxelShape { world_border: false, }) } else { - AABB::clip_iterable(&self.to_aabbs(), from, to, block_pos) + Aabb::clip_iterable(&self.to_aabbs(), from, to, block_pos) } } - pub fn collide(&self, axis: Axis, entity_box: &AABB, movement: f64) -> f64 { + pub fn collide(&self, axis: Axis, entity_box: &Aabb, movement: f64) -> f64 { self.collide_x(AxisCycle::between(axis, Axis::X), entity_box, movement) } - pub fn collide_x(&self, axis_cycle: AxisCycle, entity_box: &AABB, mut movement: f64) -> f64 { + pub fn collide_x(&self, axis_cycle: AxisCycle, entity_box: &Aabb, mut movement: f64) -> f64 { if self.shape().is_empty() { return movement; } @@ -550,10 +550,10 @@ impl VoxelShape { ); } - pub fn to_aabbs(&self) -> Vec<AABB> { + pub fn to_aabbs(&self) -> Vec<Aabb> { let mut aabbs = Vec::new(); self.for_all_boxes(|min_x, min_y, min_z, max_x, max_y, max_z| { - aabbs.push(AABB { + aabbs.push(Aabb { min: Vec3::new(min_x, min_y, min_z), max: Vec3::new(max_x, max_y, max_z), }); @@ -561,24 +561,24 @@ impl VoxelShape { aabbs } - pub fn bounds(&self) -> AABB { + pub fn bounds(&self) -> Aabb { assert!(!self.is_empty(), "Can't get bounds for empty shape"); - AABB { + Aabb { min: Vec3::new(self.min(Axis::X), self.min(Axis::Y), self.min(Axis::Z)), max: Vec3::new(self.max(Axis::X), self.max(Axis::Y), self.max(Axis::Z)), } } } -impl From<&AABB> for VoxelShape { - fn from(aabb: &AABB) -> Self { +impl From<&Aabb> for VoxelShape { + fn from(aabb: &Aabb) -> Self { box_shape( aabb.min.x, aabb.min.y, aabb.min.z, aabb.max.x, aabb.max.y, aabb.max.z, ) } } -impl From<AABB> for VoxelShape { - fn from(aabb: AABB) -> Self { +impl From<Aabb> for VoxelShape { + fn from(aabb: Aabb) -> Self { VoxelShape::from(&aabb) } } diff --git a/azalea-physics/src/collision/world_collisions.rs b/azalea-physics/src/collision/world_collisions.rs index 11e8946f..cd883649 100644 --- a/azalea-physics/src/collision/world_collisions.rs +++ b/azalea-physics/src/collision/world_collisions.rs @@ -12,9 +12,9 @@ use bevy_ecs::entity::Entity; use parking_lot::RwLock; use super::{BLOCK_SHAPE, Shapes}; -use crate::collision::{AABB, BlockWithShape, VoxelShape}; +use crate::collision::{Aabb, BlockWithShape, VoxelShape}; -pub fn get_block_collisions(world: &Instance, aabb: &AABB) -> Vec<VoxelShape> { +pub fn get_block_collisions(world: &Instance, aabb: &Aabb) -> Vec<VoxelShape> { let mut state = BlockCollisionsState::new(world, aabb, EntityCollisionContext::of(None)); let mut block_collisions = Vec::new(); @@ -34,7 +34,7 @@ pub fn get_block_collisions(world: &Instance, aabb: &AABB) -> Vec<VoxelShape> { block_collisions } -pub fn get_block_and_liquid_collisions(world: &Instance, aabb: &AABB) -> Vec<VoxelShape> { +pub fn get_block_and_liquid_collisions(world: &Instance, aabb: &Aabb) -> Vec<VoxelShape> { let mut state = BlockCollisionsState::new( world, aabb, @@ -60,7 +60,7 @@ pub fn get_block_and_liquid_collisions(world: &Instance, aabb: &AABB) -> Vec<Vox pub struct BlockCollisionsState<'a> { pub world: &'a Instance, - pub aabb: &'a AABB, + pub aabb: &'a Aabb, pub entity_shape: VoxelShape, pub cursor: Cursor3d, @@ -104,7 +104,7 @@ impl<'a> BlockCollisionsState<'a> { // if it's a full block do a faster collision check if block_state.is_collision_shape_full() { - if !self.aabb.intersects_aabb(&AABB { + if !self.aabb.intersects_aabb(&Aabb { min: item.pos.to_vec3_floored(), max: (item.pos + 1).to_vec3_floored(), }) { @@ -126,7 +126,7 @@ impl<'a> BlockCollisionsState<'a> { block_collisions.push(block_shape); } - pub fn new(world: &'a Instance, aabb: &'a AABB, context: EntityCollisionContext) -> Self { + pub fn new(world: &'a Instance, aabb: &'a Aabb, context: EntityCollisionContext) -> Self { let origin = BlockPos { x: (aabb.min.x - EPSILON).floor() as i32 - 1, y: (aabb.min.y - EPSILON).floor() as i32 - 1, @@ -284,7 +284,7 @@ impl CanStandOnFluidPredicate { /// then maybe you should try having it do that instead. pub fn for_entities_in_chunks_colliding_with( world: &Instance, - aabb: &AABB, + aabb: &Aabb, mut consumer: impl FnMut(ChunkPos, &HashSet<Entity>), ) { let min_section = ChunkSectionPos::from(aabb.min - Vec3::new(2., 4., 2.)); |
