summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-16 19:27:17 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-16 19:27:17 -0700
commitf68d8371a11b002f5494c7294551a8f955e69c0c (patch)
tree715a09bd26c3ad6e6555e0b5df7c076174b56be5 /src
parent49f5e2593b9c1c2a0fd8267ef5d5949d69cea4f6 (diff)
add test, fix scaling
Diffstat (limited to 'src')
-rw-r--r--src/camera.rs2
-rw-r--r--src/config.rs2
-rw-r--r--src/main.rs21
-rw-r--r--src/sim/cell/materials/mod.rs4
-rw-r--r--src/sim/rb_sim/mod.rs52
5 files changed, 60 insertions, 21 deletions
diff --git a/src/camera.rs b/src/camera.rs
index bd4f290..13c5ec9 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -82,7 +82,7 @@ impl Camera {
pub fn new(screen_size: (i32, i32)) -> Self {
Camera {
- zoom: 0.2,
+ zoom: 0.15,
centre: (0.0, 0.0),
screen_size,
}
diff --git a/src/config.rs b/src/config.rs
index 8739c53..eeb4f7e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -9,3 +9,5 @@ pub const SIM_FPS: u32 = 120;
// pub const SIM_DELTA_TIME: f32 = 1.0 / SIM_FPS as f32;
pub const PHYSICS_FPS: u32 = 60;
pub const PHYSICS_DELTA_TIME: f32 = 1.0 / PHYSICS_FPS as f32;
+
+pub const PIXELS_TO_METRES: f32 = 20.0;
diff --git a/src/main.rs b/src/main.rs
index 7a54638..976a272 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -41,6 +41,7 @@ struct Input {
last_mouse_pos_on_screen: Option<(f32, f32)>,
last_mouse_world_pos: Option<(f32, f32)>,
is_lmb_pressed: bool,
+ trigger_test_1: bool,
// keybindings
is_up_pressed: bool,
@@ -95,7 +96,16 @@ impl App {
camera.handle_camera_input(&self.input, delta_time)
}
- // // --TEST DRAWING--
+ // --TEST SPAWNING--
+ if self.input.trigger_test_1
+ && let Some(lm) = self.input.last_mouse_world_pos
+ && let Some(rbsm) = &mut self.rb_sim_manager
+ {
+ self.input.trigger_test_1 = false;
+ rbsm.test_spawn_box(lm.0, lm.1);
+ }
+
+ // --TEST DRAWING--
if self.input.is_lmb_pressed
&& let Some(lm) = self.input.last_mouse_world_pos
{
@@ -157,6 +167,7 @@ impl Default for App {
last_mouse_pos_on_screen: None,
last_mouse_world_pos: None,
+ trigger_test_1: false,
is_lmb_pressed: false,
is_up_pressed: false,
is_left_pressed: false,
@@ -240,6 +251,7 @@ impl ApplicationHandler for App {
KeyEvent {
physical_key: PhysicalKey::Code(code),
state,
+ repeat,
..
},
..
@@ -251,12 +263,9 @@ impl ApplicationHandler for App {
KeyCode::KeyS => self.input.is_down_pressed = pressed,
KeyCode::KeyD => self.input.is_right_pressed = pressed,
KeyCode::KeyC => self.world = Some(World::from_default_size()),
- KeyCode::Space => {
- if pressed {
- self.sim_paused = !self.sim_paused
- }
- }
+ KeyCode::Space if pressed => self.sim_paused = !self.sim_paused,
KeyCode::KeyX if pressed => self.ignore_pause_next_tick = true,
+ KeyCode::KeyQ if pressed && !repeat => self.input.trigger_test_1 = true,
_ => {}
}
}
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,
}
}
}