diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index ddce9481..78b512fa 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -59,6 +59,8 @@ bevy = { version = "0.14", default-features = false, features = [ oorandom = "11" approx = "0.5.1" glam = { version = "0.27", features = ["approx"] } +rapier2d = { version = "0.21", features = ["serde-serialize"] } +serde_json = "1.0" [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs diff --git a/bevy_rapier2d/examples/joints2.rs b/bevy_rapier2d/examples/joints2.rs index e30352ca..07c10b0f 100644 --- a/bevy_rapier2d/examples/joints2.rs +++ b/bevy_rapier2d/examples/joints2.rs @@ -1,6 +1,7 @@ use bevy::prelude::*; use bevy_rapier2d::prelude::*; +#[allow(unused)] fn main() { App::new() .insert_resource(ClearColor(Color::srgb( diff --git a/bevy_rapier2d/examples/serialization.rs b/bevy_rapier2d/examples/serialization.rs new file mode 100644 index 00000000..0162c37d --- /dev/null +++ b/bevy_rapier2d/examples/serialization.rs @@ -0,0 +1,36 @@ +//! Example for RapierContext serialization, run with `--features serde-serialize`. + +use bevy::prelude::*; +use bevy_rapier2d::prelude::*; + +/// Note: This will end up in duplication for testbed, but that's more simple. +mod joints2; + +fn main() { + App::new() + .insert_resource(ClearColor(Color::srgb( + 0xF9 as f32 / 255.0, + 0xF9 as f32 / 255.0, + 0xFF as f32 / 255.0, + ))) + .add_plugins(( + DefaultPlugins, + RapierPhysicsPlugin::::pixels_per_meter(100.0), + RapierDebugRenderPlugin::default(), + )) + .add_systems(Startup, (joints2::setup_graphics, joints2::setup_physics)) + .add_systems(PostUpdate, print_physics) + .add_systems(Last, quit) + .run(); +} + +pub fn print_physics(context: Res) { + println!( + "{}", + serde_json::to_string_pretty(&(*context)).expect("Unable to serialize `RapierContext`") + ); +} + +fn quit(mut exit_event: EventWriter) { + exit_event.send(AppExit::Success); +} diff --git a/src/plugin/context.rs b/src/plugin/context.rs index eb7fe305..e73e45eb 100644 --- a/src/plugin/context.rs +++ b/src/plugin/context.rs @@ -32,6 +32,8 @@ pub struct RapierContext { /// The island manager, which detects what object is sleeping /// (not moving much) to reduce computations. pub islands: IslandManager, + // FIXME: This shoulnd not be skipped. + #[cfg_attr(feature = "serde-serialize", serde(skip))] /// The broad-phase, which detects potential contact pairs. pub broad_phase: DefaultBroadPhase, /// The narrow-phase, which computes contact points, tests intersections,