Skip to main content

gizmo_physics/components/
collider.rs

1use gizmo_math::{Quat, Vec3};
2use serde::{Deserialize, Serialize};
3
4use super::{CollisionLayer, PhysicsMaterial, Transform};
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct Collider {
8    pub shape: ColliderShape,
9    pub is_trigger: bool,
10    pub material: PhysicsMaterial,
11    pub collision_layer: CollisionLayer,
12}
13
14impl Default for Collider {
15    fn default() -> Self {
16        Self {
17            shape: ColliderShape::Sphere(SphereShape { radius: 0.5 }),
18            is_trigger: false,
19            material: PhysicsMaterial::default(),
20            collision_layer: CollisionLayer::default(),
21        }
22    }
23}
24
25impl Collider {
26    /// Calculate AABB for this collider at given transform
27    pub fn compute_aabb(&self, position: Vec3, rotation: Quat) -> gizmo_math::Aabb {
28        match &self.shape {
29            ColliderShape::Sphere(s) => {
30                let radius_vec = Vec3::splat(s.radius);
31                gizmo_math::Aabb::from_center_half_extents(position, radius_vec)
32            }
33            ColliderShape::Box(b) => {
34                // Rotate the half extents to get world-space AABB
35                let corners = [
36                    Vec3::new(b.half_extents.x, b.half_extents.y, b.half_extents.z),
37                    Vec3::new(-b.half_extents.x, b.half_extents.y, b.half_extents.z),
38                    Vec3::new(b.half_extents.x, -b.half_extents.y, b.half_extents.z),
39                    Vec3::new(b.half_extents.x, b.half_extents.y, -b.half_extents.z),
40                    Vec3::new(-b.half_extents.x, -b.half_extents.y, b.half_extents.z),
41                    Vec3::new(-b.half_extents.x, b.half_extents.y, -b.half_extents.z),
42                    Vec3::new(b.half_extents.x, -b.half_extents.y, -b.half_extents.z),
43                    Vec3::new(-b.half_extents.x, -b.half_extents.y, -b.half_extents.z),
44                ];
45
46                let mut min = Vec3::splat(f32::INFINITY);
47                let mut max = Vec3::splat(f32::NEG_INFINITY);
48
49                for corner in &corners {
50                    let rotated = rotation * (*corner);
51                    let world_pos = position + rotated;
52                    min = min.min(world_pos);
53                    max = max.max(world_pos);
54                }
55
56                gizmo_math::Aabb::new(min, max)
57            }
58            ColliderShape::Capsule(c) => {
59                let axis = rotation * Vec3::Y;
60                let half_height_vec = axis * c.half_height;
61                let radius_vec = Vec3::splat(c.radius);
62                let extent = half_height_vec.abs() + radius_vec;
63                gizmo_math::Aabb::from_center_half_extents(position, extent)
64            }
65            ColliderShape::Plane(_) => {
66                // Infinite plane - use a very large AABB
67                let large = 10000.0;
68                gizmo_math::Aabb::new(position - Vec3::splat(large), position + Vec3::splat(large))
69            }
70            ColliderShape::TriMesh(tm) => {
71                let mut min = Vec3::splat(f32::INFINITY);
72                let mut max = Vec3::splat(f32::NEG_INFINITY);
73                for v in tm.vertices.iter() {
74                    let world_pos = position + rotation * (*v);
75                    min = min.min(world_pos);
76                    max = max.max(world_pos);
77                }
78                gizmo_math::Aabb::new(min, max)
79            }
80            ColliderShape::ConvexHull(ch) => {
81                let mut min = Vec3::splat(f32::INFINITY);
82                let mut max = Vec3::splat(f32::NEG_INFINITY);
83                for v in ch.vertices.iter() {
84                    let world_pos = position + rotation * (*v);
85                    min = min.min(world_pos);
86                    max = max.max(world_pos);
87                }
88                gizmo_math::Aabb::new(min, max)
89            }
90            ColliderShape::Compound(shapes) => {
91                let mut min = Vec3::splat(f32::INFINITY);
92                let mut max = Vec3::splat(f32::NEG_INFINITY);
93                for (local_t, sub_shape) in shapes {
94                    let world_pos = position + rotation.mul_vec3(local_t.position);
95                    let world_rot = rotation * local_t.rotation;
96
97                    let temp_col = Collider {
98                        shape: (**sub_shape).clone(),
99                        ..Default::default()
100                    };
101                    let sub_aabb = temp_col.compute_aabb(world_pos, world_rot);
102                    min = min.min(sub_aabb.min.into());
103                    max = max.max(sub_aabb.max.into());
104                }
105                gizmo_math::Aabb::new(min, max)
106            }
107        }
108    }
109
110    pub fn plane(normal: Vec3, distance: f32) -> Self {
111        Self {
112            shape: ColliderShape::Plane(PlaneShape { normal, distance }),
113            ..Default::default()
114        }
115    }
116
117    pub fn sphere(radius: f32) -> Self {
118        Self {
119            shape: ColliderShape::Sphere(SphereShape { radius }),
120            ..Default::default()
121        }
122    }
123
124    pub fn box_collider(half_extents: Vec3) -> Self {
125        Self {
126            shape: ColliderShape::Box(BoxShape { half_extents }),
127            ..Default::default()
128        }
129    }
130
131    pub fn capsule(radius: f32, half_height: f32) -> Self {
132        Self {
133            shape: ColliderShape::Capsule(CapsuleShape {
134                radius,
135                half_height,
136            }),
137            ..Default::default()
138        }
139    }
140
141    pub fn convex_hull(points: &[Vec3]) -> Self {
142        let hull = crate::quickhull::compute_convex_hull(points);
143        Self {
144            shape: ColliderShape::ConvexHull(ConvexHullShape {
145                vertices: std::sync::Arc::new(hull.vertices),
146                faces: std::sync::Arc::new(hull.faces),
147            }),
148            ..Default::default()
149        }
150    }
151
152    pub fn with_trigger(mut self, is_trigger: bool) -> Self {
153        self.is_trigger = is_trigger;
154        self
155    }
156
157    pub fn with_material(mut self, material: PhysicsMaterial) -> Self {
158        self.material = material;
159        self
160    }
161
162    // Backwards compatibility wrappers
163    pub fn aabb(half_extents: Vec3) -> Self {
164        Self::box_collider(half_extents)
165    }
166
167    pub fn new_sphere(radius: f32) -> Self {
168        Self::sphere(radius)
169    }
170
171    pub fn new_aabb(x: f32, y: f32, z: f32) -> Self {
172        Self::box_collider(Vec3::new(x, y, z))
173    }
174
175    pub fn new_capsule(radius: f32, half_height: f32) -> Self {
176        Self::capsule(radius, half_height)
177    }
178
179    pub fn with_layer(mut self, layer: CollisionLayer) -> Self {
180        self.collision_layer = layer;
181        self
182    }
183
184    pub fn volume(&self) -> f32 {
185        match &self.shape {
186            ColliderShape::Sphere(s) => (4.0 / 3.0) * std::f32::consts::PI * s.radius.powi(3),
187            ColliderShape::Box(b) => 8.0 * b.half_extents.x * b.half_extents.y * b.half_extents.z,
188            ColliderShape::Capsule(c) => {
189                let cylinder_vol = std::f32::consts::PI * c.radius.powi(2) * (c.half_height * 2.0);
190                let sphere_vol = (4.0 / 3.0) * std::f32::consts::PI * c.radius.powi(3);
191                cylinder_vol + sphere_vol
192            }
193            ColliderShape::Plane(_) => f32::MAX, // Safe value instead of INFINITY for inertia calculations
194            ColliderShape::TriMesh(_)
195            | ColliderShape::ConvexHull(_)
196            | ColliderShape::Compound(_) => {
197                let aabb = self.compute_aabb(Vec3::ZERO, Quat::IDENTITY);
198                let e = aabb.max - aabb.min;
199                e.x * e.y * e.z * 0.5 // Approximate volume from AABB
200            }
201        }
202    }
203
204    pub fn extents_y(&self) -> f32 {
205        match &self.shape {
206            ColliderShape::Sphere(s) => s.radius,
207            ColliderShape::Box(b) => b.half_extents.y,
208            ColliderShape::Capsule(c) => c.half_height + c.radius,
209            ColliderShape::Plane(_) => 0.0,
210            ColliderShape::TriMesh(_)
211            | ColliderShape::ConvexHull(_)
212            | ColliderShape::Compound(_) => {
213                let aabb = self.compute_aabb(Vec3::ZERO, Quat::IDENTITY);
214                (aabb.max.y - aabb.min.y) * 0.5
215            }
216        }
217    }
218}
219
220#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
221pub enum ColliderShape {
222    Sphere(SphereShape),
223    Box(BoxShape),
224    Capsule(CapsuleShape),
225    Plane(PlaneShape),
226    TriMesh(TriMeshShape),
227    ConvexHull(ConvexHullShape),
228    Compound(Vec<(Transform, Box<ColliderShape>)>),
229}
230
231#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
232pub struct SphereShape {
233    pub radius: f32,
234}
235
236#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
237pub struct BoxShape {
238    pub half_extents: Vec3,
239}
240
241#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
242pub struct CapsuleShape {
243    pub radius: f32,
244    pub half_height: f32, // Height of cylindrical part (not including hemispheres)
245}
246
247#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
248pub struct PlaneShape {
249    pub normal: Vec3,
250    pub distance: f32,
251}
252
253#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
254#[serde(into = "TriMeshShapeData", from = "TriMeshShapeData")]
255pub struct TriMeshShape {
256    pub vertices: std::sync::Arc<Vec<Vec3>>,
257    pub indices: std::sync::Arc<Vec<u32>>,
258    #[serde(skip)]
259    pub bvh: std::sync::Arc<crate::bvh::BvhTree>,
260}
261
262#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
263struct TriMeshShapeData {
264    vertices: Vec<Vec3>,
265    indices: Vec<u32>,
266}
267
268impl From<TriMeshShapeData> for TriMeshShape {
269    fn from(mut data: TriMeshShapeData) -> Self {
270        let bvh = crate::bvh::BvhTree::build(&data.vertices, &mut data.indices).unwrap_or_default();
271        Self {
272            vertices: std::sync::Arc::new(data.vertices),
273            indices: std::sync::Arc::new(data.indices),
274            bvh: std::sync::Arc::new(bvh),
275        }
276    }
277}
278
279impl From<TriMeshShape> for TriMeshShapeData {
280    fn from(shape: TriMeshShape) -> Self {
281        Self {
282            vertices: (*shape.vertices).clone(),
283            indices: (*shape.indices).clone(),
284        }
285    }
286}
287
288#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
289#[serde(into = "ConvexHullShapeData", from = "ConvexHullShapeData")]
290pub struct ConvexHullShape {
291    pub vertices: std::sync::Arc<Vec<Vec3>>,
292    pub faces: std::sync::Arc<Vec<[u32; 3]>>,
293}
294
295#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
296struct ConvexHullShapeData {
297    points: Vec<Vec3>, // These are raw points, we rebuild the hull on load
298}
299
300impl From<ConvexHullShapeData> for ConvexHullShape {
301    fn from(data: ConvexHullShapeData) -> Self {
302        let hull = crate::quickhull::compute_convex_hull(&data.points);
303        Self {
304            vertices: std::sync::Arc::new(hull.vertices),
305            faces: std::sync::Arc::new(hull.faces),
306        }
307    }
308}
309
310impl From<ConvexHullShape> for ConvexHullShapeData {
311    fn from(shape: ConvexHullShape) -> Self {
312        Self {
313            points: (*shape.vertices).clone(),
314        }
315    }
316}
317
318gizmo_core::impl_component!(Collider);