aboutsummaryrefslogtreecommitdiff
path: root/azalea-physics/src/collision/world_collisions.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-03-16 13:41:17 -0500
committerGitHub <noreply@github.com>2025-03-16 13:41:17 -0500
commitb0bd992adcff71ee294dd05060e00e652f62a7b2 (patch)
tree0a36d8b37befbc75c8c65cc2c8779c3df66bd87b /azalea-physics/src/collision/world_collisions.rs
parenta95408cbcc05b5bd04a084b0a286b571069206f6 (diff)
downloadazalea-drasl-b0bd992adcff71ee294dd05060e00e652f62a7b2.tar.xz
Fluid physics fixes (#210)
* start fixing code related to fluid physics * implement force_solid for blocks * afk pool test
Diffstat (limited to 'azalea-physics/src/collision/world_collisions.rs')
-rw-r--r--azalea-physics/src/collision/world_collisions.rs182
1 files changed, 151 insertions, 31 deletions
diff --git a/azalea-physics/src/collision/world_collisions.rs b/azalea-physics/src/collision/world_collisions.rs
index 3aede743..ded31275 100644
--- a/azalea-physics/src/collision/world_collisions.rs
+++ b/azalea-physics/src/collision/world_collisions.rs
@@ -1,19 +1,21 @@
-use std::sync::Arc;
+use std::{collections::HashSet, sync::Arc};
-use azalea_block::BlockState;
+use azalea_block::{BlockState, fluid_state::FluidState};
use azalea_core::{
- cursor3d::{Cursor3d, CursorIterationType},
+ cursor3d::{Cursor3d, CursorIteration, CursorIterationType},
math::EPSILON,
- position::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos, ChunkSectionPos},
+ position::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos, ChunkSectionPos, Vec3},
};
+use azalea_inventory::ItemStack;
use azalea_world::{Chunk, Instance};
+use bevy_ecs::entity::Entity;
use parking_lot::RwLock;
use super::{BLOCK_SHAPE, Shapes};
use crate::collision::{AABB, BlockWithShape, VoxelShape};
-pub fn get_block_collisions(world: &Instance, aabb: AABB) -> Vec<VoxelShape> {
- let mut state = BlockCollisionsState::new(world, aabb);
+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();
let initial_chunk_pos = ChunkPos::from(state.cursor.origin());
@@ -21,25 +23,80 @@ pub fn get_block_collisions(world: &Instance, aabb: AABB) -> Vec<VoxelShape> {
let initial_chunk = initial_chunk.as_deref().map(RwLock::read);
while let Some(item) = state.cursor.next() {
+ state.compute_next(
+ item,
+ &mut block_collisions,
+ initial_chunk_pos,
+ initial_chunk.as_deref(),
+ );
+ }
+
+ block_collisions
+}
+
+pub fn get_block_and_liquid_collisions(world: &Instance, aabb: &AABB) -> Vec<VoxelShape> {
+ let mut state = BlockCollisionsState::new(
+ world,
+ aabb,
+ EntityCollisionContext::of(None).with_include_liquids(true),
+ );
+ let mut block_collisions = Vec::new();
+
+ let initial_chunk_pos = ChunkPos::from(state.cursor.origin());
+ let initial_chunk = world.chunks.get(&initial_chunk_pos);
+ let initial_chunk = initial_chunk.as_deref().map(RwLock::read);
+
+ while let Some(item) = state.cursor.next() {
+ state.compute_next(
+ item,
+ &mut block_collisions,
+ initial_chunk_pos,
+ initial_chunk.as_deref(),
+ );
+ }
+
+ block_collisions
+}
+
+pub struct BlockCollisionsState<'a> {
+ pub world: &'a Instance,
+ pub aabb: &'a AABB,
+ pub entity_shape: VoxelShape,
+ pub cursor: Cursor3d,
+
+ _context: EntityCollisionContext,
+
+ cached_sections: Vec<(ChunkSectionPos, azalea_world::Section)>,
+ cached_block_shapes: Vec<(BlockState, &'static VoxelShape)>,
+}
+
+impl<'a> BlockCollisionsState<'a> {
+ fn compute_next(
+ &mut self,
+ item: CursorIteration,
+ block_collisions: &mut Vec<VoxelShape>,
+ initial_chunk_pos: ChunkPos,
+ initial_chunk: Option<&Chunk>,
+ ) {
if item.iteration_type == CursorIterationType::Corner {
- continue;
+ return;
}
let item_chunk_pos = ChunkPos::from(item.pos);
let block_state: BlockState = if item_chunk_pos == initial_chunk_pos {
match &initial_chunk {
Some(initial_chunk) => initial_chunk
- .get(&ChunkBlockPos::from(item.pos), state.world.chunks.min_y)
+ .get(&ChunkBlockPos::from(item.pos), self.world.chunks.min_y)
.unwrap_or(BlockState::AIR),
_ => BlockState::AIR,
}
} else {
- state.get_block_state(item.pos)
+ self.get_block_state(item.pos)
};
if block_state.is_air() {
// fast path since we can't collide with air
- continue;
+ return;
}
// TODO: continue if self.only_suffocating_blocks and the block is not
@@ -47,43 +104,29 @@ pub fn get_block_collisions(world: &Instance, aabb: AABB) -> Vec<VoxelShape> {
// if it's a full block do a faster collision check
if block_state.is_collision_shape_full() {
- if !state.aabb.intersects_aabb(&AABB {
+ if !self.aabb.intersects_aabb(&AABB {
min: item.pos.to_vec3_floored(),
max: (item.pos + 1).to_vec3_floored(),
}) {
- continue;
+ return;
}
block_collisions.push(BLOCK_SHAPE.move_relative(item.pos.to_vec3_floored()));
- continue;
+ return;
}
- let block_shape = state.get_block_shape(block_state);
+ let block_shape = self.get_block_shape(block_state);
let block_shape = block_shape.move_relative(item.pos.to_vec3_floored());
// if the entity shape and block shape don't collide, continue
- if !Shapes::matches_anywhere(&block_shape, &state.entity_shape, |a, b| a && b) {
- continue;
+ if !Shapes::matches_anywhere(&block_shape, &self.entity_shape, |a, b| a && b) {
+ return;
}
block_collisions.push(block_shape);
}
- block_collisions
-}
-
-pub struct BlockCollisionsState<'a> {
- pub world: &'a Instance,
- pub aabb: AABB,
- pub entity_shape: VoxelShape,
- pub cursor: Cursor3d,
-
- cached_sections: Vec<(ChunkSectionPos, azalea_world::Section)>,
- cached_block_shapes: Vec<(BlockState, &'static VoxelShape)>,
-}
-
-impl<'a> BlockCollisionsState<'a> {
- pub fn new(world: &'a Instance, aabb: AABB) -> 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,
@@ -104,6 +147,8 @@ impl<'a> BlockCollisionsState<'a> {
entity_shape: VoxelShape::from(aabb),
cursor,
+ _context: context,
+
cached_sections: Vec::new(),
cached_block_shapes: Vec::new(),
}
@@ -182,3 +227,78 @@ impl<'a> BlockCollisionsState<'a> {
shape
}
}
+
+pub struct EntityCollisionContext {
+ pub descending: bool,
+ pub entity_bottom: f64,
+ pub held_item: ItemStack,
+ can_stand_on_fluid_predicate: CanStandOnFluidPredicate,
+ pub entity: Option<Entity>,
+}
+
+impl EntityCollisionContext {
+ pub fn of(entity: Option<Entity>) -> Self {
+ Self {
+ descending: false,
+ entity_bottom: 0.0,
+ held_item: ItemStack::Empty,
+ can_stand_on_fluid_predicate: CanStandOnFluidPredicate::PassToEntity,
+ entity,
+ }
+ }
+ pub fn with_include_liquids(mut self, include_liquids: bool) -> Self {
+ self.can_stand_on_fluid_predicate = if include_liquids {
+ CanStandOnFluidPredicate::AlwaysTrue
+ } else {
+ CanStandOnFluidPredicate::PassToEntity
+ };
+ self
+ }
+
+ pub fn can_stand_on_fluid(&self, above: &FluidState, target: &FluidState) -> bool {
+ self.can_stand_on_fluid_predicate.matches(target) && !above.is_same_kind(target)
+ }
+}
+
+enum CanStandOnFluidPredicate {
+ PassToEntity,
+ AlwaysTrue,
+}
+impl CanStandOnFluidPredicate {
+ pub fn matches(&self, _state: &FluidState) -> bool {
+ match self {
+ Self::AlwaysTrue => true,
+ // minecraft sometimes returns true for striders here, false for every other entity
+ // though
+ Self::PassToEntity => false,
+ }
+ }
+}
+
+/// This basically gets all the chunks that an entity colliding with
+/// that bounding box could be in.
+///
+/// This is forEachAccessibleNonEmptySection in vanilla Minecraft because they
+/// sort entities into sections instead of just chunks. In theory this might be
+/// a performance loss for Azalea. If this ever turns out to be a bottleneck,
+/// then maybe you should try having it do that instead.
+pub fn for_entities_in_chunks_colliding_with(
+ world: &Instance,
+ aabb: &AABB,
+ mut consumer: impl FnMut(ChunkPos, &HashSet<Entity>),
+) {
+ let min_section = ChunkSectionPos::from(aabb.min - Vec3::new(2., 4., 2.));
+ let max_section = ChunkSectionPos::from(aabb.max + Vec3::new(2., 0., 2.));
+
+ let min_chunk = ChunkPos::from(min_section);
+ let max_chunk = ChunkPos::from(max_section);
+
+ for chunk_x in min_chunk.x..=max_chunk.x {
+ for chunk_z in min_chunk.z..=max_chunk.z {
+ let chunk_pos = ChunkPos::new(chunk_x, chunk_z);
+ if let Some(entities) = world.entities_by_chunk.get(&chunk_pos) {
+ consumer(chunk_pos, entities);
+ }
+ }
+ }
+}