Skip to content

Commit

Permalink
fix!: Update camera target position after rapier physics writeback
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Jul 19, 2024
1 parent 7dbef5b commit 6853418
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
28 changes: 12 additions & 16 deletions src/world/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::render::camera::ScalingMode;
use bevy::render::view::screenshot::ScreenshotManager;
use bevy::window::{PrimaryWindow, WindowMode};
use bevy_kira_audio::prelude::AudioReceiver;
use bevy_rapier2d::dynamics::Velocity;
use bevy_rapier2d::plugin::PhysicsSet;

use super::camera_shake::{update_camera, CameraShake};
use crate::player::input::PlayerInput;
Expand All @@ -15,9 +15,6 @@ use crate::GameState;
// How much `1.0` in bevy coordinates translates to the pixels of a sprite.
// Only relevant for the ysorting.
pub const TRANSLATION_TO_PIXEL: f32 = 0.0001;
// This is not changing the actual timestep,
// it's just a way to reduce magic numbers in code.
const RAPIER_TIMESTEP: f32 = 60.0;

#[derive(Component)]
pub struct MainCamera;
Expand All @@ -37,19 +34,12 @@ fn spawn_camera(mut commands: Commands) {
commands.spawn((MainCamera, AudioReceiver, camera));
}

fn update_camera_target(
mut shake: ResMut<CameraShake>,
q_player: Query<(&Transform, &Velocity), With<Player>>,
) {
let (player_pos, player_vel) = match q_player.get_single() {
Ok(p) => (p.0.translation, p.1),
fn update_camera_target(mut shake: ResMut<CameraShake>, q_player: Query<&Transform, With<Player>>) {
let player_transform = match q_player.get_single() {
Ok(r) => r,
Err(_) => return,
};

shake.update_target(Vec2::new(
player_pos.x + player_vel.linvel.x / RAPIER_TIMESTEP,
player_pos.y + player_vel.linvel.y / RAPIER_TIMESTEP,
));
shake.update_target(player_transform.translation.truncate());
}

fn zoom_camera(
Expand Down Expand Up @@ -137,6 +127,12 @@ impl Plugin for CameraPlugin {
),
)
.add_systems(OnEnter(GameState::Gaming), spawn_camera)
.add_systems(PostUpdate, update_camera_target.before(update_camera));
.add_systems(
PostUpdate,
update_camera_target
.after(PhysicsSet::Writeback)
.before(TransformSystem::TransformPropagate)
.before(update_camera),
);
}
}
5 changes: 4 additions & 1 deletion src/world/camera_shake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl Plugin for CameraShakePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (decay_shake_trauma,))
.init_resource::<CameraShake>()
.add_systems(PostUpdate, update_camera);
.add_systems(
PostUpdate,
update_camera.before(TransformSystem::TransformPropagate),
);
}
}

0 comments on commit 6853418

Please sign in to comment.