Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Add pitch and yaw
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Jan 4, 2024
1 parent ec83b94 commit f04b5af
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions minecraft-server/src/entities/monsters/zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions minecraft-server/src/world/collisions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion minecraft-server/src/world/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f04b5af

Please sign in to comment.