khora_data/ecs/components/physics/
kinematic_character_controller.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_macros::Component;
17use serde::{Deserialize, Serialize};
18
19/// Component for kinematic character movement.
20/// It provides high-level movement resolution (slopes, steps, etc.)
21/// that is more suitable for player characters than raw rigid-body physics.
22#[derive(Debug, Clone, Component, Serialize, Deserialize)]
23pub struct KinematicCharacterController {
24    /// The translation to apply in the current frame.
25    pub desired_translation: Vec3,
26    /// The offset from obstacles to maintain.
27    pub offset: f32,
28    /// Maximum angle for climbing slopes (in radians).
29    pub max_slope_climb_angle: f32,
30    /// Minimum angle for sliding down a slope (in radians).
31    pub min_slope_slide_angle: f32,
32    /// Maximum height for autostepping.
33    pub autostep_height: f32,
34    /// Minimum width for autostepping obstacles.
35    pub autostep_min_width: f32,
36    /// Whether autostepping is enabled.
37    pub autostep_enabled: bool,
38    /// Whether the character is currently grounded.
39    pub is_grounded: bool,
40}
41
42impl Default for KinematicCharacterController {
43    fn default() -> Self {
44        Self {
45            desired_translation: Vec3::ZERO,
46            offset: 0.01,
47            max_slope_climb_angle: 0.785, // 45 degrees
48            min_slope_slide_angle: 0.523, // 30 degrees
49            autostep_height: 0.3,
50            autostep_min_width: 0.2,
51            autostep_enabled: true,
52            is_grounded: false,
53        }
54    }
55}