summaryrefslogtreecommitdiff
path: root/src/sim/rb_manager
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim/rb_manager')
-rw-r--r--src/sim/rb_manager/debug_ops.rs48
-rw-r--r--src/sim/rb_manager/debug_render.rs6
-rw-r--r--src/sim/rb_manager/mod.rs167
-rw-r--r--src/sim/rb_manager/rb_entity.rs39
4 files changed, 33 insertions, 227 deletions
diff --git a/src/sim/rb_manager/debug_ops.rs b/src/sim/rb_manager/debug_ops.rs
deleted file mode 100644
index cb56256..0000000
--- a/src/sim/rb_manager/debug_ops.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-use glam::{ivec2, vec2};
-
-use crate::sim::{
- cell::{cell::Cell, materials::MaterialId},
- rb_manager::RbManager,
-};
-
-pub trait DebugOperator {
- fn test_spawn_box(&mut self, x: f32, y: f32, material: MaterialId) -> ();
- fn test_spawn_ball(&mut self, x: f32, y: f32, material: MaterialId) -> ();
-}
-
-impl DebugOperator for RbManager {
- fn test_spawn_box(&mut self, x: f32, y: f32, material: MaterialId) {
- let w = 10;
- let h = 10;
- let mut test_cells = vec![Cell::void(); (w * h) as usize];
-
- for x in 0..w {
- for y in 0..h {
- let cell_idx = x + y * w;
- test_cells[cell_idx as usize] = Cell::from_material(material);
- test_cells[cell_idx as usize].set_rb(true);
- }
- }
-
- self.create_rb_entity(vec2(x, y), test_cells, w, h);
- }
-
- fn test_spawn_ball(&mut self, x: f32, y: f32, material: MaterialId) {
- let r = 5;
- let w = r * 2;
- let h = r * 2;
- let mut test_cells = vec![Cell::void(); (w * h) as usize];
-
- for x in 0..w {
- for y in 0..h {
- let cell_idx = x + y * w;
- if ivec2(x, y).distance_squared(ivec2(w / 2, h / 2)) < r.pow(2) {
- test_cells[cell_idx as usize] = Cell::from_material(material);
- test_cells[cell_idx as usize].set_rb(true);
- }
- }
- }
-
- self.create_rb_entity(vec2(x, y), test_cells, w, h);
- }
-}
diff --git a/src/sim/rb_manager/debug_render.rs b/src/sim/rb_manager/debug_render.rs
index 342fc5b..532d9a6 100644
--- a/src/sim/rb_manager/debug_render.rs
+++ b/src/sim/rb_manager/debug_render.rs
@@ -1,6 +1,6 @@
use rapier2d::pipeline::{DebugColor, DebugRenderBackend, DebugRenderObject};
-use crate::config::PIXELS_TO_METRES;
+use crate::config::CELLS_TO_METRES;
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
@@ -24,11 +24,11 @@ impl DebugRenderBackend for DebugLineBuffer {
) {
let color = hsla_to_linear_rgba(color);
self.vertices.push(DebugVertex {
- position: [a.x * PIXELS_TO_METRES, a.y * PIXELS_TO_METRES],
+ position: [a.x * CELLS_TO_METRES, a.y * CELLS_TO_METRES],
color,
});
self.vertices.push(DebugVertex {
- position: [b.x * PIXELS_TO_METRES, b.y * PIXELS_TO_METRES],
+ position: [b.x * CELLS_TO_METRES, b.y * CELLS_TO_METRES],
color,
});
}
diff --git a/src/sim/rb_manager/mod.rs b/src/sim/rb_manager/mod.rs
index 2c64f12..a70c677 100644
--- a/src/sim/rb_manager/mod.rs
+++ b/src/sim/rb_manager/mod.rs
@@ -1,38 +1,32 @@
-pub mod debug_ops;
pub mod debug_render;
-pub mod rb_entity;
use fxhash::FxHashMap;
use glam::Vec2;
-use rapier2d::{dynamics, geometry, glamx::vec2, prelude};
+use rapier2d::{geometry, glamx::vec2, prelude};
use crate::{
- config::{CHUNK_SIZE, PHYSICS_DELTA_TIME, PIXELS_TO_METRES},
+ config::{CELLS_TO_METRES, CHUNK_SIZE, PHYSICS_DELTA_TIME},
sim::{
- cell::cell::Cell,
cell_manager::chunk::Chunk,
lib::marching_squares::{Marchable, marching_squares_vertex_trace},
- rb_manager::{
- debug_render::{DebugLineBuffer, DebugVertex},
- rb_entity::RbEntity,
- },
+ rb_manager::debug_render::{DebugLineBuffer, DebugVertex},
},
};
pub use rapier2d::pipeline::DebugRenderMode;
pub struct PhysicsManager {
- rigid_body_set: prelude::RigidBodySet,
- collider_set: prelude::ColliderSet,
- physics_pipeline: prelude::PhysicsPipeline,
- integration_parameters: prelude::IntegrationParameters,
- island_manager: prelude::IslandManager,
- broad_phase: prelude::DefaultBroadPhase,
- narrow_phase: prelude::NarrowPhase,
- impulse_joint_set: prelude::ImpulseJointSet,
- multibody_joint_set: prelude::MultibodyJointSet,
- ccd_solver: prelude::CCDSolver,
- debug_render_pipeline: prelude::DebugRenderPipeline,
+ pub rigid_body_set: prelude::RigidBodySet,
+ pub collider_set: prelude::ColliderSet,
+ pub physics_pipeline: prelude::PhysicsPipeline,
+ pub integration_parameters: prelude::IntegrationParameters,
+ pub island_manager: prelude::IslandManager,
+ pub broad_phase: prelude::DefaultBroadPhase,
+ pub narrow_phase: prelude::NarrowPhase,
+ pub impulse_joint_set: prelude::ImpulseJointSet,
+ pub multibody_joint_set: prelude::MultibodyJointSet,
+ pub ccd_solver: prelude::CCDSolver,
+ pub debug_render_pipeline: prelude::DebugRenderPipeline,
}
impl PhysicsManager {
@@ -43,7 +37,7 @@ impl PhysicsManager {
physics_pipeline: prelude::PhysicsPipeline::new(),
integration_parameters: prelude::IntegrationParameters {
// 20 pixels <-> 1 meter
- length_unit: PIXELS_TO_METRES,
+ length_unit: CELLS_TO_METRES,
dt: PHYSICS_DELTA_TIME,
..prelude::IntegrationParameters::default()
},
@@ -65,12 +59,12 @@ impl PhysicsManager {
}
}
+// TODO remove
pub struct RbManager {
+ // move to sim manager
chunk_colliders: FxHashMap<(i32, i32), geometry::ColliderHandle>,
- pub rb_entities: FxHashMap<u32, RbEntity>,
-
- physics_manager: PhysicsManager,
- next_id: u32,
+ // move to sim manager
+ pub physics_manager: PhysicsManager,
debug_line_buffer: DebugLineBuffer,
}
@@ -112,104 +106,11 @@ impl RbManager {
&self.debug_line_buffer.vertices
}
- pub fn create_rb_entity(&mut self, position: Vec2, cells: Vec<Cell>, w: i32, h: i32) -> u32 {
- let id = self.next_id;
- self.next_id += 1;
-
- let rb = dynamics::RigidBodyBuilder::dynamic()
- .translation(position / PIXELS_TO_METRES)
- .build();
- let rb_handle = self.physics_manager.rigid_body_set.insert(rb);
-
- let mut entity: RbEntity = RbEntity {
- id,
- cells,
- width: w,
- height: h,
- rb: rb_handle,
- collider: None,
- };
-
- let collider = self
- .convex_hull_collider_from_marchable(&entity, None)
- .build();
- let collider_handle = self.physics_manager.collider_set.insert_with_parent(
- collider,
- rb_handle,
- &mut self.physics_manager.rigid_body_set,
- );
-
- entity.collider = Some(collider_handle);
-
- self.rb_entities.insert(id, entity);
-
- id
- }
-
- pub fn update_rb_entity(&mut self, entity_id: u32) {
- let entity = self.rb_entities.get(&entity_id).unwrap();
- let new_collider = self
- .convex_hull_collider_from_marchable(entity, None)
- .build();
-
- self.physics_manager.collider_set.remove(
- entity.collider.unwrap(),
- &mut self.physics_manager.island_manager,
- &mut self.physics_manager.rigid_body_set,
- false,
- );
-
- let new_collider_handle = self.physics_manager.collider_set.insert_with_parent(
- new_collider,
- entity.rb,
- &mut self.physics_manager.rigid_body_set,
- );
-
- let entity = self.rb_entities.get_mut(&entity_id).unwrap();
- entity.collider = Some(new_collider_handle);
- }
-
- 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,
- &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 {
- Some(entity) => {
- let rb = self.physics_manager.rigid_body_set.get(entity.rb);
- rb.map(|rb| {
- let position = rb.position();
- let angle = position.rotation.angle();
- (
- position.translation.x * PIXELS_TO_METRES,
- position.translation.y * PIXELS_TO_METRES,
- angle.cos(),
- angle.sin(),
- )
- })
- }
- None => return None,
- }
- }
-
fn polyline_from_marchable(
- &self,
marchable: &impl Marchable,
minimum_verts: Option<u32>,
) -> (Vec<Vec2>, Vec<[u32; 2]>) {
- let (w, h) = marchable.size();
- let polys = marching_squares_vertex_trace(marchable, w, h);
+ let polys = marching_squares_vertex_trace(marchable);
let mut vertices = Vec::new();
let mut indices = Vec::new();
@@ -223,8 +124,7 @@ impl RbManager {
}
for i in 0..p {
vertices.push(
- (poly[i as usize] - vec2((w as f32 - 1.0) / 2.0, (h as f32 - 1.0) / 2.0))
- / PIXELS_TO_METRES,
+ (poly[i as usize] - (marchable.size().as_vec2() - 1.0) / 2.0) / CELLS_TO_METRES,
);
indices.push([v + i, v + (i + 1) % p]);
}
@@ -234,34 +134,30 @@ impl RbManager {
}
fn polyline_collider_from_marchable(
- &self,
marchable: &impl Marchable,
minimum_verts: Option<u32>,
) -> geometry::ColliderBuilder {
- let (vertices, indices) = self.polyline_from_marchable(marchable, minimum_verts);
+ let (vertices, indices) = RbManager::polyline_from_marchable(marchable, minimum_verts);
geometry::ColliderBuilder::polyline(vertices, Some(indices))
}
- fn convex_hull_collider_from_marchable(
- &self,
+ pub fn convex_hull_collider_from_marchable(
marchable: &impl Marchable,
minimum_verts: Option<u32>,
) -> geometry::ColliderBuilder {
- let (vertices, indices) = self.polyline_from_marchable(marchable, minimum_verts);
+ let (vertices, indices) = RbManager::polyline_from_marchable(marchable, minimum_verts);
geometry::ColliderBuilder::convex_decomposition(&vertices, &indices)
}
pub fn upsert_chunk_collider(&mut self, cx: i32, cy: i32, chunk: &Chunk) {
puffin::profile_function!();
- let collider = self
- .polyline_collider_from_marchable(chunk, Some(20))
- .translation(
- vec2(
- (cx as f32 + 0.5) * CHUNK_SIZE as f32,
- (cy as f32 + 0.5) * CHUNK_SIZE as f32,
- ) / PIXELS_TO_METRES,
- );
+ let collider = RbManager::polyline_collider_from_marchable(chunk, Some(20)).translation(
+ vec2(
+ (cx as f32 + 0.5) * CHUNK_SIZE as f32,
+ (cy as f32 + 0.5) * CHUNK_SIZE as f32,
+ ) / CELLS_TO_METRES,
+ );
if let Some(handle) = self.chunk_colliders.remove(&(cx, cy)) {
self.physics_manager.collider_set.remove(
@@ -278,11 +174,8 @@ impl RbManager {
pub fn new() -> Self {
RbManager {
- rb_entities: FxHashMap::default(),
chunk_colliders: FxHashMap::default(),
-
physics_manager: PhysicsManager::new(),
- next_id: 0,
debug_line_buffer: DebugLineBuffer::default(),
}
}
diff --git a/src/sim/rb_manager/rb_entity.rs b/src/sim/rb_manager/rb_entity.rs
deleted file mode 100644
index 18a6693..0000000
--- a/src/sim/rb_manager/rb_entity.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-use rapier2d::prelude;
-
-use crate::sim::{
- cell::{cell::Cell, materials::MaterialId},
- lib::marching_squares::Marchable,
-};
-
-pub struct RbEntity {
- pub id: u32,
- pub width: i32,
- pub height: i32,
- pub cells: Vec<Cell>,
- pub rb: prelude::RigidBodyHandle,
- pub collider: Option<prelude::ColliderHandle>,
-}
-
-impl RbEntity {
- #[inline]
- pub fn get_cell_at_local_position(&self, x: u8, y: u8) -> Cell {
- self.cells[x as usize + y as usize * self.width as usize]
- }
- #[inline]
- pub fn set_cell_at_local_position(&mut self, x: u8, y: u8, cell: Cell) {
- self.cells[x as usize + y as usize * self.width as usize] = cell;
- }
-}
-
-impl Marchable for RbEntity {
- fn occupied(&self, x: i32, y: i32) -> bool {
- if x < 0 || x >= self.width || y < 0 || y >= self.height {
- false
- } else {
- self.get_cell_at_local_position(x as u8, y as u8).material != MaterialId::Void
- }
- }
- fn size(&self) -> (i32, i32) {
- (self.width, self.height)
- }
-}