khora_data/ecs/components/physics/
collider.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use khora_core::math::Vec3;
16use khora_core::physics::{ColliderHandle, ColliderShape};
17use khora_macros::Component;
18use serde::{Deserialize, Serialize};
19
20/// Component representing a collider attached to an entity.
21#[derive(Debug, Clone, Component, Serialize, Deserialize)]
22pub struct Collider {
23    /// Opaque handle used by the physics provider.
24    pub handle: Option<ColliderHandle>,
25    /// Shape of the collider.
26    pub shape: ColliderShape,
27    /// Friction coefficient.
28    pub friction: f32,
29    /// Restitution (bounciness) coefficient.
30    pub restitution: f32,
31    /// Whether this collider is a sensor (does not respond to forces).
32    pub is_sensor: bool,
33}
34
35impl Default for Collider {
36    fn default() -> Self {
37        Self {
38            handle: None,
39            shape: ColliderShape::Sphere(0.5),
40            friction: 0.5,
41            restitution: 0.0,
42            is_sensor: false,
43        }
44    }
45}
46
47impl Collider {
48    /// Creates a new box collider.
49    pub fn new_box(half_extents: Vec3) -> Self {
50        Self {
51            handle: None,
52            shape: ColliderShape::Box(half_extents),
53            friction: 0.5,
54            restitution: 0.0,
55            is_sensor: false,
56        }
57    }
58
59    /// Creates a new sphere collider.
60    pub fn new_sphere(radius: f32) -> Self {
61        Self {
62            handle: None,
63            shape: ColliderShape::Sphere(radius),
64            friction: 0.5,
65            restitution: 0.0,
66            is_sensor: false,
67        }
68    }
69}