Skip to main content

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)]
23#[component(domain = Physics)]
24pub struct KinematicCharacterController {
25    /// The translation to apply in the current frame.
26    pub desired_translation: Vec3,
27    /// The offset from obstacles to maintain.
28    pub offset: f32,
29    /// Maximum angle for climbing slopes (in radians).
30    pub max_slope_climb_angle: f32,
31    /// Minimum angle for sliding down a slope (in radians).
32    pub min_slope_slide_angle: f32,
33    /// Maximum height for autostepping.
34    pub autostep_height: f32,
35    /// Minimum width for autostepping obstacles.
36    pub autostep_min_width: f32,
37    /// Whether autostepping is enabled.
38    pub autostep_enabled: bool,
39    /// Whether the character is currently grounded.
40    pub is_grounded: bool,
41}
42
43impl Default for KinematicCharacterController {
44    fn default() -> Self {
45        Self {
46            desired_translation: Vec3::ZERO,
47            offset: 0.01,
48            max_slope_climb_angle: 0.785, // 45 degrees
49            min_slope_slide_angle: 0.523, // 30 degrees
50            autostep_height: 0.3,
51            autostep_min_width: 0.2,
52            autostep_enabled: true,
53            is_grounded: false,
54        }
55    }
56}