From 2b14bd95ccf7049cc146dcd6238e7928d1352c32 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Mon, 8 Aug 2022 11:09:40 +0200 Subject: synced transforms, it desyncs and it's too slow --- src/main.rs | 99 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 37 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e4cb968..2e427d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use std::fs::File; use std::io::BufReader; +use std::{arch::global_asm, fs::File}; use bevy::{prelude::*, render::camera::ScalingMode, window::WindowResized}; @@ -120,6 +120,15 @@ fn main() -> Result<(), Box> { ) .with_stage_after( ROLLBACK_PHYSICS_2, + ROLLBACK_PHYSICS_3, + SystemStage::parallel().with_system_set( + RapierPhysicsPlugin::::get_systems( + PhysicsStages::DetectDespawn, + ), + ), + ) + .with_stage_after( + ROLLBACK_PHYSICS_3, ROLLBACK_TEARDOWN, SystemStage::single(physics_ser), ), @@ -154,32 +163,54 @@ fn main() -> Result<(), Box> { //.add_plugin(LogDiagnosticsPlugin::default()) //.add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_plugin(RapierDebugRenderPlugin::default()) - .add_system_to_stage(CoreStage::PostUpdate, camera_follow) .add_stage_after( - CoreStage::Update, - "deser", - SystemStage::single(physics_deser), - ) - .add_stage_after( - "deser", + CoreStage::PostUpdate, "sync physics", - SystemStage::parallel().with_system_set(RapierPhysicsPlugin::::get_systems( - PhysicsStages::Writeback, - )), + SystemStage::single(writeback_rigid_bodies), ) + .add_stage_after("sync physics", "cam", SystemStage::single(camera_follow)) .add_system(window_resized_event) - .add_stage_before( - CoreStage::Last, - ROLLBACK_PHYSICS_3, - SystemStage::parallel().with_system_set(RapierPhysicsPlugin::::get_systems( - PhysicsStages::DetectDespawn, - )), - ) .run(); Ok(()) } +pub fn writeback_rigid_bodies( + mut context: ResMut, + config: Res, + mut transforms: Query<(&mut Transform, &GlobalTransform)>, +) { + let context = &mut *context; + + if config.physics_pipeline_active { + /* + for (handle, rb) in context.bodies.iter() { + let interpolated_pos = bevy_rapier2d::utils::iso_to_transform(rb.position(), 100.0); + if let Some(entity) = context.rigid_body_entity(handle) { + if let Some((mut t, mut gt)) = transforms.get_mut(entity).ok() { + let (scale, _, _) = global_transform.to_scale_rotation_translation(); + let transform = Transform { + translation: interpolated_pos.translation, + rotation: interpolated_pos.rotation, + scale: scale, + }; + *global_transform = GlobalTransform::from(transform); + } + } + } + */ + for (handle, col) in context.colliders.iter() { + let interpolated_pos = bevy_rapier2d::utils::iso_to_transform(col.position(), 100.0); + if let Some(entity) = context.collider_entity(handle) { + if let Some((mut t, _gt)) = transforms.get_mut(entity).ok() { + t.translation = interpolated_pos.translation; + t.rotation = interpolated_pos.rotation; + } + } + } + } +} + #[derive(Default, Reflect, Component)] struct SerPhysics { pub ser: Vec, @@ -425,17 +456,23 @@ fn setup_map(mut commands: Commands) { let upleft = Vec3::new(wall[0] as f32, wall[1] as f32, 0.0); let downright = Vec3::new(wall[2] as f32, wall[3] as f32, 0.0); let center = (upleft + downright - origin) / 2.0; - let size_big = Vec3::new( - (wall[2] - wall[0] + 3) as f32, - (wall[3] - wall[1] + 3) as f32, - 1.0, - ); + let size = Vec3::new((wall[2] - wall[0]) as f32, (wall[3] - wall[1]) as f32, 1.0); + let color = match wall[4] { + 1 => Color::rgba(0.7, 0.2, 0.0, 1.0), + 2 => Color::rgba(0.15, 0.4, 0.03, 1.0), + 3 => Color::rgba(0.4, 0.4, 0.4, 1.0), + _ => Color::rgba(1.0, 0.4, 0.03, 1.0), + }; let movecenter = center - Vec3::new(0.0, 0.0, if wall[4] == 2 { 1.0 } else { 0.0 }); commands.spawn_bundle(SpriteBundle { transform: Transform { translation: movecenter, - scale: size_big, + scale: Vec3::new( + (wall[2] - wall[0] + 3) as f32, + (wall[3] - wall[1] + 3) as f32, + 1.0, + ), ..default() }, sprite: Sprite { @@ -444,19 +481,7 @@ fn setup_map(mut commands: Commands) { }, ..default() }); - } - for wall in &map.walls { - let upleft = Vec3::new(wall[0] as f32, wall[1] as f32, 0.0); - let downright = Vec3::new(wall[2] as f32, wall[3] as f32, 0.0); - let center = (upleft + downright - origin) / 2.0; - let size = Vec3::new((wall[2] - wall[0]) as f32, (wall[3] - wall[1]) as f32, 1.0); - let color = match wall[4] { - 1 => Color::rgba(0.7, 0.2, 0.0, 1.0), - 2 => Color::rgba(0.15, 0.4, 0.03, 1.0), - 3 => Color::rgba(0.4, 0.4, 0.4, 1.0), - _ => Color::rgba(1.0, 0.4, 0.03, 1.0), - }; - let movecenter = center - Vec3::new(0.0, 0.0, if wall[4] == 2 { 1.0 } else { 0.0 }); + let entity = commands .spawn_bundle(SpriteBundle { transform: Transform { -- cgit v1.2.3-54-g00ecf