aboutsummaryrefslogtreecommitdiff
path: root/examples3d/spring_joints3.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2024-01-22 21:45:40 +0100
committerGitHub <noreply@github.com>2024-01-22 21:45:40 +0100
commitaef85ec2554476485dbf3de5f01257ced22bfe2f (patch)
tree0fbfae9a523835079c9a362a93a69f2e78ccca25 /examples3d/spring_joints3.rs
parent9ac3503b879f95fcdf5414470ba5aedf195b9a97 (diff)
parent6cb727390a6172e539b3f0ef91c2861457495258 (diff)
downloadrapier-aef85ec2554476485dbf3de5f01257ced22bfe2f.tar.gz
rapier-aef85ec2554476485dbf3de5f01257ced22bfe2f.tar.bz2
rapier-aef85ec2554476485dbf3de5f01257ced22bfe2f.zip
Merge pull request #579 from dimforge/joints-improvements
Feat: implement a "small-steps" velocity-based constraints solver + joint improvements
Diffstat (limited to 'examples3d/spring_joints3.rs')
-rw-r--r--examples3d/spring_joints3.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples3d/spring_joints3.rs b/examples3d/spring_joints3.rs
new file mode 100644
index 0000000..3336402
--- /dev/null
+++ b/examples3d/spring_joints3.rs
@@ -0,0 +1,64 @@
+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();
+
+ /*
+ * Fixed ground to attach one end of the joints.
+ */
+ let rigid_body = RigidBodyBuilder::fixed();
+ let ground_handle = bodies.insert(rigid_body);
+
+ /*
+ * Spring joints with a variety of spring parameters.
+ * The middle one has uses critical damping.
+ */
+ let num = 30;
+ let radius = 0.5;
+ let mass = Ball::new(radius).mass_properties(1.0).mass();
+ let stiffness = 1.0e3;
+ let critical_damping = 2.0 * (stiffness * mass).sqrt();
+ for i in 0..=num {
+ let x_pos = -6.0 + 1.5 * i as f32;
+ let ball_pos = point![x_pos, 4.5, 0.0];
+ let rigid_body = RigidBodyBuilder::dynamic()
+ .translation(ball_pos.coords)
+ .can_sleep(false);
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::ball(radius);
+ colliders.insert_with_parent(collider, handle, &mut bodies);
+
+ let damping_ratio = i as f32 / (num as f32 / 2.0);
+ let damping = damping_ratio * critical_damping;
+ let joint = SpringJointBuilder::new(0.0, stiffness, damping)
+ .local_anchor1(ball_pos - Vector::y() * 3.0);
+ impulse_joints.insert(ground_handle, handle, joint, true);
+
+ // Box that will fall on to of the springed balls, makes the simulation funier to watch.
+ let rigid_body =
+ RigidBodyBuilder::dynamic().translation(ball_pos.coords + Vector::y() * 5.0);
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(radius, radius, radius).density(100.0);
+ colliders.insert_with_parent(collider, handle, &mut bodies);
+ }
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world_with_params(
+ bodies,
+ colliders,
+ impulse_joints,
+ multibody_joints,
+ vector![0.0, -9.81, 0.0],
+ (),
+ );
+ testbed.look_at(point![15.0, 5.0, 42.0], point![13.0, 1.0, 1.0]);
+}