diff --git a/azalea-core/README.md b/azalea-core/README.md new file mode 100644 index 00000000..7d826076 --- /dev/null +++ b/azalea-core/README.md @@ -0,0 +1,3 @@ +# Azalea Core + +Miscellaneous things in Azalea. diff --git a/azalea-physics/src/aabb.rs b/azalea-core/src/aabb.rs similarity index 98% rename from azalea-physics/src/aabb.rs rename to azalea-core/src/aabb.rs index a6c0f5e4..b6de6c39 100644 --- a/azalea-physics/src/aabb.rs +++ b/azalea-core/src/aabb.rs @@ -1,9 +1,9 @@ -use crate::BlockHitResult; -use azalea_core::{Axis, BlockPos, Direction, PositionXYZ, Vec3}; +use crate::{Axis, BlockHitResult, BlockPos, Direction, PositionXYZ, Vec3}; pub const EPSILON: f64 = 1.0E-7; -#[derive(Copy, Clone, Debug, PartialEq)] +/// A rectangular prism with a starting and ending point. +#[derive(Copy, Clone, Debug, PartialEq, Default)] pub struct AABB { pub min_x: f64, pub min_y: f64, diff --git a/azalea-physics/src/block_hit_result.rs b/azalea-core/src/block_hit_result.rs similarity index 77% rename from azalea-physics/src/block_hit_result.rs rename to azalea-core/src/block_hit_result.rs index 8321f43a..80c9b8fc 100644 --- a/azalea-physics/src/block_hit_result.rs +++ b/azalea-core/src/block_hit_result.rs @@ -1,4 +1,4 @@ -use azalea_core::{BlockPos, Direction, Vec3}; +use crate::{BlockPos, Direction, Vec3}; pub struct BlockHitResult { pub location: Vec3, diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 7d0df41c..5aca7d52 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -32,6 +32,12 @@ pub use cursor3d::*; mod bitset; pub use bitset::*; +mod aabb; +pub use aabb::*; + +mod block_hit_result; +pub use block_hit_result::*; + // java moment // TODO: add tests and optimize/simplify this pub fn floor_mod(x: i32, y: u32) -> u32 { diff --git a/azalea-entity/src/dimensions.rs b/azalea-entity/src/dimensions.rs new file mode 100644 index 00000000..1d013d10 --- /dev/null +++ b/azalea-entity/src/dimensions.rs @@ -0,0 +1,23 @@ +use azalea_core::{Vec3, AABB}; + +#[derive(Debug, Default)] +pub struct EntityDimensions { + pub width: f32, + pub height: f32, +} + +impl EntityDimensions { + pub fn make_bounding_box(&self, pos: &Vec3) -> AABB { + let radius = (self.width / 2.0) as f64; + let height = self.height as f64; + AABB { + min_x: pos.x - radius, + min_y: pos.y, + min_z: pos.z - radius, + + max_x: pos.x + radius, + max_y: pos.y + height, + max_z: pos.z + radius, + } + } +} diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 1fab90bf..7f266a67 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -1,7 +1,9 @@ mod data; +mod dimensions; -use azalea_core::{PositionDelta, Vec3}; +use azalea_core::{PositionDelta, Vec3, AABB}; pub use data::*; +pub use dimensions::*; use uuid::Uuid; #[derive(Default, Debug)] @@ -17,10 +19,19 @@ pub struct Entity { pub x_rot: f32, pub y_rot: f32, + + /// The width and height of the entity. + pub dimensions: EntityDimensions, + /// The bounding box of the entity. This is more than just width and height, unlike dimensions. + pub bounding_box: AABB, } impl Entity { pub fn new(id: u32, uuid: Uuid, pos: Vec3) -> Self { + let dimensions = EntityDimensions { + width: 0.8, + height: 1.8, + }; Self { id, uuid, @@ -29,6 +40,9 @@ impl Entity { delta: PositionDelta::default(), x_rot: 0.0, y_rot: 0.0, + // TODO: have this be based on the entity type + bounding_box: dimensions.make_bounding_box(&pos), + dimensions: dimensions, } } diff --git a/azalea-physics/src/dimension_collisions.rs b/azalea-physics/src/dimension_collisions.rs index 2015ed8c..386ed2ff 100644 --- a/azalea-physics/src/dimension_collisions.rs +++ b/azalea-physics/src/dimension_collisions.rs @@ -1,6 +1,6 @@ -use crate::{aabb::EPSILON, ArrayVoxelShape, VoxelShape, AABB}; +use crate::{ArrayVoxelShape, VoxelShape, AABB}; use azalea_block::{Block, BlockState}; -use azalea_core::{ChunkPos, ChunkSectionPos, Cursor3d, CursorIterationType}; +use azalea_core::{ChunkPos, ChunkSectionPos, Cursor3d, CursorIterationType, EPSILON}; use azalea_entity::Entity; use azalea_world::{Chunk, Dimension}; use std::sync::{Arc, Mutex}; diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index 9a117faa..cf7a16cc 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -1,14 +1,10 @@ -mod aabb; -mod block_hit_result; mod dimension_collisions; mod discrete_voxel_shape; mod shape; -pub use aabb::*; -use azalea_core::{Axis, PositionDelta, PositionXYZ, Vec3}; +use azalea_core::{Axis, PositionDelta, PositionXYZ, Vec3, AABB}; use azalea_entity::Entity; use azalea_world::Dimension; -pub use block_hit_result::*; use dimension_collisions::CollisionGetter; pub use discrete_voxel_shape::*; pub use shape::*; @@ -23,6 +19,7 @@ pub enum MoverType { trait HasPhysics { fn move_entity(&mut self, mover_type: &MoverType, movement: &PositionDelta); + fn collide(&self, movement: &Vec3, dimension: &Dimension) -> Vec3; fn collide_bounding_box( entity: Option<&Self>, movement: &Vec3, @@ -60,19 +57,52 @@ impl HasPhysics for Entity { // TODO } - // fn collide(movement: &Vec3, dimension: &Dimension) -> Vec3 { - // if movement.length_sqr() == 0.0 { - // *movement - // } else { - // // Self::collide_bounding_box( - // // Some(self), - // // movement, - // // entityBoundingBox, - // // this.level, - // // entityCollisions, - // // ) + // 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(&self, movement: &Vec3, dimension: &Dimension) -> Vec3 { + let entity_bounding_box = self.bounding_box; + // TODO: get_entity_collisions + // let entity_collisions = dimension.get_entity_collisions(self, entity_bounding_box.expand_towards(movement)); + let entity_collisions = Vec::new(); + let collided_movement = if movement.length_sqr() == 0.0 { + *movement + } else { + Self::collide_bounding_box( + Some(self), + movement, + &entity_bounding_box, + dimension, + entity_collisions, + ) + }; + + // TODO: stepping (for stairs and stuff) + + collided_movement + } fn collide_bounding_box( entity: Option<&Self>, diff --git a/azalea-physics/src/shape.rs b/azalea-physics/src/shape.rs index 893e376a..5469d48a 100644 --- a/azalea-physics/src/shape.rs +++ b/azalea-physics/src/shape.rs @@ -1,6 +1,5 @@ -use azalea_core::{binary_search, Axis, AxisCycle}; - -use crate::{BitSetDiscreteVoxelShape, DiscreteVoxelShape, AABB, EPSILON}; +use crate::{BitSetDiscreteVoxelShape, DiscreteVoxelShape, AABB}; +use azalea_core::{binary_search, Axis, AxisCycle, EPSILON}; use std::{cmp, ops::Add}; pub struct Shapes {}