diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-22 00:39:39 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-22 00:39:39 -0700 |
| commit | 21f6ee0be48f83db3a0052269cfc47ac73dc64f9 (patch) | |
| tree | c9d90a898a7cf56880a4b48ce3c6fe7c6918886c /src/sim/particle_sim | |
| parent | b13e8cc52ca3d9e80339fd6acc10159c5ffc2903 (diff) | |
refactor dda
Diffstat (limited to 'src/sim/particle_sim')
| -rw-r--r-- | src/sim/particle_sim/mod.rs | 40 |
1 files changed, 7 insertions, 33 deletions
diff --git a/src/sim/particle_sim/mod.rs b/src/sim/particle_sim/mod.rs index 08febfb..4378d25 100644 --- a/src/sim/particle_sim/mod.rs +++ b/src/sim/particle_sim/mod.rs @@ -8,6 +8,7 @@ use crate::{ materials::{MaterialForm, MaterialId}, }, cell_sim::world::World, + lib::ray::AwDda, particle_sim::particle::Particle, }, }; @@ -35,46 +36,20 @@ impl ParticleManager { p.velocity.y += PARTICLE_GRAVITY * delta_time; let dt_velocity = p.velocity * delta_time; - // Amanatides and Woo's fast Voxel Traversal - { - let mut cur = IVec2::new(p.position.x.round() as i32, p.position.y.round() as i32); - // direction we step for each component on each iteration - let step_sign = - IVec2::new(dt_velocity.x.signum() as i32, dt_velocity.y.signum() as i32); - let delta = Vec2::new(1.0 / dt_velocity.x.abs(), 1.0 / dt_velocity.y.abs()); + let mut dda = AwDda::new(p.position, p.position + p.velocity * delta_time); - let frac = Vec2::new( - if step_sign.x > 0 { - (cur.x as f32 + 0.5) - p.position.x - } else { - p.position.x - (cur.x as f32 - 0.5) - }, - if step_sign.y > 0 { - (cur.y as f32 + 0.5) - p.position.y - } else { - p.position.y - (cur.y as f32 - 0.5) - }, - ); - let mut t_max = frac * delta; - - let mut prev = cur; + if let Some(prev) = dda.next() { if let Some(cell) = world.get_cell_from_game_position(prev.x, prev.y) && [MaterialForm::Solid, MaterialForm::Powder] .contains(&cell.material.def().form) { - // already occupied, die + // the particle is already inside a collider, we should just kill it self.particles.swap_remove(i); continue 'outer; } - while t_max.min_element() <= 1.0 { - if t_max.x < t_max.y { - cur.x += step_sign.x; - t_max.x += delta.x; - } else { - cur.y += step_sign.y; - t_max.y += delta.y; - } - if let Some(cell) = world.get_cell_from_game_position(cur.x, cur.y) + + for cell in dda.map(|p| world.get_cell_from_game_position(p.x, p.y)) { + if let Some(cell) = cell && [MaterialForm::Solid, MaterialForm::Powder] .contains(&cell.material.def().form) { @@ -88,7 +63,6 @@ impl ParticleManager { self.particles.swap_remove(i); continue 'outer; } - prev = cur; } } |
