feat(physics): add voltex_physics crate with Collider and ContactPoint
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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"] }
|
||||
|
||||
8
crates/voltex_physics/Cargo.toml
Normal file
8
crates/voltex_physics/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "voltex_physics"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
voltex_math.workspace = true
|
||||
voltex_ecs.workspace = true
|
||||
42
crates/voltex_physics/src/collider.rs
Normal file
42
crates/voltex_physics/src/collider.rs
Normal file
@@ -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));
|
||||
}
|
||||
}
|
||||
12
crates/voltex_physics/src/contact.rs
Normal file
12
crates/voltex_physics/src/contact.rs
Normal file
@@ -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,
|
||||
}
|
||||
5
crates/voltex_physics/src/lib.rs
Normal file
5
crates/voltex_physics/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod collider;
|
||||
pub mod contact;
|
||||
|
||||
pub use collider::Collider;
|
||||
pub use contact::ContactPoint;
|
||||
Reference in New Issue
Block a user