diff --git a/crates/rust4d_core/src/world.rs b/crates/rust4d_core/src/world.rs index f2fbb5e..67d9180 100644 --- a/crates/rust4d_core/src/world.rs +++ b/crates/rust4d_core/src/world.rs @@ -2,6 +2,7 @@ //! //! The World manages all entities in the simulation. +use std::collections::HashMap; use crate::Entity; use rust4d_physics::{PhysicsConfig, PhysicsWorld}; use slotmap::{new_key_type, SlotMap}; @@ -23,6 +24,8 @@ new_key_type! { pub struct World { /// All entities in the world (using generational keys) entities: SlotMap, + /// Index from entity names to keys (for fast name lookup) + name_index: HashMap, /// Optional physics simulation (None = no physics) physics_world: Option, } @@ -38,6 +41,7 @@ impl World { pub fn new() -> Self { Self { entities: SlotMap::with_key(), + name_index: HashMap::new(), physics_world: None, } } @@ -46,6 +50,7 @@ impl World { pub fn with_capacity(capacity: usize) -> Self { Self { entities: SlotMap::with_capacity_and_key(capacity), + name_index: HashMap::new(), physics_world: None, } } @@ -68,12 +73,30 @@ impl World { /// Add an entity to the world, returning its key pub fn add_entity(&mut self, entity: Entity) -> EntityKey { - self.entities.insert(entity) + // Get the name before moving the entity + let name = entity.name.clone(); + let key = self.entities.insert(entity); + + // If the entity has a name, add it to the index + if let Some(name) = name { + self.name_index.insert(name, key); + } + + key } /// Remove an entity from the world and return it pub fn remove_entity(&mut self, key: EntityKey) -> Option { - self.entities.remove(key) + // Remove from entities first + if let Some(entity) = self.entities.remove(key) { + // Clean up name index if the entity had a name + if let Some(ref name) = entity.name { + self.name_index.remove(name); + } + Some(entity) + } else { + None + } } /// Get a reference to an entity by key @@ -86,6 +109,25 @@ impl World { self.entities.get_mut(key) } + /// Get an entity by name + pub fn get_by_name(&self, name: &str) -> Option<(EntityKey, &Entity)> { + let key = *self.name_index.get(name)?; + let entity = self.entities.get(key)?; + Some((key, entity)) + } + + /// Get a mutable reference to an entity by name + pub fn get_by_name_mut(&mut self, name: &str) -> Option<(EntityKey, &mut Entity)> { + let key = *self.name_index.get(name)?; + let entity = self.entities.get_mut(key)?; + Some((key, entity)) + } + + /// Get all entities with a specific tag + pub fn get_by_tag<'a>(&'a self, tag: &'a str) -> impl Iterator { + self.entities.iter().filter(move |(_, entity)| entity.has_tag(tag)) + } + /// Get the number of entities #[inline] pub fn entity_count(&self) -> usize { @@ -129,6 +171,7 @@ impl World { /// Clear all entities from the world pub fn clear(&mut self) { self.entities.clear(); + self.name_index.clear(); } /// Iterate over all entities @@ -368,4 +411,122 @@ mod tests { assert_eq!(entity.transform.position.x, 5.0); assert_eq!(entity.transform.position.y, 5.0); } + + #[test] + fn test_get_by_name() { + let mut world = World::new(); + + // Add a named entity + let entity = make_test_entity().with_name("tesseract"); + let key = world.add_entity(entity); + + // Should be able to find by name + let result = world.get_by_name("tesseract"); + assert!(result.is_some()); + let (found_key, found_entity) = result.unwrap(); + assert_eq!(found_key, key); + assert_eq!(found_entity.name, Some("tesseract".to_string())); + + // Non-existent name should return None + assert!(world.get_by_name("nonexistent").is_none()); + } + + #[test] + fn test_get_by_name_mut() { + let mut world = World::new(); + + // Add a named entity + let entity = make_test_entity().with_name("tesseract"); + let key = world.add_entity(entity); + + // Should be able to get mutable reference by name + { + let result = world.get_by_name_mut("tesseract"); + assert!(result.is_some()); + let (found_key, entity) = result.unwrap(); + assert_eq!(found_key, key); + entity.material = Material::RED; + } + + // Verify the mutation worked + let entity = world.get_entity(key).unwrap(); + assert_eq!(entity.material.base_color, [1.0, 0.0, 0.0, 1.0]); + } + + #[test] + fn test_get_by_tag() { + let mut world = World::new(); + + // Add entities with different tags + let dynamic1 = make_test_entity().with_tag("dynamic").with_name("dyn1"); + let dynamic2 = make_test_entity().with_tag("dynamic").with_name("dyn2"); + let static1 = make_test_entity().with_tag("static").with_name("stat1"); + let _key1 = world.add_entity(dynamic1); + let _key2 = world.add_entity(dynamic2); + let _key3 = world.add_entity(static1); + + // Should find 2 dynamic entities + let dynamic_entities: Vec<_> = world.get_by_tag("dynamic").collect(); + assert_eq!(dynamic_entities.len(), 2); + + // Should find 1 static entity + let static_entities: Vec<_> = world.get_by_tag("static").collect(); + assert_eq!(static_entities.len(), 1); + assert_eq!(static_entities[0].1.name, Some("stat1".to_string())); + + // Non-existent tag should return empty iterator + let none_entities: Vec<_> = world.get_by_tag("nonexistent").collect(); + assert!(none_entities.is_empty()); + } + + #[test] + fn test_name_index_cleanup_on_remove() { + let mut world = World::new(); + + // Add a named entity + let entity = make_test_entity().with_name("tesseract"); + let key = world.add_entity(entity); + + // Should be able to find by name + assert!(world.get_by_name("tesseract").is_some()); + + // Remove the entity + world.remove_entity(key); + + // Name should no longer be in the index + assert!(world.get_by_name("tesseract").is_none()); + } + + #[test] + fn test_name_index_cleanup_on_clear() { + let mut world = World::new(); + + // Add named entities + world.add_entity(make_test_entity().with_name("entity1")); + world.add_entity(make_test_entity().with_name("entity2")); + + // Should be able to find by name + assert!(world.get_by_name("entity1").is_some()); + assert!(world.get_by_name("entity2").is_some()); + + // Clear the world + world.clear(); + + // Names should no longer be in the index + assert!(world.get_by_name("entity1").is_none()); + assert!(world.get_by_name("entity2").is_none()); + } + + #[test] + fn test_entity_without_name() { + let mut world = World::new(); + + // Add an unnamed entity + let entity = make_test_entity(); + let key = world.add_entity(entity); + + // Entity should exist but not be findable by any name + assert!(world.get_entity(key).is_some()); + assert!(world.get_by_name("").is_none()); + } } diff --git a/crates/rust4d_physics/src/world.rs b/crates/rust4d_physics/src/world.rs index b48150e..ed6abb6 100644 --- a/crates/rust4d_physics/src/world.rs +++ b/crates/rust4d_physics/src/world.rs @@ -2,6 +2,7 @@ use crate::body::{BodyKey, RigidBody4D}; use crate::collision::{aabb_vs_aabb, aabb_vs_plane, sphere_vs_aabb, sphere_vs_plane}; +use crate::material::PhysicsMaterial; use crate::shapes::{Collider, Plane4D}; use rust4d_math::Vec4; use slotmap::SlotMap; @@ -13,8 +14,8 @@ pub struct PhysicsConfig { pub gravity: f32, /// Y position of the floor plane pub floor_y: f32, - /// Default restitution (bounciness) for collisions - pub restitution: f32, + /// Physics material for the floor + pub floor_material: PhysicsMaterial, } impl Default for PhysicsConfig { @@ -22,20 +23,29 @@ impl Default for PhysicsConfig { Self { gravity: -20.0, floor_y: 0.0, - restitution: 0.0, + floor_material: PhysicsMaterial::CONCRETE, } } } impl PhysicsConfig { /// Create a new physics config with custom values + /// + /// The `restitution` parameter sets floor material restitution for backwards compatibility. + /// For full material control, use `with_floor_material()`. pub fn new(gravity: f32, floor_y: f32, restitution: f32) -> Self { Self { gravity, floor_y, - restitution, + floor_material: PhysicsMaterial::new(PhysicsMaterial::CONCRETE.friction, restitution), } } + + /// Set the floor material + pub fn with_floor_material(mut self, material: PhysicsMaterial) -> Self { + self.floor_material = material; + self + } } /// The physics world containing all rigid bodies @@ -44,6 +54,8 @@ pub struct PhysicsWorld { bodies: SlotMap, /// The floor plane floor: Plane4D, + /// Physics material for the floor + floor_material: PhysicsMaterial, /// Physics configuration pub config: PhysicsConfig, } @@ -59,6 +71,7 @@ impl PhysicsWorld { Self { bodies: SlotMap::with_key(), floor: Plane4D::floor(config.floor_y), + floor_material: config.floor_material, config, } } @@ -147,7 +160,7 @@ impl PhysicsWorld { let velocity_along_normal = body.velocity.dot(contact.normal); if velocity_along_normal < 0.0 { // Body is moving into the floor - let restitution = body.material.restitution.max(self.config.restitution); + let restitution = body.material.restitution.max(self.floor_material.restitution); // Remove the normal component of velocity and optionally bounce let normal_velocity = contact.normal * velocity_along_normal; @@ -274,10 +287,14 @@ impl PhysicsWorld { } // Handle velocity response (simple push-apart) + // Use the maximum restitution between the two colliding bodies + let restitution_a = self.bodies[key_a].material.restitution; + let restitution_b = self.bodies[key_b].material.restitution; + let restitution = restitution_a.max(restitution_b); + if !is_static_a { let vel_along_normal = self.bodies[key_a].velocity.dot(-contact.normal); if vel_along_normal < 0.0 { - let restitution = self.bodies[key_a].material.restitution.max(self.config.restitution); let normal_velocity = -contact.normal * vel_along_normal; self.bodies[key_a].velocity = self.bodies[key_a].velocity - normal_velocity * (1.0 + restitution); } @@ -286,7 +303,6 @@ impl PhysicsWorld { if !is_static_b { let vel_along_normal = self.bodies[key_b].velocity.dot(contact.normal); if vel_along_normal < 0.0 { - let restitution = self.bodies[key_b].material.restitution.max(self.config.restitution); let normal_velocity = contact.normal * vel_along_normal; self.bodies[key_b].velocity = self.bodies[key_b].velocity - normal_velocity * (1.0 + restitution); } @@ -309,7 +325,7 @@ mod tests { let config = PhysicsConfig::default(); assert_eq!(config.gravity, -20.0); assert_eq!(config.floor_y, 0.0); - assert_eq!(config.restitution, 0.0); + assert_eq!(config.floor_material, PhysicsMaterial::CONCRETE); } #[test] @@ -317,7 +333,7 @@ mod tests { let config = PhysicsConfig::new(-10.0, 5.0, 0.5); assert_eq!(config.gravity, -10.0); assert_eq!(config.floor_y, 5.0); - assert_eq!(config.restitution, 0.5); + assert_eq!(config.floor_material.restitution, 0.5); } #[test]