Commit e76aee14 authored by Rahix's avatar Rahix 🦀
Browse files

colliders: Delete dropped colliders

Use a channel to drop colliders which are deallocated from the world.

Unfortunately, we can only use a `SyncChannel` here which means we can
only drop a limited number of colliders per frame.  This could be
a problem at the very end of a level, when an entire level with all its
colliders is dropped.  Let's see ...
parent 145b72fa
......@@ -2,9 +2,11 @@ use crate::components;
use legion::IntoQuery;
use ncollide2d::pipeline;
use ncollide2d::shape;
use std::sync::mpsc;
pub struct Collider {
handle: Option<ncollide2d::pipeline::CollisionObjectSlabHandle>,
drop_notifier: Option<mpsc::SyncSender<ncollide2d::pipeline::CollisionObjectSlabHandle>>,
shape: shape::ShapeHandle<f32>,
groups: pipeline::CollisionGroups,
}
......@@ -13,6 +15,7 @@ impl Collider {
pub fn new(shape: shape::ShapeHandle<f32>, groups: pipeline::CollisionGroups) -> Collider {
Collider {
handle: None,
drop_notifier: None,
shape,
groups,
}
......@@ -40,14 +43,30 @@ impl Collider {
}
}
impl Drop for Collider {
fn drop(&mut self) {
if let (Some(drop_notifier), Some(handle)) = (self.drop_notifier.take(), self.handle.take())
{
if let Err(e) = drop_notifier.try_send(handle) {
crate::console_warn!("Failed to schedule collider drop!! Error: {}", e);
}
}
}
}
pub struct CollisionWorld {
world: ncollide2d::world::CollisionWorld<f32, legion::Entity>,
drop_notifier: mpsc::SyncSender<ncollide2d::pipeline::CollisionObjectSlabHandle>,
drop_receiver: mpsc::Receiver<ncollide2d::pipeline::CollisionObjectSlabHandle>,
}
impl CollisionWorld {
pub fn new() -> CollisionWorld {
let (drop_notifier, drop_receiver) = mpsc::sync_channel(32);
CollisionWorld {
world: ncollide2d::world::CollisionWorld::new(0.5),
drop_notifier,
drop_receiver,
}
}
......@@ -78,8 +97,13 @@ pub fn synchronize_collisision_world(
*ent,
);
collider.handle = Some(handle);
collider.drop_notifier = Some(collision_world.drop_notifier.clone());
}
}
while let Ok(handle) = collision_world.drop_receiver.try_recv() {
collision_world.world.remove(&[handle]);
}
}
#[legion::system]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment