Skip to content

Commit

Permalink
fix events test
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jun 26, 2024
1 parent 4e7c36c commit 4952ea5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
33 changes: 14 additions & 19 deletions src/pipeline/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ mod test {
}
}
pub fn save_events<E: Event + Clone>(
mut collision_events: EventReader<E>,
mut events: EventReader<E>,
mut saver: ResMut<EventsSaver<E>>,
) {
for event in collision_events.read() {
for event in events.read() {
saver.events.push(event.clone());
}
}
Expand All @@ -166,8 +166,16 @@ mod test {
.add_systems(PostUpdate, save_events::<ContactForceEvent>)
.init_resource::<EventsSaver<CollisionEvent>>()
.init_resource::<EventsSaver<ContactForceEvent>>();

for _ in 0..200 {
// while app.plugins_state() == bevy::app::PluginsState::Adding {
// #[cfg(not(target_arch = "wasm32"))]
// bevy::tasks::tick_global_task_pools_on_main_thread();
// }
// app.finish();
// app.cleanup();
let mut time = app.world_mut().get_resource_mut::<Time<Virtual>>().unwrap();
time.set_relative_speed(1000f32);
for _ in 0..300 {
// FIXME: advance by set durations to avoid being at the mercy of the CPU.
app.update();
}
let saved_collisions = app
Expand All @@ -185,29 +193,16 @@ mod test {
/// Adapted from events example
fn main() {
let mut app = App::new();
app.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
app.add_plugins((
HeadlessRenderPlugin,
TransformPlugin,
TimePlugin,
RapierPhysicsPlugin::<NoUserData>::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics));

.add_systems(Startup, setup_physics);
run_test(&mut app);
}

pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 25.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}

pub fn setup_physics(mut commands: Commands) {
/*
* Ground
Expand Down
40 changes: 35 additions & 5 deletions src/plugin/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub struct RapierContext {
// physics update, to the entity they was attached to.
#[cfg_attr(feature = "serde-serialize", serde(skip))]
pub(crate) deleted_colliders: HashMap<ColliderHandle, Entity>,

pub(crate) collision_events_to_send: Vec<CollisionEvent>,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
pub(crate) contact_force_events_to_send: Vec<ContactForceEvent>,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
pub(crate) character_collisions_collector: Vec<rapier::control::CharacterCollision>,
}
Expand All @@ -105,7 +109,9 @@ impl Default for RapierContext {
entity2impulse_joint: HashMap::new(),
entity2multibody_joint: HashMap::new(),
deleted_colliders: HashMap::new(),
character_collisions_collector: vec![],
collision_events_to_send: Vec::new(),
contact_force_events_to_send: Vec::new(),
character_collisions_collector: Vec::new(),
}
}
}
Expand Down Expand Up @@ -234,7 +240,7 @@ impl RapierContext {
None
};

let events = self
let event_handler = self
.event_handler
.as_deref()
.or_else(|| event_queue.as_ref().map(|q| q as &dyn EventHandler))
Expand Down Expand Up @@ -284,7 +290,7 @@ impl RapierContext {
&mut self.ccd_solver,
None,
hooks,
events,
event_handler,
);
}

Expand Down Expand Up @@ -315,7 +321,7 @@ impl RapierContext {
&mut self.ccd_solver,
None,
hooks,
events,
event_handler,
);
}
}
Expand All @@ -339,11 +345,35 @@ impl RapierContext {
&mut self.ccd_solver,
None,
hooks,
events,
event_handler,
);
}
}
}
if let Some(mut event_queue) = event_queue {
self.collision_events_to_send =
std::mem::take(event_queue.collision_events.get_mut().unwrap());
self.contact_force_events_to_send =
std::mem::take(event_queue.contact_force_events.get_mut().unwrap());
}
}
/// Generates bevy events for any physics interactions that have happened
/// that are stored in the events list
pub fn send_bevy_events(
&mut self,
collision_event_writer: &mut EventWriter<CollisionEvent>,
contact_force_event_writer: &mut EventWriter<ContactForceEvent>,
) {
for collision_event in self.collision_events_to_send.iter() {
collision_event_writer.send(*collision_event);
}
self.collision_events_to_send.clear();

for contact_force_event in self.contact_force_events_to_send.iter() {
contact_force_event_writer.send(*contact_force_event);
}

self.contact_force_events_to_send.clear();
}

/// This method makes sure that the rigid-body positions have been propagated to
Expand Down
5 changes: 3 additions & 2 deletions src/plugin/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub fn step_simulation<Hooks>(
hooks: StaticSystemParam<Hooks>,
time: Res<Time>,
mut sim_to_render_time: Query<&mut SimulationToRenderTime>,
collision_events: EventWriter<CollisionEvent>,
contact_force_events: EventWriter<ContactForceEvent>,
mut collision_events: EventWriter<CollisionEvent>,
mut contact_force_events: EventWriter<ContactForceEvent>,
mut interpolation_query: Query<(&RapierRigidBodyHandle, &mut TransformInterpolation)>,
) where
Hooks: 'static + BevyPhysicsHooks,
Expand Down Expand Up @@ -64,6 +64,7 @@ pub fn step_simulation<Hooks>(
if config.query_pipeline_active {
context.update_query_pipeline();
}
context.send_bevy_events(&mut collision_events, &mut contact_force_events);
}
}

Expand Down

0 comments on commit 4952ea5

Please sign in to comment.