aboutsummaryrefslogtreecommitdiff
path: root/azalea-physics/src/collision
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-06-11 22:58:41 -0630
committermat <git@matdoes.dev>2025-06-11 22:58:41 -0630
commita2606569bb79867d07a075bcf7b05730e4264d72 (patch)
treefb97fb52aa0c4d7575f6bd5e4e36c2f01c87f942 /azalea-physics/src/collision
parent89ddd5e85f4f2fb98697df15528df6e07a3ddd07 (diff)
downloadazalea-drasl-a2606569bb79867d07a075bcf7b05730e4264d72.tar.xz
use owned instead of borrowed Vec3 more
Diffstat (limited to 'azalea-physics/src/collision')
-rw-r--r--azalea-physics/src/collision/mod.rs79
-rw-r--r--azalea-physics/src/collision/shape.rs16
2 files changed, 38 insertions, 57 deletions
diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs
index ef994deb..41fc6c85 100644
--- a/azalea-physics/src/collision/mod.rs
+++ b/azalea-physics/src/collision/mod.rs
@@ -35,7 +35,7 @@ pub enum MoverType {
// Entity.collide
fn collide(
- movement: &Vec3,
+ movement: Vec3,
world: &Instance,
physics: &azalea_entity::Physics,
source_entity: Option<Entity>,
@@ -51,7 +51,7 @@ fn collide(
collidable_entity_query,
);
let collided_delta = if movement.length_squared() == 0.0 {
- *movement
+ movement
} else {
collide_bounding_box(movement, &entity_bounding_box, world, &entity_collisions)
};
@@ -65,20 +65,20 @@ fn collide(
let max_up_step = 0.6;
if max_up_step > 0. && on_ground && (x_collision || z_collision) {
let mut step_to_delta = collide_bounding_box(
- &movement.with_y(max_up_step),
+ movement.with_y(max_up_step),
&entity_bounding_box,
world,
&entity_collisions,
);
let directly_up_delta = collide_bounding_box(
- &Vec3::ZERO.with_y(max_up_step),
- &entity_bounding_box.expand_towards(&Vec3::new(movement.x, 0., movement.z)),
+ Vec3::ZERO.with_y(max_up_step),
+ &entity_bounding_box.expand_towards(Vec3::new(movement.x, 0., movement.z)),
world,
&entity_collisions,
);
if directly_up_delta.y < max_up_step {
let target_movement = collide_bounding_box(
- &movement.with_y(0.),
+ movement.with_y(0.),
&entity_bounding_box.move_relative(directly_up_delta),
world,
&entity_collisions,
@@ -95,7 +95,7 @@ fn collide(
> collided_delta.horizontal_distance_squared()
{
return step_to_delta.add(collide_bounding_box(
- &Vec3::ZERO.with_y(-step_to_delta.y + movement.y),
+ Vec3::ZERO.with_y(-step_to_delta.y + movement.y),
&entity_bounding_box.move_relative(step_to_delta),
world,
&entity_collisions,
@@ -112,7 +112,7 @@ fn collide(
#[allow(clippy::too_many_arguments)]
pub fn move_colliding(
_mover_type: MoverType,
- movement: &Vec3,
+ movement: Vec3,
world: &Instance,
position: &mut Mut<azalea_entity::Position>,
physics: &mut azalea_entity::Physics,
@@ -180,7 +180,7 @@ pub fn move_colliding(
// TODO: minecraft checks for a "minor" horizontal collision here
- let _block_pos_below = azalea_entity::on_pos_legacy(&world.chunks, position);
+ let _block_pos_below = azalea_entity::on_pos_legacy(&world.chunks, **position);
// let _block_state_below = self
// .world
// .get_block_state(&block_pos_below)
@@ -239,7 +239,7 @@ pub fn move_colliding(
}
fn collide_bounding_box(
- movement: &Vec3,
+ movement: Vec3,
entity_bounding_box: &AABB,
world: &Instance,
entity_collisions: &[VoxelShape],
@@ -259,63 +259,44 @@ fn collide_bounding_box(
}
fn collide_with_shapes(
- movement: &Vec3,
+ mut movement: Vec3,
mut entity_box: AABB,
- collision_boxes: &Vec<VoxelShape>,
+ collision_boxes: &[VoxelShape],
) -> Vec3 {
if collision_boxes.is_empty() {
- return *movement;
+ return movement;
}
- let mut x_movement = movement.x;
- let mut y_movement = movement.y;
- let mut z_movement = movement.z;
- if y_movement != 0. {
- y_movement = Shapes::collide(&Axis::Y, &entity_box, collision_boxes, y_movement);
- if y_movement != 0. {
- entity_box = entity_box.move_relative(Vec3 {
- x: 0.,
- y: y_movement,
- z: 0.,
- });
+ if movement.y != 0. {
+ movement.y = Shapes::collide(Axis::Y, &entity_box, collision_boxes, movement.y);
+ if movement.y != 0. {
+ entity_box = entity_box.move_relative(Vec3::new(0., movement.y, 0.));
}
}
// whether the player is moving more in the z axis than x
// this is done to fix a movement bug, minecraft does this too
- let more_z_movement = x_movement.abs() < z_movement.abs();
-
- if more_z_movement && z_movement != 0. {
- z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement);
- if z_movement != 0. {
- entity_box = entity_box.move_relative(Vec3 {
- x: 0.,
- y: 0.,
- z: z_movement,
- });
+ let more_z_movement = movement.x.abs() < movement.z.abs();
+
+ if more_z_movement && movement.z != 0. {
+ movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z);
+ if movement.z != 0. {
+ entity_box = entity_box.move_relative(Vec3::new(0., 0., movement.z));
}
}
- if x_movement != 0. {
- x_movement = Shapes::collide(&Axis::X, &entity_box, collision_boxes, x_movement);
- if x_movement != 0. {
- entity_box = entity_box.move_relative(Vec3 {
- x: x_movement,
- y: 0.,
- z: 0.,
- });
+ if movement.x != 0. {
+ movement.x = Shapes::collide(Axis::X, &entity_box, collision_boxes, movement.x);
+ if movement.x != 0. {
+ entity_box = entity_box.move_relative(Vec3::new(movement.x, 0., 0.));
}
}
- if !more_z_movement && z_movement != 0. {
- z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement);
+ if !more_z_movement && movement.z != 0. {
+ movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z);
}
- Vec3 {
- x: x_movement,
- y: y_movement,
- z: z_movement,
- }
+ movement
}
/// Get the [`VoxelShape`] for the given fluid state.
diff --git a/azalea-physics/src/collision/shape.rs b/azalea-physics/src/collision/shape.rs
index 4d430ee7..9caae590 100644
--- a/azalea-physics/src/collision/shape.rs
+++ b/azalea-physics/src/collision/shape.rs
@@ -98,9 +98,9 @@ impl Shapes {
}
pub fn collide(
- axis: &Axis,
+ axis: Axis,
entity_box: &AABB,
- collision_boxes: &Vec<VoxelShape>,
+ collision_boxes: &[VoxelShape],
mut movement: f64,
) -> f64 {
for shape in collision_boxes {
@@ -408,7 +408,7 @@ impl VoxelShape {
}
}
- pub fn clip(&self, from: &Vec3, to: &Vec3, block_pos: BlockPos) -> Option<BlockHitResult> {
+ pub fn clip(&self, from: Vec3, to: Vec3, block_pos: BlockPos) -> Option<BlockHitResult> {
if self.is_empty() {
return None;
}
@@ -416,7 +416,7 @@ impl VoxelShape {
if vector.length_squared() < EPSILON {
return None;
}
- let right_after_start = from + &(vector * 0.001);
+ 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),
@@ -436,8 +436,8 @@ impl VoxelShape {
}
}
- 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(&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 {
if self.shape().is_empty() {
@@ -753,8 +753,8 @@ mod tests {
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.),
+ Vec3::new(-0.3, 0.5, 0.),
+ Vec3::new(5.3, 0.5, 0.),
BlockPos::new(0, 0, 0),
)
.unwrap();