summaryrefslogtreecommitdiff
path: root/src/sim
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim')
-rw-r--r--src/sim/cell/materials/mod.rs4
-rw-r--r--src/sim/rb_sim/mod.rs52
2 files changed, 42 insertions, 14 deletions
diff --git a/src/sim/cell/materials/mod.rs b/src/sim/cell/materials/mod.rs
index a55ed51..0d19696 100644
--- a/src/sim/cell/materials/mod.rs
+++ b/src/sim/cell/materials/mod.rs
@@ -85,4 +85,8 @@ impl MaterialId {
pub fn def(self) -> &'static MaterialDef {
&MATERIALS[self as usize]
}
+
+ pub fn from_index(idx: u8) -> MaterialId {
+ MaterialId::ALL[idx as usize]
+ }
}
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,
}
}
}