summaryrefslogtreecommitdiff
path: root/src/sim/rb_sim/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim/rb_sim/mod.rs')
-rw-r--r--src/sim/rb_sim/mod.rs57
1 files changed, 37 insertions, 20 deletions
diff --git a/src/sim/rb_sim/mod.rs b/src/sim/rb_sim/mod.rs
index d76a721..10172a1 100644
--- a/src/sim/rb_sim/mod.rs
+++ b/src/sim/rb_sim/mod.rs
@@ -54,7 +54,7 @@ impl PhysicsManager {
pub struct RbSimManager {
physics_manager: PhysicsManager,
pub rb_entities: FxHashMap<u32, RbEntity>,
- last_id: u32,
+ next_id: u32,
}
impl RbSimManager {
@@ -77,6 +77,36 @@ impl RbSimManager {
);
}
+ pub fn create_rb_entity(
+ &mut self,
+ cells: Box<[Cell; CELLS_IN_CHUNK]>,
+ rb_parent: prelude::RigidBodyHandle,
+ ) {
+ let id = self.next_id;
+ self.next_id += 1;
+ let entity: RbEntity = RbEntity {
+ id,
+ cells,
+ rb_parent,
+ };
+
+ self.rb_entities.insert(id, entity);
+ }
+
+ pub fn destroy_rb_entity(&mut self, entity_id: u32) {
+ if let Some(entity) = self.rb_entities.get(&entity_id) {
+ self.physics_manager.rigid_body_set.remove(
+ entity.rb_parent,
+ &mut self.physics_manager.island_manager,
+ &mut self.physics_manager.collider_set,
+ &mut self.physics_manager.impulse_joint_set,
+ &mut self.physics_manager.multibody_joint_set,
+ true,
+ );
+ self.rb_entities.remove(&entity_id);
+ }
+ }
+
pub fn get_rb_entity_transform(&self, entity_id: u32) -> Option<(f32, f32, f32, f32)> {
let entity = self.rb_entities.get(&entity_id);
match entity {
@@ -109,7 +139,7 @@ impl RbSimManager {
self.physics_manager.collider_set.insert(collider);
}
- pub fn test_spawn_box(&mut self, x: f32, y: f32) {
+ pub fn test_spawn_box(&mut self, x: f32, y: f32, material: MaterialId) {
/* Create the bouncing ball. */
let rigid_body = dynamics::RigidBodyBuilder::dynamic()
.translation(math::Vector::new(
@@ -127,36 +157,23 @@ impl RbSimManager {
&mut self.physics_manager.rigid_body_set,
);
- let id = self.last_id;
- self.last_id += 1;
-
- let test_cells = Box::new([Cell::void(); CELLS_IN_CHUNK]);
- let mut rb_entity = RbEntity {
- id,
- cells: test_cells,
- rb_parent: ball_body_handle,
- };
+ let mut test_cells = Box::new([Cell::void(); CELLS_IN_CHUNK]);
- let mut idx = 0;
for x in CHUNK_SIZE / 2 - 5..CHUNK_SIZE / 2 + 5 {
for y in CHUNK_SIZE / 2 - 5..CHUNK_SIZE / 2 + 5 {
- idx += 1;
- rb_entity.set_cell_at_local_position(
- x as u8,
- y as u8,
- Cell::from_material(MaterialId::from_index(1 + idx % 6)),
- );
+ let cell_idx = x + y * CHUNK_SIZE;
+ test_cells[cell_idx as usize] = Cell::from_material(material);
}
}
- self.rb_entities.insert(id, rb_entity);
+ self.create_rb_entity(test_cells, ball_body_handle);
}
pub fn new() -> Self {
RbSimManager {
physics_manager: PhysicsManager::new(),
rb_entities: FxHashMap::default(),
- last_id: 0,
+ next_id: 0,
}
}
}