aboutsummaryrefslogtreecommitdiff
path: root/examples3d
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2022-10-02 17:36:30 +0200
committerSébastien Crozet <developer@crozet.re>2022-10-02 17:36:30 +0200
commit36e85d0708e53a01731dfa95a9a2b4792ef03fe2 (patch)
tree3ab02f579d8cc603c0aca5b387a8b1368281320c /examples3d
parenta1802323285622e0626cd69c7ea3b3ca60638b2e (diff)
downloadrapier-36e85d0708e53a01731dfa95a9a2b4792ef03fe2.tar.gz
rapier-36e85d0708e53a01731dfa95a9a2b4792ef03fe2.tar.bz2
rapier-36e85d0708e53a01731dfa95a9a2b4792ef03fe2.zip
Add a character controller implementation
Diffstat (limited to 'examples3d')
-rw-r--r--examples3d/all_examples3.rs4
-rw-r--r--examples3d/character_controller3.rs157
-rw-r--r--examples3d/newton_cradle3.rs45
3 files changed, 206 insertions, 0 deletions
diff --git a/examples3d/all_examples3.rs b/examples3d/all_examples3.rs
index d7fb4e3..f599fdb 100644
--- a/examples3d/all_examples3.rs
+++ b/examples3d/all_examples3.rs
@@ -33,8 +33,10 @@ mod fountain3;
mod heightfield3;
mod joints3;
// mod joints3;
+mod character_controller3;
mod keva3;
mod locked_rotations3;
+mod newton_cradle3;
mod one_way_platforms3;
mod platform3;
mod primitives3;
@@ -79,6 +81,7 @@ pub fn main() {
.to_camel_case();
let mut builders: Vec<(_, fn(&mut Testbed))> = vec![
+ ("Character controller", character_controller3::init_world),
("Fountain", fountain3::init_world),
("Primitives", primitives3::init_world),
("Multibody joints", joints3::init_world_with_articulations),
@@ -98,6 +101,7 @@ pub fn main() {
("Sensor", sensor3::init_world),
("TriMesh", trimesh3::init_world),
("Keva tower", keva3::init_world),
+ ("Newton cradle", newton_cradle3::init_world),
("(Debug) multibody_joints", debug_articulations3::init_world),
(
"(Debug) add/rm collider",
diff --git a/examples3d/character_controller3.rs b/examples3d/character_controller3.rs
new file mode 100644
index 0000000..939cfd1
--- /dev/null
+++ b/examples3d/character_controller3.rs
@@ -0,0 +1,157 @@
+use rapier3d::prelude::*;
+use rapier_testbed3d::Testbed;
+
+pub fn init_world(testbed: &mut Testbed) {
+ /*
+ * World
+ */
+ let mut bodies = RigidBodySet::new();
+ let mut colliders = ColliderSet::new();
+ let mut impulse_joints = ImpulseJointSet::new();
+ let multibody_joints = MultibodyJointSet::new();
+
+ /*
+ * Ground
+ */
+ let ground_size = 5.0;
+ let ground_height = 0.1;
+
+ let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height, 0.0]);
+ let floor_handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size);
+ colliders.insert_with_parent(collider, floor_handle, &mut bodies);
+
+ /*
+ * Character we will control manually.
+ */
+ let rigid_body =
+ RigidBodyBuilder::kinematic_position_based().translation(vector![-3.0, 5.0, 0.0]);
+ let character_handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::capsule_y(0.3, 0.15); // 0.15, 0.3, 0.15);
+ colliders.insert_with_parent(collider, character_handle, &mut bodies);
+
+ /*
+ * Create the cubes
+ */
+ let num = 8;
+ let rad = 0.1;
+
+ let shift = rad * 2.0;
+ let centerx = shift * (num / 2) as f32;
+ let centery = rad;
+
+ for j in 0usize..4 {
+ for k in 0usize..4 {
+ for i in 0..num {
+ let x = i as f32 * shift - centerx;
+ let y = j as f32 * shift + centery;
+ let z = k as f32 * shift + centerx;
+
+ let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x, y, z]);
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(rad, rad, rad);
+ colliders.insert_with_parent(collider, handle, &mut bodies);
+ }
+ }
+ }
+
+ /*
+ * Create some stairs.
+ */
+ let stair_width = 1.0;
+ let stair_height = 0.1;
+ for i in 0..10 {
+ let x = i as f32 * stair_width / 2.0;
+ let y = i as f32 * stair_height * 1.5 + 3.0;
+
+ let collider = ColliderBuilder::cuboid(stair_width / 2.0, stair_height / 2.0, stair_width)
+ .translation(vector![x, y, 0.0]);
+ colliders.insert(collider);
+ }
+
+ /*
+ * Create a slope we can climb.
+ */
+ let slope_angle = 0.2;
+ let slope_size = 2.0;
+ let collider = ColliderBuilder::cuboid(slope_size, ground_height, slope_size)
+ .translation(vector![ground_size + slope_size, -ground_height + 0.4, 0.0])
+ .rotation(Vector::z() * slope_angle);
+ colliders.insert(collider);
+
+ /*
+ * Create a slope we can’t climb.
+ */
+ let impossible_slope_angle = 0.9;
+ let impossible_slope_size = 2.0;
+ let collider = ColliderBuilder::cuboid(slope_size, ground_height, ground_size)
+ .translation(vector![
+ ground_size + slope_size * 2.0 + impossible_slope_size - 0.9,
+ -ground_height + 2.3,
+ 0.0
+ ])
+ .rotation(Vector::z() * impossible_slope_angle);
+ colliders.insert(collider);
+
+ /*
+ * Create a moving platform.
+ */
+ let body = RigidBodyBuilder::kinematic_velocity_based().translation(vector![-8.0, 1.5, 0.0]);
+ // .rotation(-0.3);
+ let platform_handle = bodies.insert(body);
+ let collider = ColliderBuilder::cuboid(2.0, ground_height, 2.0);
+ colliders.insert_with_parent(collider, platform_handle, &mut bodies);
+
+ /*
+ * More complex ground.
+ */
+ let ground_size = Vector::new(10.0, 1.0, 10.0);
+ let nsubdivs = 20;
+
+ let heights = DMatrix::from_fn(nsubdivs + 1, nsubdivs + 1, |i, j| {
+ (i as f32 * ground_size.x / (nsubdivs as f32) / 2.0).cos()
+ + (j as f32 * ground_size.z / (nsubdivs as f32) / 2.0).cos()
+ });
+
+ let collider =
+ ColliderBuilder::heightfield(heights, ground_size).translation(vector![-8.0, 5.0, 0.0]);
+ colliders.insert(collider);
+
+ /*
+ * A tilting dynamic body with a limited joint.
+ */
+ let ground = RigidBodyBuilder::fixed().translation(vector![0.0, 5.0, 0.0]);
+ let ground_handle = bodies.insert(ground);
+ let body = RigidBodyBuilder::dynamic().translation(vector![0.0, 5.0, 0.0]);
+ let handle = bodies.insert(body);
+ let collider = ColliderBuilder::cuboid(1.0, 0.1, 2.0);
+ colliders.insert_with_parent(collider, handle, &mut bodies);
+ let joint = RevoluteJointBuilder::new(Vector::z_axis()).limits([-0.3, 0.3]);
+ impulse_joints.insert(ground_handle, handle, joint, true);
+
+ /*
+ * Setup a callback to move the platform.
+ */
+ testbed.add_callback(move |_, physics, _, run_state| {
+ let linvel = vector![
+ (run_state.time * 2.0).sin() * 2.0,
+ (run_state.time * 5.0).sin() * 1.5,
+ 0.0
+ ];
+ // let angvel = run_state.time.sin() * 0.5;
+
+ // Update the velocity-based kinematic body by setting its velocity.
+ if let Some(platform) = physics.bodies.get_mut(platform_handle) {
+ platform.set_linvel(linvel, true);
+ // NOTE: interaction with rotating platforms isn’t handled very well yet.
+ // platform.set_angvel(angvel, true);
+ }
+ });
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
+ testbed.set_character_body(character_handle);
+ testbed.look_at(point!(10.0, 10.0, 10.0), Point::origin());
+}
diff --git a/examples3d/newton_cradle3.rs b/examples3d/newton_cradle3.rs
new file mode 100644
index 0000000..b281149
--- /dev/null
+++ b/examples3d/newton_cradle3.rs
@@ -0,0 +1,45 @@
+use rapier3d::prelude::*;
+use rapier_testbed3d::Testbed;
+
+pub fn init_world(testbed: &mut Testbed) {
+ /*
+ * World
+ */
+ let mut bodies = RigidBodySet::new();
+ let mut colliders = ColliderSet::new();
+ let mut impulse_joints = ImpulseJointSet::new();
+ let multibody_joints = MultibodyJointSet::new();
+
+ let radius = 0.5;
+ let length = 10.0 * radius;
+ let rb = RigidBodyBuilder::dynamic();
+ let co = ColliderBuilder::ball(radius).restitution(1.0);
+
+ let n = 5;
+
+ for i in 0..n {
+ let (ball_pos, attach) = (
+ vector![i as Real * 2.2 * radius, 0.0, 0.0],
+ Vector::y() * length,
+ );
+ let vel = if i >= n - 1 {
+ vector![7.0, 0.0, 0.0]
+ } else {
+ Vector::zeros()
+ };
+
+ let ground = bodies.insert(RigidBodyBuilder::fixed().translation(ball_pos + attach));
+ let rb = rb.clone().translation(ball_pos).linvel(vel);
+ let handle = bodies.insert(rb);
+ colliders.insert_with_parent(co.clone(), handle, &mut bodies);
+
+ let joint = SphericalJointBuilder::new().local_anchor2(attach.into());
+ impulse_joints.insert(ground, handle, joint, true);
+ }
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
+ testbed.look_at(point![100.0, 100.0, 100.0], Point::origin());
+}