diff --git a/Cargo.toml b/Cargo.toml index ec01f6a..aca94e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "examples/multi_light_demo", "examples/shadow_demo", "examples/ibl_demo", + "crates/voltex_physics", ] [workspace.dependencies] @@ -23,6 +24,7 @@ voltex_platform = { path = "crates/voltex_platform" } voltex_renderer = { path = "crates/voltex_renderer" } voltex_ecs = { path = "crates/voltex_ecs" } voltex_asset = { path = "crates/voltex_asset" } +voltex_physics = { path = "crates/voltex_physics" } wgpu = "28.0" winit = "0.30" bytemuck = { version = "1", features = ["derive"] } diff --git a/crates/voltex_physics/Cargo.toml b/crates/voltex_physics/Cargo.toml new file mode 100644 index 0000000..0e0f1c4 --- /dev/null +++ b/crates/voltex_physics/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "voltex_physics" +version = "0.1.0" +edition = "2021" + +[dependencies] +voltex_math.workspace = true +voltex_ecs.workspace = true diff --git a/crates/voltex_physics/src/collider.rs b/crates/voltex_physics/src/collider.rs new file mode 100644 index 0000000..faaebf2 --- /dev/null +++ b/crates/voltex_physics/src/collider.rs @@ -0,0 +1,42 @@ +use voltex_math::{Vec3, AABB}; + +#[derive(Debug, Clone, Copy)] +pub enum Collider { + Sphere { radius: f32 }, + Box { half_extents: Vec3 }, +} + +impl Collider { + pub fn aabb(&self, position: Vec3) -> AABB { + match self { + Collider::Sphere { radius } => { + let r = Vec3::new(*radius, *radius, *radius); + AABB::new(position - r, position + r) + } + Collider::Box { half_extents } => { + AABB::new(position - *half_extents, position + *half_extents) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sphere_aabb() { + let c = Collider::Sphere { radius: 2.0 }; + let aabb = c.aabb(Vec3::new(1.0, 0.0, 0.0)); + assert_eq!(aabb.min, Vec3::new(-1.0, -2.0, -2.0)); + assert_eq!(aabb.max, Vec3::new(3.0, 2.0, 2.0)); + } + + #[test] + fn test_box_aabb() { + let c = Collider::Box { half_extents: Vec3::new(1.0, 2.0, 3.0) }; + let aabb = c.aabb(Vec3::ZERO); + assert_eq!(aabb.min, Vec3::new(-1.0, -2.0, -3.0)); + assert_eq!(aabb.max, Vec3::new(1.0, 2.0, 3.0)); + } +} diff --git a/crates/voltex_physics/src/contact.rs b/crates/voltex_physics/src/contact.rs new file mode 100644 index 0000000..b559df5 --- /dev/null +++ b/crates/voltex_physics/src/contact.rs @@ -0,0 +1,12 @@ +use voltex_ecs::Entity; +use voltex_math::Vec3; + +#[derive(Debug, Clone, Copy)] +pub struct ContactPoint { + pub entity_a: Entity, + pub entity_b: Entity, + pub normal: Vec3, + pub depth: f32, + pub point_on_a: Vec3, + pub point_on_b: Vec3, +} diff --git a/crates/voltex_physics/src/lib.rs b/crates/voltex_physics/src/lib.rs new file mode 100644 index 0000000..6d58942 --- /dev/null +++ b/crates/voltex_physics/src/lib.rs @@ -0,0 +1,5 @@ +pub mod collider; +pub mod contact; + +pub use collider::Collider; +pub use contact::ContactPoint;