move actor::position_to_transform to camera module

This commit is contained in:
yuni 2024-05-01 21:50:59 +02:00
parent e361b1f493
commit 591b4a4f46
3 changed files with 35 additions and 35 deletions

View file

@ -16,13 +16,11 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use bevy_xpbd_3d::plugins::sync;
use bevy::scene::SceneInstance; use bevy::scene::SceneInstance;
use bevy::math::DVec3; use bevy::math::DVec3;
use crate::{actor, audio, camera, chat, commands, effects, hud, nature, var, world}; use crate::{actor, audio, camera, chat, commands, effects, hud, nature, var, world};
use std::collections::HashMap; use std::collections::HashMap;
const CENTER_WORLD_ON_PLAYER: bool = true;
pub const ENGINE_SPEED_FACTOR: f32 = 30.0; pub const ENGINE_SPEED_FACTOR: f32 = 30.0;
const MAX_TRANSMISSION_DISTANCE: f32 = 100.0; const MAX_TRANSMISSION_DISTANCE: f32 = 100.0;
const MAX_INTERACT_DISTANCE: f32 = 50.0; const MAX_INTERACT_DISTANCE: f32 = 50.0;
@ -54,18 +52,6 @@ impl Plugin for ActorPlugin {
app.add_event::<VehicleEnterExitEvent>(); app.add_event::<VehicleEnterExitEvent>();
app.add_event::<PlayerDiesEvent>(); app.add_event::<PlayerDiesEvent>();
app.insert_resource(Id2Pos(HashMap::new())); 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)); 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<actor::PlayerCamera>>,
mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without<Parent>>,
) {
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();
}
}
}

View file

@ -21,10 +21,12 @@ use bevy::pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap};
use bevy::transform::TransformSystem; use bevy::transform::TransformSystem;
use bevy::math::{DVec3, DQuat}; use bevy::math::{DVec3, DQuat};
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use bevy_xpbd_3d::plugins::sync;
use std::f32::consts::PI; use std::f32::consts::PI;
use std::f64::consts::PI as PI64; use std::f64::consts::PI as PI64;
use crate::{actor, audio, hud, var}; use crate::{actor, audio, hud, var};
const CENTER_WORLD_ON_PLAYER: bool = true;
pub const INITIAL_ZOOM_LEVEL: f64 = 10.0; pub const INITIAL_ZOOM_LEVEL: f64 = 10.0;
pub struct CameraPlugin; pub struct CameraPlugin;
@ -45,6 +47,18 @@ impl Plugin for CameraPlugin {
.after(PhysicsSet::Sync) .after(PhysicsSet::Sync)
.before(TransformSystem::TransformPropagate)); .before(TransformSystem::TransformPropagate));
app.insert_resource(MapCam::default()); 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(); .normalize_or_zero();
return calc_angular_diameter_known_target_vector(target, camera, &target_vector); 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<actor::PlayerCamera>>,
mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without<Parent>>,
) {
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();
}
}
}

View file

@ -46,10 +46,10 @@ impl Plugin for HudPlugin {
app.add_systems(PostUpdate, ( app.add_systems(PostUpdate, (
update_overlay_visibility, update_overlay_visibility,
update_ar_overlays update_ar_overlays
.after(actor::position_to_transform) .after(camera::position_to_transform)
.in_set(sync::SyncSet::PositionToTransform), .in_set(sync::SyncSet::PositionToTransform),
update_poi_overlays update_poi_overlays
.after(actor::position_to_transform) .after(camera::position_to_transform)
.in_set(sync::SyncSet::PositionToTransform), .in_set(sync::SyncSet::PositionToTransform),
update_target_selectagon update_target_selectagon
.after(PhysicsSet::Sync) .after(PhysicsSet::Sync)