Spatial Grid Plugin, Forest Fire and Spatial Pandemic Examples#1
Conversation
haath
left a comment
There was a problem hiding this comment.
Great stuff, thanks a lot!
Only one point I'd like to discuss: what do you think about making the dimensionality generic so that it works both for 2D and 3D grids?
It seems most of the code would be the same, and we would get the extra dimension practically for free.
The way I'm thinking it SpatialGrid would be generic over a sealed trait which we would implement only for IVec2 and IVec3.
Then we would provide shorthand types for the user like this:
type SpatialGrid2D = SpatialGrid<IVec2>;
type SpatialGrid3D = SpatialGrid<IVec3>;Let me know if you'd like to take on this effort yourself.
No worries if you'd rather not spend more time on this, I could also do it in a follow-up change.
|
Ok, lots of feedback, appreciate you taking the time to go through this. I think I covered all of them. Was trying this repo out of curiosity, but really liking how you structured this and contained the bevy complexities while making good use of bevys strength. Let me know if I missed anything. There are many projects I want to do with a system like this, so its in my best interest to help out with anything I can. |
|
Added a multi grid example: let mut simulation = SimulationBuilder::new()
// Create separate spatial grids for each entity type
.add_spatial_grid::<IVec2, Prey>(bounds)
.add_spatial_grid::<IVec2, Predator>(bounds)
.add_spatial_grid::<IVec2, Vegetation>(bounds)
.add_entity_spawner(spawn_ecosystem)
.add_systems((
prey_behavior,
predator_hunting.after(prey_behavior),
vegetation_dynamics.after(predator_hunting),
prey_feeding.after(vegetation_dynamics),
lifecycle_system.after(prey_feeding),
display_ecosystem_stats.after(lifecycle_system),
))
.build(); |
|
Tests and examples are updated and clippy fixed. Ecosystem removed. |
5f8d2ac to
8fe1a67
Compare
|
rebased on main |
|
Also, do not feel pressured to accept this PR. Already getting a good learning experience out of the process. |
|
|
||
| mod error; | ||
| mod plugins; | ||
| pub mod plugins; |
There was a problem hiding this comment.
Don't expose the plugins module.
Instead we do pub use only for the types the user might need to reference (GridPosition, SpatialGrid etc).
| { | ||
| for (entity, position) in &query | ||
| { | ||
| spatial_grid.remove(entity); |
There was a problem hiding this comment.
Sorry my bad, I didn't see that insert() calls remove() on its own.
Can remove this line.
| /// Returns the position where the entity was located, if it was found. | ||
| pub(crate) fn remove(&mut self, entity: Entity) -> Option<GridPosition<T>> | ||
| { | ||
| if let Some(position) = self.entity_to_position.remove(&entity) |
There was a problem hiding this comment.
Let's handle the error case as well.
let position = self.entity_to_position.remove(&entity)?;
let Some(entities_at_position) = self.position_to_entities.get_mut(&position)
else
{
panic!("entity found in one hashmap but not the other?");
};
entities_at_position.remove(&entity);
if entities_at_position.is_empty()
{
self.position_to_entities.remove(&position);
}
Some(position)| } | ||
|
|
||
| /// Clear all entities from the spatial index. | ||
| pub fn clear(&mut self) |
There was a problem hiding this comment.
Should be private.
Same for insert and remove actually, I think pub(crate) is not necessary.
| { | ||
| // only get new samples once every 'sample_interval' steps | ||
| if step_counter.is_multiple_of(time_series.sample_interval) | ||
| if (**step_counter).is_multiple_of(time_series.sample_interval) |
There was a problem hiding this comment.
Explicit deref with ** is not needed here.
Adds spatial indexing capabilities for grid-based Monte Carlo simulations.
New Features
SpatialGridPlugin
SimulationBuilder Integration
Bevy Math Integration
Examples
Forest Fire Simulation
Pandemic Simulation
Implementation Details