feat: add PD and PID controller implementations (#804)

* feat: add a PID controller implementation

* feat: add small rigid-body utilities + test interpolation test

* fix: make scrolling weaker on macos

* feat: add the option to use the PID controller in the character controller demo.

* feat: add a stateless PD controller

* feat(rapier_testbed): cleanup & support PidController in 2D too

* chore: add comments for the PD and PID controllers

* chore: update changelog

* feat: rename PidErrors to PdErrors which is more accurate

* fix cargo doc

* chore: remove dead code

* chore: make test module non-pub
This commit is contained in:
Sébastien Crozet
2025-03-05 14:06:49 +01:00
committed by GitHub
parent 955795dfbb
commit 108a2a18d6
19 changed files with 1275 additions and 242 deletions

View File

@@ -1,4 +1,8 @@
use rapier3d::{control::KinematicCharacterController, prelude::*};
use crate::utils::character::{self, CharacterControlMode};
use rapier3d::{
control::{KinematicCharacterController, PidController},
prelude::*,
};
use rapier_testbed3d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
@@ -40,8 +44,12 @@ pub fn init_world(testbed: &mut Testbed) {
/*
* Character we will control manually.
*/
let rigid_body =
RigidBodyBuilder::kinematic_position_based().translation(vector![0.0, 0.5, 0.0] * scale);
let rigid_body = RigidBodyBuilder::kinematic_position_based()
.translation(vector![0.0, 0.5, 0.0] * scale)
// The two config below makes the character
// nicer to control with the PID control enabled.
.gravity_scale(10.0)
.soft_ccd_prediction(10.0);
let character_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::capsule_y(0.3 * scale, 0.15 * scale); // 0.15, 0.3, 0.15);
colliders.insert_with_parent(collider, character_handle, &mut bodies);
@@ -124,7 +132,7 @@ pub fn init_world(testbed: &mut Testbed) {
* Create a moving platform.
*/
let body =
RigidBodyBuilder::kinematic_velocity_based().translation(vector![-8.0, 1.5, 0.0] * scale);
RigidBodyBuilder::kinematic_velocity_based().translation(vector![-8.0, 0.0, 0.0] * scale);
// .rotation(-0.3);
let platform_handle = bodies.insert(body);
let collider = ColliderBuilder::cuboid(2.0 * scale, ground_height * scale, 2.0 * scale);
@@ -177,15 +185,33 @@ pub fn init_world(testbed: &mut Testbed) {
});
/*
* Set up the testbed.
* Callback to update the character based on user inputs.
*/
testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
testbed.set_character_body(character_handle);
testbed.set_character_controller(Some(KinematicCharacterController {
let mut control_mode = CharacterControlMode::Kinematic;
let mut controller = KinematicCharacterController {
max_slope_climb_angle: impossible_slope_angle - 0.02,
min_slope_slide_angle: impossible_slope_angle - 0.02,
slide: true,
..Default::default()
}));
};
let mut pid = PidController::default();
testbed.add_callback(move |graphics, physics, _, _| {
if let Some(graphics) = graphics {
character::update_character(
graphics,
physics,
&mut control_mode,
&mut controller,
&mut pid,
character_handle,
);
}
});
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
testbed.look_at(point!(10.0, 10.0, 10.0), Point::origin());
}