diff options
| author | mat <git@matdoes.dev> | 2025-08-04 16:17:41 -0900 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-08-04 19:33:00 -0545 |
| commit | 827d943c3f27c65724ff83689b40c87d1cd1838c (patch) | |
| tree | 52d0145a8d482e912ceccb076accddce4da75c5e | |
| parent | 013cd9f17674a4939d885ef91b7058b5a59d9ae1 (diff) | |
| download | azalea-drasl-827d943c3f27c65724ff83689b40c87d1cd1838c.tar.xz | |
fix wrong float rounding in get_friction_influenced_speed
| -rw-r--r-- | azalea-client/src/plugins/movement.rs | 2 | ||||
| -rw-r--r-- | azalea-client/tests/correct_movement.rs | 103 | ||||
| -rw-r--r-- | azalea-core/src/math.rs | 2 | ||||
| -rw-r--r-- | azalea-entity/src/lib.rs | 5 | ||||
| -rw-r--r-- | azalea-physics/src/lib.rs | 2 |
5 files changed, 109 insertions, 5 deletions
diff --git a/azalea-client/src/plugins/movement.rs b/azalea-client/src/plugins/movement.rs index e4778be1..aeb946fa 100644 --- a/azalea-client/src/plugins/movement.rs +++ b/azalea-client/src/plugins/movement.rs @@ -423,7 +423,7 @@ fn distance_to_unit_square(v: Vec2) -> f32 { let x = v.x.abs(); let y = v.y.abs(); let ratio = if y > x { x / y } else { y / x }; - (1.0 + ratio * ratio).sqrt() + (1. + ratio * ratio).sqrt() } impl Client { diff --git a/azalea-client/tests/correct_movement.rs b/azalea-client/tests/correct_movement.rs new file mode 100644 index 00000000..c20fdb85 --- /dev/null +++ b/azalea-client/tests/correct_movement.rs @@ -0,0 +1,103 @@ +use azalea_client::{StartWalkEvent, WalkDirection, test_utils::prelude::*}; +use azalea_core::{ + position::{BlockPos, ChunkPos, Vec3}, + resource_location::ResourceLocation, +}; +use azalea_entity::LookDirection; +use azalea_protocol::{ + common::movements::{MoveFlags, PositionMoveRotation, RelativeMovements}, + packets::{ + ConnectionProtocol, + config::{ClientboundFinishConfiguration, ClientboundRegistryData}, + game::{ + ClientboundBlockUpdate, ClientboundPlayerPosition, ClientboundSetChunkCacheCenter, + ServerboundGamePacket, ServerboundMovePlayerPos, + }, + }, +}; +use azalea_registry::{Block, DataRegistry, DimensionType}; +use simdnbt::owned::{NbtCompound, NbtTag}; + +#[test] +fn test_correct_movement() { + init_tracing(); + + let mut simulation = Simulation::new(ConnectionProtocol::Configuration); + let sent_packets = SentPackets::new(&mut simulation); + + simulation.receive_packet(ClientboundRegistryData { + registry_id: ResourceLocation::new("minecraft:dimension_type"), + entries: vec![( + ResourceLocation::new("minecraft:overworld"), + Some(NbtCompound::from_values(vec![ + ("height".into(), NbtTag::Int(384)), + ("min_y".into(), NbtTag::Int(-64)), + ])), + )] + .into_iter() + .collect(), + }); + simulation.tick(); + simulation.receive_packet(ClientboundFinishConfiguration); + simulation.tick(); + + simulation.receive_packet(make_basic_login_packet( + DimensionType::new_raw(0), // overworld + ResourceLocation::new("minecraft:overworld"), + )); + simulation.tick(); + + sent_packets.expect_tick_end(); + sent_packets.expect_empty(); + + // receive a chunk so the player is "loaded" now + simulation.receive_packet(ClientboundSetChunkCacheCenter { x: 1, z: 23 }); + simulation.receive_packet(make_basic_empty_chunk( + ChunkPos::new(1, 23), + (384 + 64) / 16, + )); + simulation.receive_packet(ClientboundBlockUpdate { + pos: BlockPos::new(31, 63, 370), + block_state: Block::Stone.into(), + }); + simulation.receive_packet(ClientboundPlayerPosition { + id: 1, + change: PositionMoveRotation { + pos: Vec3::new(31.5, 64., 370.5), + delta: Vec3::ZERO, + look_direction: LookDirection::default(), + }, + relative: RelativeMovements::all_absolute(), + }); + simulation.tick(); + simulation.tick(); + + // walk for a tick + simulation.send_event(StartWalkEvent { + entity: simulation.entity, + direction: WalkDirection::Forward, + }); + sent_packets.clear(); + simulation.tick(); + sent_packets.expect("PlayerInput", |p| { + matches!(p, ServerboundGamePacket::PlayerInput(_)) + }); + sent_packets.expect("MovePlayerPos { pos.z: 370.59800000336764, ... }", |p| { + matches!( + p, + ServerboundGamePacket::MovePlayerPos(ServerboundMovePlayerPos { + pos: Vec3 { + x: 31.5, + y: 64.0, + z: 370.59800000336764 + }, + flags: MoveFlags { + on_ground: true, + horizontal_collision: false + } + }) + ) + }); + sent_packets.expect_tick_end(); + sent_packets.expect_empty(); +} diff --git a/azalea-core/src/math.rs b/azalea-core/src/math.rs index a115927d..d5fe5af9 100644 --- a/azalea-core/src/math.rs +++ b/azalea-core/src/math.rs @@ -4,7 +4,7 @@ use std::{ sync::LazyLock, }; -pub const EPSILON: f64 = 1.0E-7; +pub const EPSILON: f64 = 1.0e-7; pub static SIN: LazyLock<[f32; 65536]> = LazyLock::new(|| std::array::from_fn(|i| f64::sin((i as f64) * PI * 2. / 65536.) as f32)); diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 18e9e1a1..984cce27 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -12,6 +12,7 @@ mod plugin; pub mod vec_delta_codec; use std::{ + f64::consts::PI, fmt::{self, Debug}, hash::{Hash, Hasher}, }; @@ -59,8 +60,8 @@ pub fn input_vector(direction: LookDirection, speed: f32, acceleration: Vec3) -> acceleration } .scale(speed as f64); - let y_rot = math::sin(direction.y_rot * 0.017453292f32); - let x_rot = math::cos(direction.y_rot * 0.017453292f32); + let y_rot = math::sin(direction.y_rot * (PI / 180.) as f32); + let x_rot = math::cos(direction.y_rot * (PI / 180.) as f32); Vec3 { x: acceleration.x * (x_rot as f64) - acceleration.z * (y_rot as f64), y: acceleration.y, diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index 9481cef7..779ee067 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -493,7 +493,7 @@ fn get_friction_influenced_speed( // TODO: have speed & flying_speed fields in entity if physics.on_ground() { let speed: f32 = attributes.speed.calculate() as f32; - speed * (0.216f32 / (friction * friction * friction)) + speed * (0.21600002f32 / (friction * friction * friction)) } else { // entity.flying_speed if is_sprinting { 0.025999999f32 } else { 0.02 } |
