1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

fix collisions bugs

This commit is contained in:
mat 2025-06-02 19:24:39 -13:00
parent 61443fa481
commit cc3e64a315
4 changed files with 43 additions and 26 deletions

View file

@ -338,13 +338,6 @@ impl Client {
/// Similar to [`Self::get_component`], but doesn't clone the component
/// since it's passed as a reference. [`Self::ecs`] will remain locked
/// while the callback is being run.
///
/// ```
/// # use azalea_client::{Client, mining::Mining};
/// # fn example(bot: &Client) {
/// let is_mining = bot.map_get_component::<Mining, _>(|m| m).is_some();
/// # }
/// ```
pub fn map_get_component<T: Component, R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
let mut ecs = self.ecs.lock();
let value = self.query::<Option<&T>>(&mut ecs);

View file

@ -455,7 +455,12 @@ impl EntityBundle {
world_name: ResourceLocation,
) -> Self {
let dimensions = EntityDimensions::from(kind);
let eye_height = dimensions.height * 0.85;
let eye_height = match kind {
// TODO: codegen hardcoded eye heights, search withEyeHeight with mojmap
// also, eye height should change depending on pose (like sneaking, swimming, etc)
azalea_registry::EntityKind::Player => 1.62,
_ => dimensions.height * 0.85,
};
Self {
kind: EntityKind(kind),

View file

@ -399,17 +399,8 @@ impl VoxelShape {
}
pub fn find_index(&self, axis: Axis, coord: f64) -> i32 {
// let r = binary_search(0, (self.shape().size(axis) + 1) as i32, &|t| {
// coord < self.get(axis, t as usize)
// }) - 1;
// r
match self {
VoxelShape::Cube(s) => s.find_index(axis, coord),
_ => {
let upper_limit = (self.shape().size(axis) + 1) as i32;
binary_search(0, upper_limit, |t| coord < self.get(axis, t as usize)) - 1
}
}
let upper_limit = (self.shape().size(axis) + 1) as i32;
binary_search(0, upper_limit, |t| coord < self.get(axis, t as usize)) - 1
}
pub fn clip(&self, from: &Vec3, to: &Vec3, block_pos: &BlockPos) -> Option<BlockHitResult> {
@ -420,7 +411,7 @@ impl VoxelShape {
if vector.length_squared() < EPSILON {
return None;
}
let right_after_start = from + &(vector * 0.0001);
let right_after_start = from + &(vector * 0.001);
if self.shape().is_full_wide(
self.find_index(Axis::X, right_after_start.x - block_pos.x as f64),
@ -645,7 +636,6 @@ impl ArrayVoxelShape {
impl CubeVoxelShape {
pub fn new(shape: DiscreteVoxelShape) -> Self {
// pre-calculate the coor
let x_coords = Self::calculate_coords(&shape, Axis::X);
let y_coords = Self::calculate_coords(&shape, Axis::Y);
let z_coords = Self::calculate_coords(&shape, Axis::Z);
@ -679,10 +669,11 @@ impl CubeVoxelShape {
axis.choose(&self.x_coords, &self.y_coords, &self.z_coords)
}
fn find_index(&self, axis: Axis, coord: f64) -> i32 {
let n = self.shape().size(axis);
(f64::clamp(coord * (n as f64), -1f64, n as f64)) as i32
}
// unused
// fn find_index(&self, axis: Axis, coord: f64) -> i32 {
// let n = self.shape().size(axis);
// (f64::clamp(coord * (n as f64), -1f64, n as f64)) as i32
// }
}
#[derive(Debug)]
@ -752,4 +743,32 @@ mod tests {
let joined = Shapes::matches_anywhere(&shape, &shape2, |a, b| a && b);
assert!(joined, "Shapes should intersect");
}
#[test]
fn clip_in_front_of_block() {
let block_shape = &*BLOCK_SHAPE;
let block_hit_result = block_shape
.clip(
&Vec3::new(-0.3, 0.5, 0.),
&Vec3::new(5.3, 0.5, 0.),
&BlockPos::new(0, 0, 0),
)
.unwrap();
assert_eq!(
block_hit_result,
BlockHitResult {
location: Vec3 {
x: 0.0,
y: 0.5,
z: 0.0
},
direction: Direction::West,
block_pos: BlockPos { x: 0, y: 0, z: 0 },
inside: false,
world_border: false,
miss: false
}
);
}
}

View file

@ -1569,7 +1569,7 @@ mod tests {
simulation.app.world_mut().send_event(GotoEvent {
entity: simulation.entity,
goal: Arc::new(BlockPosGoal(BlockPos::new(0, 70, 0))),
goal: Arc::new(BlockPosGoal(BlockPos::new(0, 69, 0))),
successors_fn: moves::default_move,
allow_mining: true,
min_timeout: PathfinderTimeout::Nodes(1_000_000),