khora_data/ecs/components/physics/
rigid_body.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::{BodyType, RigidBodyHandle};
17use khora_macros::Component;
18use serde::{Deserialize, Serialize};
19
20/// Component representing a rigid body in the physics simulation.
21#[derive(Debug, Clone, Component, Serialize, Deserialize)]
22pub struct RigidBody {
23    /// Opaque handle used by the physics provider.
24    pub handle: Option<RigidBodyHandle>,
25    /// Global type of the body (Static, Dynamic, Kinematic).
26    pub body_type: BodyType,
27    /// Mass of the body in kilograms.
28    pub mass: f32,
29    /// Whether to enable Continuous Collision Detection (CCD).
30    pub ccd_enabled: bool,
31    /// Current linear velocity.
32    pub linear_velocity: Vec3,
33    /// Current angular velocity.
34    pub angular_velocity: Vec3,
35}
36
37impl Default for RigidBody {
38    fn default() -> Self {
39        Self {
40            handle: None,
41            body_type: BodyType::Dynamic,
42            mass: 1.0,
43            ccd_enabled: false,
44            linear_velocity: Vec3::ZERO,
45            angular_velocity: Vec3::ZERO,
46        }
47    }
48}
49
50impl RigidBody {
51    /// Creates a new dynamic rigid body.
52    pub fn new_dynamic(mass: f32) -> Self {
53        Self {
54            handle: None,
55            body_type: BodyType::Dynamic,
56            mass,
57            ccd_enabled: false,
58            linear_velocity: Vec3::ZERO,
59            angular_velocity: Vec3::ZERO,
60        }
61    }
62
63    /// Creates a new static rigid body.
64    pub fn new_static() -> Self {
65        Self {
66            handle: None,
67            body_type: BodyType::Static,
68            mass: 0.0,
69            ccd_enabled: false,
70            linear_velocity: Vec3::ZERO,
71            angular_velocity: Vec3::ZERO,
72        }
73    }
74}