From 591b4a4f46608788d7e990786ed5fa81588ad3da Mon Sep 17 00:00:00 2001 From: hut Date: Wed, 1 May 2024 21:50:59 +0200 Subject: [PATCH] move actor::position_to_transform to camera module --- src/actor.rs | 33 --------------------------------- src/camera.rs | 33 +++++++++++++++++++++++++++++++++ src/hud.rs | 4 ++-- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 95bc73d..9917d6b 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -16,13 +16,11 @@ use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; -use bevy_xpbd_3d::plugins::sync; use bevy::scene::SceneInstance; use bevy::math::DVec3; use crate::{actor, audio, camera, chat, commands, effects, hud, nature, var, world}; use std::collections::HashMap; -const CENTER_WORLD_ON_PLAYER: bool = true; pub const ENGINE_SPEED_FACTOR: f32 = 30.0; const MAX_TRANSMISSION_DISTANCE: f32 = 100.0; const MAX_INTERACT_DISTANCE: f32 = 50.0; @@ -54,18 +52,6 @@ impl Plugin for ActorPlugin { app.add_event::(); app.add_event::(); app.insert_resource(Id2Pos(HashMap::new())); - - if CENTER_WORLD_ON_PLAYER { - // Disable bevy_xpbd's position->transform sync function - app.insert_resource(sync::SyncConfig { - position_to_transform: true, - transform_to_position: false, - }); - // Add own position->transform sync function - app.add_systems(PostUpdate, position_to_transform - .after(sync::position_to_transform) - .in_set(sync::SyncSet::PositionToTransform)); - } } } @@ -658,22 +644,3 @@ fn handle_cheats( ew_playerdies.send(actor::PlayerDiesEvent(actor::DamageType::Trauma)); } } - -// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts -// the rendering position to center entities at the player camera. -// This avoids rendering glitches when very far away from the origin. -pub fn position_to_transform( - q_player: Query<&Position, With>, - mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without>, -) { - if let Ok(player_pos) = q_player.get_single() { - for (mut transform, pos, rot) in &mut q_trans { - transform.translation = Vec3::new( - (pos.x - player_pos.x) as f32, - (pos.y - player_pos.y) as f32, - (pos.z - player_pos.z) as f32, - ); - transform.rotation = rot.as_quat(); - } - } -} diff --git a/src/camera.rs b/src/camera.rs index d03926f..a62cfa5 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -21,10 +21,12 @@ use bevy::pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap}; use bevy::transform::TransformSystem; use bevy::math::{DVec3, DQuat}; use bevy_xpbd_3d::prelude::*; +use bevy_xpbd_3d::plugins::sync; use std::f32::consts::PI; use std::f64::consts::PI as PI64; use crate::{actor, audio, hud, var}; +const CENTER_WORLD_ON_PLAYER: bool = true; pub const INITIAL_ZOOM_LEVEL: f64 = 10.0; pub struct CameraPlugin; @@ -45,6 +47,18 @@ impl Plugin for CameraPlugin { .after(PhysicsSet::Sync) .before(TransformSystem::TransformPropagate)); app.insert_resource(MapCam::default()); + + if CENTER_WORLD_ON_PLAYER { + // Disable bevy_xpbd's position->transform sync function + app.insert_resource(sync::SyncConfig { + position_to_transform: true, + transform_to_position: false, + }); + // Add own position->transform sync function + app.add_systems(PostUpdate, position_to_transform + .after(sync::position_to_transform) + .in_set(sync::SyncSet::PositionToTransform)); + } } } @@ -672,3 +686,22 @@ pub fn calc_angular_diameter( .normalize_or_zero(); return calc_angular_diameter_known_target_vector(target, camera, &target_vector); } + +// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts +// the rendering position to center entities at the player camera. +// This avoids rendering glitches when very far away from the origin. +pub fn position_to_transform( + q_player: Query<&Position, With>, + mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without>, +) { + if let Ok(player_pos) = q_player.get_single() { + for (mut transform, pos, rot) in &mut q_trans { + transform.translation = Vec3::new( + (pos.x - player_pos.x) as f32, + (pos.y - player_pos.y) as f32, + (pos.z - player_pos.z) as f32, + ); + transform.rotation = rot.as_quat(); + } + } +} diff --git a/src/hud.rs b/src/hud.rs index a778022..24eb78c 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -46,10 +46,10 @@ impl Plugin for HudPlugin { app.add_systems(PostUpdate, ( update_overlay_visibility, update_ar_overlays - .after(actor::position_to_transform) + .after(camera::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_poi_overlays - .after(actor::position_to_transform) + .after(camera::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_target_selectagon .after(PhysicsSet::Sync)