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