|
| 1 | +//! This is a minimal example to show how synchronized fixed update works. |
| 2 | +
|
| 3 | +use bevy::dev_tools::fps_overlay::FpsOverlayPlugin; |
| 4 | +use bevy::prelude::*; |
| 5 | +use bevy_fixed_update_task::{ |
| 6 | + BackgroundFixedUpdatePlugin, SpawnTaskSet, TaskResults, TaskToRenderTime, TaskWorker, |
| 7 | + TaskWorkerTrait, Timestep, |
| 8 | +}; |
| 9 | +use bevy_rapier2d::prelude::*; |
| 10 | + |
| 11 | +use std::{mem, time::Duration}; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + let mut app = App::new(); |
| 15 | + |
| 16 | + app.add_plugins(( |
| 17 | + DefaultPlugins, |
| 18 | + FpsOverlayPlugin::default(), |
| 19 | + BackgroundFixedUpdatePlugin::<TaskWorkerTraitImpl>::default(), |
| 20 | + RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0) |
| 21 | + .with_custom_initialization(RapierContextInitialization::NoAutomaticRapierContext) |
| 22 | + .in_schedule(bevy_fixed_update_task::FixedMain) |
| 23 | + .set_physics_sets_to_initialize([].into()), |
| 24 | + RapierDebugRenderPlugin::default(), |
| 25 | + )); |
| 26 | + app.add_systems(Startup, (setup_worker, (setup_info, setup_physics)).chain()); |
| 27 | + app.add_systems(Update, update_info); |
| 28 | + // TODO: SyncBackend before [`SpawnTask`]. |
| 29 | + app.add_systems( |
| 30 | + bevy_fixed_update_task::SpawnTask, |
| 31 | + RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend) |
| 32 | + .in_set(SpawnTaskSet::PreSpawn), |
| 33 | + ); |
| 34 | + // TODO: StepSimulation removed, that's our spawn task + handle task. |
| 35 | + // TODO: Writeback before [`PostWriteBack`]. |
| 36 | + app.add_systems( |
| 37 | + bevy_fixed_update_task::PostWriteBack, |
| 38 | + RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::Writeback), |
| 39 | + ); |
| 40 | + |
| 41 | + // Run the app. |
| 42 | + app.run(); |
| 43 | +} |
| 44 | + |
| 45 | +fn setup_worker(mut commands: Commands) { |
| 46 | + commands.spawn(( |
| 47 | + Timestep { |
| 48 | + timestep: Duration::from_secs_f32(1.0 / 25.0), |
| 49 | + }, |
| 50 | + TaskResults::<TaskWorkerTraitImpl>::default(), |
| 51 | + TaskWorker { |
| 52 | + worker: TaskWorkerTraitImpl {}, |
| 53 | + }, |
| 54 | + RapierContextSimulation::default(), |
| 55 | + DefaultRapierContext, |
| 56 | + RapierConfiguration { |
| 57 | + gravity: Vect::Y * -9.81 * 100.0, |
| 58 | + physics_pipeline_active: true, |
| 59 | + query_pipeline_active: true, |
| 60 | + scaled_shape_subdivision: 10, |
| 61 | + force_update_from_transform_changes: false, |
| 62 | + }, |
| 63 | + )); |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Component)] |
| 67 | +pub struct SimToRenderText; |
| 68 | + |
| 69 | +pub fn setup_info(mut commands: Commands) { |
| 70 | + // Simulation to render time |
| 71 | + commands |
| 72 | + .spawn(( |
| 73 | + Text::new("simulation to render time: "), |
| 74 | + Node { |
| 75 | + position_type: PositionType::Absolute, |
| 76 | + top: Val::Px(50.0), |
| 77 | + left: Val::Px(15.0), |
| 78 | + ..default() |
| 79 | + }, |
| 80 | + )) |
| 81 | + .with_child((TextSpan::default(), SimToRenderText)); |
| 82 | +} |
| 83 | +pub fn update_info( |
| 84 | + task_to_render_time: Query<&TaskToRenderTime>, |
| 85 | + mut query: Query<&mut TextSpan, With<SimToRenderText>>, |
| 86 | +) { |
| 87 | + for mut span in query.iter_mut() { |
| 88 | + **span = format!("{:.2}s", task_to_render_time.single().diff); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +pub fn setup_physics(mut commands: Commands) { |
| 93 | + let num = 80; |
| 94 | + let rad = 10.0; |
| 95 | + |
| 96 | + let shift = rad * 2.0 + rad; |
| 97 | + let centerx = shift * (num / 2) as f32; |
| 98 | + let centery = shift / 2.0; |
| 99 | + /* |
| 100 | + * Camera |
| 101 | + */ |
| 102 | + commands.spawn(( |
| 103 | + Camera2d::default(), |
| 104 | + OrthographicProjection { |
| 105 | + scale: 6f32, |
| 106 | + ..OrthographicProjection::default_2d() |
| 107 | + }, |
| 108 | + Transform::from_xyz(-2500.0, 2080.0, 0.0), |
| 109 | + )); |
| 110 | + /* |
| 111 | + * Ground |
| 112 | + */ |
| 113 | + let ground_size = 13500.0; |
| 114 | + let ground_height = 100.0; |
| 115 | + |
| 116 | + commands.spawn(( |
| 117 | + Transform::from_xyz(-centerx, 0.0 * -ground_height - 100.0, 0.0), |
| 118 | + Collider::cuboid(ground_size, ground_height), |
| 119 | + )); |
| 120 | + |
| 121 | + /* |
| 122 | + * Create the cubes |
| 123 | + */ |
| 124 | + let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5; |
| 125 | + |
| 126 | + for j in 0usize..100 { |
| 127 | + for i in 0..num { |
| 128 | + let x = i as f32 * shift - centerx + offset; |
| 129 | + let y = j as f32 * shift + centery + 30.0; |
| 130 | + |
| 131 | + commands.spawn(( |
| 132 | + // Mesh2d(mesh.clone()), |
| 133 | + //MeshMaterial2d(material.clone()), |
| 134 | + Transform::from_xyz(x, y, 0.0), |
| 135 | + RigidBody::Dynamic, |
| 136 | + Collider::cuboid(rad, rad), |
| 137 | + )); |
| 138 | + } |
| 139 | + |
| 140 | + offset -= 0.05 * rad * ((num as f32 * 1.0) - 1.0); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +#[derive(Debug, Clone, Default)] |
| 145 | +pub struct TaskWorkerTraitImpl; |
| 146 | + |
| 147 | +impl TaskWorkerTrait for TaskWorkerTraitImpl { |
| 148 | + type TaskExtractedData = TaskExtractedData; |
| 149 | + type TaskResultPure = TaskResult; |
| 150 | + |
| 151 | + fn work( |
| 152 | + &self, |
| 153 | + _worker: Entity, |
| 154 | + mut input: TaskExtractedData, |
| 155 | + timestep: Duration, |
| 156 | + substep_count: u32, |
| 157 | + ) -> Self::TaskResultPure { |
| 158 | + input.rapier_context.step_simulation( |
| 159 | + &mut input.colliders, |
| 160 | + &mut input.joints, |
| 161 | + &mut input.bodies, |
| 162 | + input.configuration.gravity, |
| 163 | + TimestepMode::Fixed { |
| 164 | + dt: timestep.as_secs_f32(), |
| 165 | + substeps: substep_count as usize, |
| 166 | + }, |
| 167 | + None, // FIXME: change `None` to `true` (see bevy's integration from Thierry) |
| 168 | + &(), // FIXME: &hooks_adapter, |
| 169 | + &input.time, |
| 170 | + &mut input.sim_to_render_time, |
| 171 | + None, |
| 172 | + ); |
| 173 | + TaskResult { |
| 174 | + rapier_context: input.rapier_context, |
| 175 | + colliders: input.colliders, |
| 176 | + bodies: input.bodies, |
| 177 | + joints: input.joints, |
| 178 | + query_pipeline: input.query_pipeline, |
| 179 | + sim_to_render_time: input.sim_to_render_time, |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + fn extract(&self, worker_entity: Entity, world: &mut World) -> TaskExtractedData { |
| 184 | + // Time is not actually used as we're only using `TimestepMode::Fixed`, |
| 185 | + // but rapier API requires it. |
| 186 | + let time = world.get_resource::<Time>().unwrap(); |
| 187 | + |
| 188 | + let time = time.clone(); |
| 189 | + let mut rapier_context_query = world.query::<( |
| 190 | + &mut RapierContextSimulation, |
| 191 | + &RapierContextColliders, |
| 192 | + &RapierRigidBodySet, |
| 193 | + &RapierContextJoints, |
| 194 | + &RapierQueryPipeline, |
| 195 | + &RapierConfiguration, |
| 196 | + &mut SimulationToRenderTime, |
| 197 | + )>(); |
| 198 | + let ( |
| 199 | + mut context_ecs, |
| 200 | + colliders, |
| 201 | + bodies, |
| 202 | + joints, |
| 203 | + query_pipeline, |
| 204 | + config, |
| 205 | + sim_to_render_time, |
| 206 | + ) = rapier_context_query.get_mut(world, worker_entity).unwrap(); |
| 207 | + |
| 208 | + // FIXME: Clone this properly? |
| 209 | + let mut rapier_context = RapierContextSimulation::default(); |
| 210 | + mem::swap(&mut rapier_context, &mut *context_ecs); |
| 211 | + // TODO: use a double buffering system to avoid this more expensive (to verify) cloning. |
| 212 | + let colliders = colliders.clone(); |
| 213 | + let bodies = bodies.clone(); |
| 214 | + let joints = joints.clone(); |
| 215 | + let query_pipeline = query_pipeline.clone(); |
| 216 | + // let mut context: RapierContext = |
| 217 | + // unsafe { mem::transmute_copy::<RapierContext, RapierContext>(&*context_ecs) }; |
| 218 | + let configuration = config.clone(); |
| 219 | + |
| 220 | + let sim_to_render_time = sim_to_render_time.clone(); |
| 221 | + |
| 222 | + TaskExtractedData { |
| 223 | + time, |
| 224 | + rapier_context, |
| 225 | + colliders, |
| 226 | + bodies, |
| 227 | + joints, |
| 228 | + query_pipeline, |
| 229 | + configuration, |
| 230 | + sim_to_render_time, |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + fn write_back( |
| 235 | + &self, |
| 236 | + worker_entity: Entity, |
| 237 | + result: bevy_fixed_update_task::TaskResult<Self>, |
| 238 | + world: &mut World, |
| 239 | + ) { |
| 240 | + let mut rapier_context_query = world.query::<( |
| 241 | + &mut RapierContextSimulation, |
| 242 | + &mut RapierContextColliders, |
| 243 | + &mut RapierRigidBodySet, |
| 244 | + &mut RapierContextJoints, |
| 245 | + &mut RapierQueryPipeline, |
| 246 | + &mut SimulationToRenderTime, |
| 247 | + )>(); |
| 248 | + let ( |
| 249 | + mut context_ecs, |
| 250 | + mut colliders, |
| 251 | + mut bodies, |
| 252 | + mut joints, |
| 253 | + mut query_pipeline, |
| 254 | + mut sim_to_render_time, |
| 255 | + ) = rapier_context_query.get_mut(world, worker_entity).unwrap(); |
| 256 | + |
| 257 | + *context_ecs = result.result_raw.result.rapier_context; |
| 258 | + *colliders = result.result_raw.result.colliders; |
| 259 | + *bodies = result.result_raw.result.bodies; |
| 260 | + *joints = result.result_raw.result.joints; |
| 261 | + *query_pipeline = result.result_raw.result.query_pipeline; |
| 262 | + *sim_to_render_time = result.result_raw.result.sim_to_render_time; |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +#[derive(Component)] |
| 267 | +pub struct TaskExtractedData { |
| 268 | + pub time: Time, |
| 269 | + pub rapier_context: RapierContextSimulation, |
| 270 | + pub colliders: RapierContextColliders, |
| 271 | + pub bodies: RapierRigidBodySet, |
| 272 | + pub joints: RapierContextJoints, |
| 273 | + pub query_pipeline: RapierQueryPipeline, |
| 274 | + pub configuration: RapierConfiguration, |
| 275 | + pub sim_to_render_time: SimulationToRenderTime, |
| 276 | +} |
| 277 | + |
| 278 | +#[derive(Component)] |
| 279 | +pub struct TaskResult { |
| 280 | + pub rapier_context: RapierContextSimulation, |
| 281 | + pub colliders: RapierContextColliders, |
| 282 | + pub bodies: RapierRigidBodySet, |
| 283 | + pub joints: RapierContextJoints, |
| 284 | + pub query_pipeline: RapierQueryPipeline, |
| 285 | + pub sim_to_render_time: SimulationToRenderTime, |
| 286 | +} |
0 commit comments