azalea_physics/collision/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
mod blocks;
mod discrete_voxel_shape;
mod mergers;
mod shape;
mod world_collisions;
use std::ops::Add;
use azalea_core::{aabb::AABB, direction::Axis, math::EPSILON, position::Vec3};
use azalea_world::{Instance, MoveEntityError};
use bevy_ecs::world::Mut;
pub use blocks::BlockWithShape;
pub use discrete_voxel_shape::*;
pub use shape::*;
use self::world_collisions::get_block_collisions;
pub enum MoverType {
Own,
Player,
Piston,
ShulkerBox,
Shulker,
}
// private Vec3 collide(Vec3 var1) {
// AABB var2 = this.getBoundingBox();
// List var3 = this.level.getEntityCollisions(this,
// var2.expandTowards(var1)); Vec3 var4 = var1.lengthSqr() == 0.0D ?
// var1 : collideBoundingBox(this, var1, var2, this.level, var3);
// boolean var5 = var1.x != var4.x;
// boolean var6 = var1.y != var4.y;
// boolean var7 = var1.z != var4.z;
// boolean var8 = this.onGround || var6 && var1.y < 0.0D;
// if (this.maxUpStep > 0.0F && var8 && (var5 || var7)) {
// Vec3 var9 = collideBoundingBox(this, new Vec3(var1.x,
// (double)this.maxUpStep, var1.z), var2, this.level, var3); Vec3
// var10 = collideBoundingBox(this, new Vec3(0.0D, (double)this.maxUpStep,
// 0.0D), var2.expandTowards(var1.x, 0.0D, var1.z), this.level, var3);
// if (var10.y < (double)this.maxUpStep) {
// Vec3 var11 = collideBoundingBox(this, new Vec3(var1.x, 0.0D,
// var1.z), var2.move(var10), this.level, var3).add(var10); if
// (var11.horizontalDistanceSqr() > var9.horizontalDistanceSqr()) {
// var9 = var11;
// }
// }
// if (var9.horizontalDistanceSqr() > var4.horizontalDistanceSqr()) {
// return var9.add(collideBoundingBox(this, new Vec3(0.0D, -var9.y +
// var1.y, 0.0D), var2.move(var9), this.level, var3)); }
// }
// return var4;
// }
fn collide(movement: &Vec3, world: &Instance, physics: &azalea_entity::Physics) -> Vec3 {
let entity_bounding_box = physics.bounding_box;
// TODO: get_entity_collisions
// let entity_collisions = world.get_entity_collisions(self,
// entity_bounding_box.expand_towards(movement));
let entity_collisions = Vec::new();
let collided_delta = if movement.length_sqr() == 0.0 {
*movement
} else {
collide_bounding_box(
movement,
&entity_bounding_box,
world,
entity_collisions.clone(),
)
};
let x_collision = movement.x != collided_delta.x;
let y_collision = movement.y != collided_delta.y;
let z_collision = movement.z != collided_delta.z;
let on_ground = physics.on_ground || y_collision && movement.y < 0.;
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(
&Vec3 {
x: movement.x,
y: max_up_step,
z: movement.z,
},
&entity_bounding_box,
world,
entity_collisions.clone(),
);
let directly_up_delta = collide_bounding_box(
&Vec3 {
x: 0.,
y: max_up_step,
z: 0.,
},
&entity_bounding_box.expand_towards(&Vec3::new(movement.x, 0., movement.z)),
world,
entity_collisions.clone(),
);
if directly_up_delta.y < max_up_step {
let target_movement = collide_bounding_box(
&Vec3 {
x: movement.x,
y: 0.,
z: movement.z,
},
&entity_bounding_box.move_relative(&directly_up_delta),
world,
entity_collisions.clone(),
)
.add(directly_up_delta);
if target_movement.horizontal_distance_sqr() > step_to_delta.horizontal_distance_sqr() {
step_to_delta = target_movement;
}
}
if step_to_delta.horizontal_distance_sqr() > collided_delta.horizontal_distance_sqr() {
return step_to_delta.add(collide_bounding_box(
&Vec3 {
x: 0.,
y: -step_to_delta.y + movement.y,
z: 0.,
},
&entity_bounding_box.move_relative(&step_to_delta),
world,
entity_collisions.clone(),
));
}
}
collided_delta
}
/// Move an entity by a given delta, checking for collisions.
pub fn move_colliding(
_mover_type: &MoverType,
movement: &Vec3,
world: &Instance,
position: &mut Mut<azalea_entity::Position>,
physics: &mut azalea_entity::Physics,
) -> Result<(), MoveEntityError> {
// TODO: do all these
// if self.no_physics {
// return;
// };
// if (var1 == MoverType.PISTON) {
// var2 = this.limitPistonMovement(var2);
// if (var2.equals(Vec3.ZERO)) {
// return;
// }
// }
// if (this.stuckSpeedMultiplier.lengthSqr() > 1.0E-7D) {
// var2 = var2.multiply(this.stuckSpeedMultiplier);
// this.stuckSpeedMultiplier = Vec3.ZERO;
// this.setDeltaMovement(Vec3.ZERO);
// }
// movement = this.maybeBackOffFromEdge(movement, moverType);
let collide_result = collide(movement, world, physics);
let move_distance = collide_result.length_sqr();
if move_distance > EPSILON {
// TODO: fall damage
let new_pos = {
Vec3 {
x: position.x + collide_result.x,
y: position.y + collide_result.y,
z: position.z + collide_result.z,
}
};
if new_pos != ***position {
***position = new_pos;
}
}
let x_collision = movement.x != collide_result.x;
let z_collision = movement.z != collide_result.z;
let horizontal_collision = x_collision || z_collision;
let vertical_collision = movement.y != collide_result.y;
let on_ground = vertical_collision && movement.y < 0.;
physics.horizontal_collision = horizontal_collision;
physics.vertical_collision = vertical_collision;
physics.on_ground = on_ground;
// TODO: minecraft checks for a "minor" horizontal collision here
let _block_pos_below = azalea_entity::on_pos_legacy(&world.chunks, position);
// let _block_state_below = self
// .world
// .get_block_state(&block_pos_below)
// .expect("Couldn't get block state below");
// self.check_fall_damage(collide_result.y, on_ground, block_state_below,
// block_pos_below);
// if self.isRemoved() { return; }
if horizontal_collision {
let delta_movement = &physics.velocity;
physics.velocity = Vec3 {
x: if x_collision { 0. } else { delta_movement.x },
y: delta_movement.y,
z: if z_collision { 0. } else { delta_movement.z },
}
}
if vertical_collision {
// blockBelow.updateEntityAfterFallOn(this.level, this);
// the default implementation of updateEntityAfterFallOn sets the y movement to
// 0
physics.velocity.y = 0.;
}
if on_ground {
// blockBelow.stepOn(this.level, blockPosBelow, blockStateBelow,
// this);
}
// sounds
// this.tryCheckInsideBlocks();
// float var25 = this.getBlockSpeedFactor();
// this.setDeltaMovement(this.getDeltaMovement().multiply((double)var25, 1.0D,
// (double)var25)); if (this.level.getBlockStatesIfLoaded(this.
// getBoundingBox().deflate(1.0E-6D)).noneMatch((var0) -> {
// return var0.is(BlockTags.FIRE) || var0.is(Blocks.LAVA);
// })) {
// if (this.remainingFireTicks <= 0) {
// this.setRemainingFireTicks(-this.getFireImmuneTicks());
// }
// if (this.wasOnFire && (this.isInPowderSnow ||
// this.isInWaterRainOrBubble())) { this.
// playEntityOnFireExtinguishedSound(); }
// }
// if (this.isOnFire() && (this.isInPowderSnow || this.isInWaterRainOrBubble()))
// { this.setRemainingFireTicks(-this.getFireImmuneTicks());
// }
Ok(())
}
fn collide_bounding_box(
movement: &Vec3,
entity_bounding_box: &AABB,
world: &Instance,
entity_collisions: Vec<VoxelShape>,
) -> Vec3 {
let mut collision_boxes: Vec<VoxelShape> = Vec::with_capacity(entity_collisions.len() + 1);
if !entity_collisions.is_empty() {
collision_boxes.extend(entity_collisions);
}
// TODO: world border
let block_collisions =
get_block_collisions(world, entity_bounding_box.expand_towards(movement));
collision_boxes.extend(block_collisions);
collide_with_shapes(movement, *entity_bounding_box, &collision_boxes)
}
fn collide_with_shapes(
movement: &Vec3,
mut entity_box: AABB,
collision_boxes: &Vec<VoxelShape>,
) -> Vec3 {
if collision_boxes.is_empty() {
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.,
});
}
}
// 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,
});
}
}
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 !more_z_movement && z_movement != 0. {
z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement);
}
Vec3 {
x: x_movement,
y: y_movement,
z: z_movement,
}
}