From f04b5afccdec2cbc3bc5f3e350aa1f9a118101bc Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Thu, 4 Jan 2024 20:56:08 +0100 Subject: [PATCH] Add pitch and yaw --- minecraft-server/src/entities/monsters/zombies.rs | 4 ++++ minecraft-server/src/world/collisions.rs | 9 +++++++++ minecraft-server/src/world/ecs.rs | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/minecraft-server/src/entities/monsters/zombies.rs b/minecraft-server/src/entities/monsters/zombies.rs index 95b86f10..aa2f2e68 100644 --- a/minecraft-server/src/entities/monsters/zombies.rs +++ b/minecraft-server/src/entities/monsters/zombies.rs @@ -147,8 +147,12 @@ impl ZombieTask { // Get the movement to apply if let Some((self_position, target_position)) = positions { let movement = self.get_movement(&h, &self_position, &target_position).await; + let (yaw, pitch) = movement.yaw_pitch(); h.mutate(|e| { e.get_entity_mut().position += movement; + e.get_entity_mut().yaw = yaw; + e.get_entity_mut().pitch = pitch; + e.get_living_entity_mut().head_yaw = yaw; // TODO: Make pitch and yaw work on zombies }).await; } diff --git a/minecraft-server/src/world/collisions.rs b/minecraft-server/src/world/collisions.rs index 33ed52e5..177b236a 100644 --- a/minecraft-server/src/world/collisions.rs +++ b/minecraft-server/src/world/collisions.rs @@ -275,6 +275,15 @@ pub struct Translation { pub z: f64, } +impl Translation { + pub fn yaw_pitch(&self) -> (f32, f32) { + let r = (self.x * self.x + self.y * self.y + self.z * self.z).sqrt(); + let pitch = -(self.y / r).asin() / std::f64::consts::PI * 180.0; + let yaw = -(self.x / self.z).atan() / std::f64::consts::PI * 180.0; + (yaw as f32, pitch as f32) + } +} + pub struct TranslationFragmentIterator<'a> { translation: &'a Translation, position: &'a CollisionShape, diff --git a/minecraft-server/src/world/ecs.rs b/minecraft-server/src/world/ecs.rs index 489defee..9828fdeb 100644 --- a/minecraft-server/src/world/ecs.rs +++ b/minecraft-server/src/world/ecs.rs @@ -59,12 +59,13 @@ impl Entities { let prev_position = entity.as_entity().position.clone(); let prev_velocity = entity.as_entity().velocity.clone(); let prev_pitch = entity.as_entity().pitch; + let prev_yaw = entity.as_entity().yaw; let r = mutator(entity); let mut changes = EntityChanges::other(); if prev_velocity != entity.as_entity().velocity { changes += EntityChanges::velocity(); } - if prev_pitch != entity.as_entity().pitch { // TODO: detect yaw changes + if prev_pitch != entity.as_entity().pitch || prev_yaw != entity.as_entity().yaw { changes += EntityChanges::pitch(); } if prev_position != entity.as_entity().position {