summaryrefslogtreecommitdiff
path: root/src/sim/entity
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim/entity')
-rw-r--r--src/sim/entity/mod.rs56
1 files changed, 29 insertions, 27 deletions
diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs
index d8a03e0..3a3fb1d 100644
--- a/src/sim/entity/mod.rs
+++ b/src/sim/entity/mod.rs
@@ -13,6 +13,13 @@ use crate::{
},
};
+fn mass_from_cells(cells: &[Cell]) -> f32 {
+ cells
+ .iter()
+ .fold(0, |acc, cur| acc + cur.material.def().density) as f32
+ * MASS_SCALING
+}
+
pub struct EntityCells {
pub size: IVec2,
pub cells: Vec<Cell>,
@@ -43,13 +50,7 @@ impl Marchable for EntityCells {
}
pub trait EntityBehaviour {
- fn update(
- &mut self,
- _update_ctx: &mut EntityUpdateCtx,
- _ctx: &mut SimCtx,
- _delta_time: f32,
- ) {
- }
+ fn update(&mut self, _update_ctx: &mut EntityUpdateCtx, _ctx: &mut SimCtx, _delta_time: f32) {}
fn physics_update(
&mut self,
_update_ctx: &mut EntityUpdateCtx,
@@ -76,14 +77,8 @@ impl EntityDef {
.translation(position / CELLS_TO_METRES)
.build();
- let mass = cells
- .cells
- .iter()
- .fold(0, |acc, cur| acc + cur.material.def().density) as f32
- * MASS_SCALING;
-
let collider = RbManager::convex_hull_collider_from_marchable(&cells, None)
- .mass(mass)
+ .mass(mass_from_cells(&cells.cells))
.build();
EntityDef {
@@ -130,19 +125,18 @@ impl<'a> EntityUpdateCtx<'a> {
impl EntityData {
pub fn transform(&self, ctx: &SimCtx) -> Option<(Vec2, (f32, f32))> {
- self.rb_h
- .and_then(|rb_h| {
- ctx.rb_manager
- .physics_manager
- .rigid_body_set
- .get(rb_h)
- .map(|rb| {
- (
- rb.translation() * CELLS_TO_METRES,
- (rb.rotation().cos(), rb.rotation().sin()),
- )
- })
- })
+ self.rb_h.and_then(|rb_h| {
+ ctx.rb_manager
+ .physics_manager
+ .rigid_body_set
+ .get(rb_h)
+ .map(|rb| {
+ (
+ rb.translation() * CELLS_TO_METRES,
+ (rb.rotation().cos(), rb.rotation().sin()),
+ )
+ })
+ })
}
}
@@ -174,6 +168,14 @@ impl Entity {
None
}
+ pub fn compute_collider(&self) -> Option<Collider> {
+ self.data.cells.as_ref().map(|cells| {
+ RbManager::convex_hull_collider_from_marchable(cells, None)
+ .mass(mass_from_cells(&cells.cells))
+ .build()
+ })
+ }
+
pub fn new(
id: u32,
rb_h: Option<RigidBodyHandle>,