tirbofish/dropbear
main / crates / eucalyptus-core / src / physics / kcc.rs · 8942 bytes
crates/eucalyptus-core/src/physics/kcc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Module that relates to the [Kinematic Character Controller](https://rapier.rs/docs/user_guides/rust/character_controller)
//! (or kcc for short) in the [rapier3d] physics engine.
use crate::component::{
Component, ComponentDescriptor, DisabilityFlags, InspectableComponent, SerializedComponent,
};
use crate::physics::PhysicsState;
use crate::states::Label;
use dropbear_engine::graphics::SharedGraphicsContext;
use egui::{ComboBox, DragValue, Ui};
use hecs::{Entity, World};
use rapier3d::control::{
CharacterAutostep, CharacterCollision, CharacterLength, KinematicCharacterController,
};
use rapier3d::na::{UnitVector3, Vector3};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use glam::Vec3;
/// The kinematic character controller (kcc) component.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct KCC {
pub entity: Label,
pub controller: KinematicCharacterController,
#[serde(skip)]
pub collisions: Vec<CharacterCollision>,
#[serde(skip)]
pub movement: Option<CharacterMovementResult>,
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CharacterMovementResult {
pub translation: Vec3,
pub grounded: bool,
pub is_sliding_down_slope: bool,
}
#[typetag::serde]
impl SerializedComponent for KCC {}
impl Component for KCC {
type SerializedForm = Self;
type RequiredComponentTypes = (Self,);
fn descriptor() -> ComponentDescriptor {
ComponentDescriptor {
disabled_flags: DisabilityFlags::Disabled,
internal: false,
fqtn: "eucalyptus_core::physics::kcc::KCC".to_string(),
type_name: "KinematicCharacterController".to_string(),
category: Some("Physics".to_string()),
description: Some("A kinematic character controller".to_string()),
}
}
fn init(
ser: &'_ Self::SerializedForm,
_: Arc<SharedGraphicsContext>,
) -> crate::component::ComponentInitFuture<'_, Self> {
Box::pin(async move { Ok((ser.clone(),)) })
}
fn update_component(
&mut self,
_world: &World,
_physics: &mut PhysicsState,
_entity: Entity,
_dt: f32,
_graphics: Arc<SharedGraphicsContext>,
) {
}
fn save(&self, _world: &World, _entity: Entity) -> Box<dyn SerializedComponent> {
Box::new(self.clone())
}
}
impl InspectableComponent for KCC {
fn inspect(
&mut self,
_world: &World,
entity: Entity,
ui: &mut Ui,
_graphics: Arc<SharedGraphicsContext>,
) {
egui::CollapsingHeader::new("Kinematic Character Controller")
.default_open(true)
.id_salt(format!(
"Kinematic Character Controller {}",
entity.to_bits()
))
.show(ui, |ui| {
fn edit_character_length(
ui: &mut Ui,
id_salt: impl std::hash::Hash,
value: &mut CharacterLength,
text: &str,
) {
ui.horizontal(|ui| {
ui.label(text);
let (mut kind, mut v) = match *value {
CharacterLength::Absolute(x) => (0, x),
CharacterLength::Relative(x) => (1, x),
};
ComboBox::from_id_salt(id_salt)
.selected_text(match kind {
0 => "Absolute",
_ => "Relative",
})
.show_ui(ui, |ui| {
ui.selectable_value(&mut kind, 0, "Absolute");
ui.selectable_value(&mut kind, 1, "Relative");
});
ui.add(DragValue::new(&mut v).speed(0.01));
*value = if kind == 0 {
CharacterLength::Absolute(v)
} else {
CharacterLength::Relative(v)
};
});
}
ui.vertical(|ui| {
ui.label("Up Vector:");
let up = self.controller.up;
let mut up_v = Vector3::new(up.x, up.y, up.z);
ui.horizontal(|ui| {
ui.label("X:");
ui.add(DragValue::new(&mut up_v.x).speed(0.01));
ui.label("Y:");
ui.add(DragValue::new(&mut up_v.y).speed(0.01));
ui.label("Z:");
ui.add(DragValue::new(&mut up_v.z).speed(0.01));
});
if up_v.norm_squared() > 0.0 {
self.controller.up = UnitVector3::new_normalize(up_v).into();
}
ui.add_space(8.0);
edit_character_length(ui, "kcc_offset", &mut self.controller.offset, "Offset:");
ui.add_space(8.0);
ui.separator();
ui.checkbox(&mut self.controller.slide, "Slide against floor?");
ui.label("Slope Angles (degrees):");
ui.horizontal(|ui| {
ui.label("Max climb:");
let mut deg = self.controller.max_slope_climb_angle.to_degrees();
if ui.add(DragValue::new(&mut deg).speed(1.0)).changed() {
self.controller.max_slope_climb_angle = deg.to_radians();
}
});
ui.horizontal(|ui| {
ui.label("Min slide:");
let mut deg = self.controller.min_slope_slide_angle.to_degrees();
if ui.add(DragValue::new(&mut deg).speed(1.0)).changed() {
self.controller.min_slope_slide_angle = deg.to_radians();
}
});
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.label("Normal nudge:");
ui.add(
egui::Slider::new(
&mut self.controller.normal_nudge_factor,
0.001..=0.5,
)
.logarithmic(true)
.smallest_positive(0.001)
.largest_finite(0.5)
.suffix(" m"),
);
});
ui.add_space(8.0);
ui.separator();
let mut enable_snap = self.controller.snap_to_ground.is_some();
ui.checkbox(&mut enable_snap, "Snap to ground");
if enable_snap && self.controller.snap_to_ground.is_none() {
self.controller.snap_to_ground = Some(CharacterLength::Absolute(0.2));
} else if !enable_snap {
self.controller.snap_to_ground = None;
}
if let Some(ref mut snap) = self.controller.snap_to_ground {
edit_character_length(ui, "kcc_snap_to_ground", snap, "Snap distance:");
}
ui.add_space(8.0);
ui.separator();
let mut enable_autostep = self.controller.autostep.is_some();
ui.checkbox(&mut enable_autostep, "Enable autostep");
if enable_autostep && self.controller.autostep.is_none() {
self.controller.autostep = Some(CharacterAutostep::default());
} else if !enable_autostep {
self.controller.autostep = None;
}
if let Some(step) = &mut self.controller.autostep {
edit_character_length(
ui,
"kcc_autostep_max_height",
&mut step.max_height,
"Max height:",
);
edit_character_length(
ui,
"kcc_autostep_min_width",
&mut step.min_width,
"Min width:",
);
ui.checkbox(&mut step.include_dynamic_bodies, "Include dynamic bodies");
}
});
});
}
}
impl KCC {
pub fn new(label: &Label) -> Self {
KCC {
entity: label.clone(),
controller: KinematicCharacterController::default(),
collisions: vec![],
movement: None,
}
}
}