diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-16 19:27:17 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-16 19:27:17 -0700 |
| commit | f68d8371a11b002f5494c7294551a8f955e69c0c (patch) | |
| tree | 715a09bd26c3ad6e6555e0b5df7c076174b56be5 /src/sim/rb_sim/mod.rs | |
| parent | 49f5e2593b9c1c2a0fd8267ef5d5949d69cea4f6 (diff) | |
add test, fix scaling
Diffstat (limited to 'src/sim/rb_sim/mod.rs')
| -rw-r--r-- | src/sim/rb_sim/mod.rs | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/src/sim/rb_sim/mod.rs b/src/sim/rb_sim/mod.rs index 4cac8a2..d76a721 100644 --- a/src/sim/rb_sim/mod.rs +++ b/src/sim/rb_sim/mod.rs @@ -9,7 +9,7 @@ use rapier2d::{ }; use crate::{ - config::{CELLS_IN_CHUNK, PHYSICS_DELTA_TIME}, + config::{CELLS_IN_CHUNK, CHUNK_SIZE, PHYSICS_DELTA_TIME, PIXELS_TO_METRES}, sim::{ cell::{cell::Cell, materials::MaterialId}, rb_sim::rb_entity::RbEntity, @@ -36,6 +36,8 @@ impl PhysicsManager { collider_set: prelude::ColliderSet::new(), physics_pipeline: prelude::PhysicsPipeline::new(), integration_parameters: prelude::IntegrationParameters { + // 20 pixels <-> 1 meter + length_unit: PIXELS_TO_METRES, dt: PHYSICS_DELTA_TIME, ..prelude::IntegrationParameters::default() }, @@ -52,11 +54,12 @@ impl PhysicsManager { pub struct RbSimManager { physics_manager: PhysicsManager, pub rb_entities: FxHashMap<u32, RbEntity>, + last_id: u32, } impl RbSimManager { pub fn rb_tick(&mut self, delta_time: f32) { - let gravity = vec2(0.0, 25.0); + let gravity = vec2(0.0, 9.81); self.physics_manager.physics_pipeline.step( gravity, @@ -83,8 +86,8 @@ impl RbSimManager { let position = rb.position(); let angle = position.rotation.angle(); ( - position.translation.x, - position.translation.y, + position.translation.x * PIXELS_TO_METRES, + position.translation.y * PIXELS_TO_METRES, angle.cos(), angle.sin(), ) @@ -96,16 +99,27 @@ impl RbSimManager { pub fn test(&mut self) { /* Create the ground. */ - let collider = geometry::ColliderBuilder::cuboid(100.0, 0.1).build(); + let collider = + geometry::ColliderBuilder::cuboid(100.0 / PIXELS_TO_METRES, 8.0 / PIXELS_TO_METRES) + .position(math::Pose2::from_translation(vec2( + 0 as f32, + CHUNK_SIZE as f32 / PIXELS_TO_METRES, + ))) + .build(); self.physics_manager.collider_set.insert(collider); + } + pub fn test_spawn_box(&mut self, x: f32, y: f32) { /* Create the bouncing ball. */ let rigid_body = dynamics::RigidBodyBuilder::dynamic() - .translation(math::Vector::new(0.0, -100.0)) - .build(); - let collider = geometry::ColliderBuilder::ball(5.0) - .restitution(0.7) + .translation(math::Vector::new( + x / PIXELS_TO_METRES, + y / PIXELS_TO_METRES, + )) .build(); + let collider = + geometry::ColliderBuilder::cuboid(5.0 / PIXELS_TO_METRES, 5.0 / PIXELS_TO_METRES) + .build(); let ball_body_handle = self.physics_manager.rigid_body_set.insert(rigid_body); self.physics_manager.collider_set.insert_with_parent( collider, @@ -113,26 +127,36 @@ 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: 0, + id, cells: test_cells, rb_parent: ball_body_handle, }; - for x in 0..10 { - for y in 0..10 { - rb_entity.set_cell_at_local_position(x, y, Cell::from_material(MaterialId::Wood)); + 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)), + ); } } - self.rb_entities.insert(0, rb_entity); + self.rb_entities.insert(id, rb_entity); } pub fn new() -> Self { RbSimManager { physics_manager: PhysicsManager::new(), rb_entities: FxHashMap::default(), + last_id: 0, } } } |
