First public release of Rapier.

This commit is contained in:
Sébastien Crozet
2020-08-25 22:10:25 +02:00
commit 754a48b7ff
175 changed files with 32819 additions and 0 deletions

27
examples2d/Cargo.toml Normal file
View File

@@ -0,0 +1,27 @@
[package]
name = "nphysics-examples-2d"
version = "0.1.0"
authors = [ "Sébastien Crozet <developer@crozet.re>" ]
edition = "2018"
[features]
parallel = [ "rapier2d/parallel", "rapier_testbed2d/parallel" ]
simd-stable = [ "rapier2d/simd-stable" ]
simd-nightly = [ "rapier2d/simd-nightly" ]
other-backends = [ "rapier_testbed2d/other-backends" ]
enhanced-determinism = [ "rapier2d/enhanced-determinism" ]
[dependencies]
rand = "0.7"
Inflector = "0.11"
nalgebra = "0.22"
[dependencies.rapier_testbed2d]
path = "../build/rapier_testbed2d"
[dependencies.rapier2d]
path = "../build/rapier2d"
[[bin]]
name = "all_examples2"
path = "./all_examples2.rs"

View File

@@ -0,0 +1,90 @@
#![allow(dead_code)]
extern crate nalgebra as na;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use inflector::Inflector;
use rapier_testbed2d::Testbed;
use std::cmp::Ordering;
mod balls2;
mod boxes2;
mod capsules2;
mod debug_box_ball2;
mod heightfield2;
mod joints2;
mod kinematic2;
mod pyramid2;
mod sensor2;
mod stress_joint_ball2;
mod stress_joint_fixed2;
mod stress_joint_prismatic2;
fn demo_name_from_command_line() -> Option<String> {
let mut args = std::env::args();
while let Some(arg) = args.next() {
if &arg[..] == "--example" {
return args.next();
}
}
None
}
#[cfg(any(target_arch = "wasm32", target_arch = "asmjs"))]
fn demo_name_from_url() -> Option<String> {
None
// let window = stdweb::web::window();
// let hash = window.location()?.search().ok()?;
// Some(hash[1..].to_string())
}
#[cfg(not(any(target_arch = "wasm32", target_arch = "asmjs")))]
fn demo_name_from_url() -> Option<String> {
None
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
let demo = demo_name_from_command_line()
.or_else(|| demo_name_from_url())
.unwrap_or(String::new())
.to_camel_case();
let mut builders: Vec<(_, fn(&mut Testbed))> = vec![
("Balls", balls2::init_world),
("Boxes", boxes2::init_world),
("Capsules", capsules2::init_world),
("Heightfield", heightfield2::init_world),
("Joints", joints2::init_world),
("Kinematic", kinematic2::init_world),
("Pyramid", pyramid2::init_world),
("Sensor", sensor2::init_world),
("(Debug) box ball", debug_box_ball2::init_world),
("(Stress test) joint ball", stress_joint_ball2::init_world),
("(Stress test) joint fixed", stress_joint_fixed2::init_world),
(
"(Stress test) joint prismatic",
stress_joint_prismatic2::init_world,
),
];
// Lexicographic sort, with stress tests moved at the end of the list.
builders.sort_by(|a, b| match (a.0.starts_with("("), b.0.starts_with("(")) {
(true, true) | (false, false) => a.0.cmp(b.0),
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
});
let i = builders
.iter()
.position(|builder| builder.0.to_camel_case().as_str() == demo.as_str())
.unwrap_or(0);
let testbed = Testbed::from_builders(i, builders);
testbed.run()
}

68
examples2d/balls2.rs Normal file
View File

@@ -0,0 +1,68 @@
use na::Point2;
use rapier2d::dynamics::{BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let _ground_size = 25.0;
/*
let ground_shape = ShapeHandle::new(Cuboid::new(Vector2::new(ground_size, 1.0)));
let ground_handle = bodies.insert(Ground::new());
let co = ColliderDesc::new(ground_shape)
.translation(-Vector2::y())
.build(BodyPartHandle(ground_handle, 0));
colliders.insert(co);
*/
/*
* Create the balls
*/
let num = 50;
let rad = 1.0;
let shiftx = rad * 2.5;
let shifty = rad * 2.0;
let centerx = shiftx * (num as f32) / 2.0;
let centery = shifty / 2.0;
for i in 0..num {
for j in 0usize..num * 5 {
let x = i as f32 * shiftx - centerx;
let y = j as f32 * shifty + centery;
let status = if j == 0 {
BodyStatus::Static
} else {
BodyStatus::Dynamic
};
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new(status).translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 2.5), 5.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]);
testbed.run()
}

73
examples2d/boxes2.rs Normal file
View File

@@ -0,0 +1,73 @@
use na::Point2;
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let ground_size = 25.0;
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
let rigid_body = RigidBodyBuilder::new_static()
.rotation(std::f32::consts::FRAC_PI_2)
.translation(ground_size, ground_size * 2.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
let rigid_body = RigidBodyBuilder::new_static()
.rotation(std::f32::consts::FRAC_PI_2)
.translation(-ground_size, ground_size * 2.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the cubes
*/
let num = 26;
let rad = 0.5;
let shift = rad * 2.0;
let centerx = shift * (num as f32) / 2.0;
let centery = shift / 2.0;
for i in 0..num {
for j in 0usize..num * 5 {
let x = i as f32 * shift - centerx;
let y = j as f32 * shift + centery + 2.0;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 50.0), 10.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]);
testbed.run()
}

76
examples2d/capsules2.rs Normal file
View File

@@ -0,0 +1,76 @@
use na::Point2;
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let ground_size = 25.0;
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
let rigid_body = RigidBodyBuilder::new_static()
.rotation(std::f32::consts::FRAC_PI_2)
.translation(ground_size, ground_size * 4.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size * 4.0, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
let rigid_body = RigidBodyBuilder::new_static()
.rotation(std::f32::consts::FRAC_PI_2)
.translation(-ground_size, ground_size * 4.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size * 4.0, 1.2).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the cubes
*/
let num = 26;
let rad = 0.5;
let shift = rad * 2.0;
let shifty = rad * 5.0;
let centerx = shift * (num as f32) / 2.0;
let centery = shift / 2.0;
for i in 0..num {
for j in 0usize..num * 5 {
let x = i as f32 * shift - centerx;
let y = j as f32 * shifty + centery + 3.0;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::capsule_y(rad * 1.5, rad)
.density(1.0)
.build();
colliders.insert(collider, handle, &mut bodies);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 50.0), 10.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]);
testbed.run()
}

View File

@@ -0,0 +1,44 @@
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let rad = 1.0;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -rad)
.rotation(std::f32::consts::PI / 4.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).build();
colliders.insert(collider, handle, &mut bodies);
// Build the dynamic box rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 3.0 * rad)
.can_sleep(false)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
// testbed.look_at(Point2::new(10.0, 10.0, 10.0), Point2::origin());
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]);
testbed.run()
}

View File

@@ -0,0 +1,72 @@
use na::{DVector, Point2, Vector2};
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let ground_size = Vector2::new(50.0, 1.0);
let nsubdivs = 2000;
let heights = DVector::from_fn(nsubdivs + 1, |i, _| {
if i == 0 || i == nsubdivs {
80.0
} else {
(i as f32 * ground_size.x / (nsubdivs as f32)).cos() * 2.0
}
});
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::heightfield(heights, ground_size).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the cubes
*/
let num = 26;
let rad = 0.5;
let shift = rad * 2.0;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
for i in 0..num {
for j in 0usize..num * 5 {
let x = i as f32 * shift - centerx;
let y = j as f32 * shift + centery + 3.0;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
if j % 2 == 0 {
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
} else {
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
}
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 50.0), 10.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Heightfield", init_world)]);
testbed.run()
}

75
examples2d/joints2.rs Normal file
View File

@@ -0,0 +1,75 @@
use na::Point2;
use rapier2d::dynamics::{BallJoint, BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let mut joints = JointSet::new();
/*
* Create the balls
*/
// Build the rigid body.
// NOTE: a smaller radius (e.g. 0.1) breaks Box2D so
// in order to be able to compare rapier with Box2D,
// we set it to 0.4.
let rad = 0.4;
let numi = 100; // Num vertical nodes.
let numk = 100; // Num horizontal nodes.
let shift = 1.0;
let mut body_handles = Vec::new();
for k in 0..numk {
for i in 0..numi {
let fk = k as f32;
let fi = i as f32;
let status = if i == 0 && (k % 4 == 0 || k == numk - 1) {
BodyStatus::Static
} else {
BodyStatus::Dynamic
};
let rigid_body = RigidBodyBuilder::new(status)
.translation(fk * shift, -fi * shift)
.build();
let child_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, child_handle, &mut bodies);
// Vertical joint.
if i > 0 {
let parent_handle = *body_handles.last().unwrap();
let joint = BallJoint::new(Point2::origin(), Point2::new(0.0, shift));
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
// Horizontal joint.
if k > 0 {
let parent_index = body_handles.len() - numi;
let parent_handle = body_handles[parent_index];
let joint = BallJoint::new(Point2::origin(), Point2::new(-shift, 0.0));
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
body_handles.push(child_handle);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(numk as f32 * rad, numi as f32 * -rad), 5.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Joints", init_world)]);
testbed.run()
}

93
examples2d/kinematic2.rs Normal file
View File

@@ -0,0 +1,93 @@
use na::Point2;
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground.
*/
let ground_size = 10.0;
let ground_height = 0.1;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the boxes
*/
let num = 6;
let rad = 0.2;
let shift = rad * 2.0;
let centerx = shift * num as f32 / 2.0;
let centery = shift / 2.0 + 3.04;
for i in 0usize..num {
for j in 0usize..num * 50 {
let x = i as f32 * shift - centerx;
let y = j as f32 * shift + centery;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
}
}
/*
* Setup a kinematic rigid body.
*/
let platform_body = RigidBodyBuilder::new_kinematic()
.translation(-10.0 * rad, 1.5 + 0.8)
.build();
let platform_handle = bodies.insert(platform_body);
let collider = ColliderBuilder::cuboid(rad * 10.0, rad)
.density(1.0)
.build();
colliders.insert(collider, platform_handle, &mut bodies);
/*
* Setup a callback to control the platform.
*/
testbed.add_callback(move |bodies, _, _, _, time| {
let mut platform = bodies.get_mut(platform_handle).unwrap();
let mut next_pos = platform.position;
let dt = 0.016;
next_pos.translation.vector.y += (time * 5.0).sin() * dt;
next_pos.translation.vector.x += time.sin() * 5.0 * dt;
if next_pos.translation.vector.x >= rad * 10.0 {
next_pos.translation.vector.x -= dt;
}
if next_pos.translation.vector.x <= -rad * 10.0 {
next_pos.translation.vector.x += dt;
}
platform.set_next_kinematic_position(next_pos);
});
/*
* Run the simulation.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 1.0), 40.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Kinematic body", init_world)]);
testbed.run()
}

60
examples2d/pyramid2.rs Normal file
View File

@@ -0,0 +1,60 @@
use na::Point2;
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let ground_size = 100.0;
let ground_thickness = 1.0;
let rigid_body = RigidBodyBuilder::new_static().build();
let ground_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_thickness).build();
colliders.insert(collider, ground_handle, &mut bodies);
/*
* Create the cubes
*/
let num = 100;
let rad = 0.5;
let shift = rad * 2.0;
let centerx = shift * (num as f32) / 2.0;
let centery = shift / 2.0 + ground_thickness + rad * 1.5;
for i in 0usize..num {
for j in i..num {
let fj = j as f32;
let fi = i as f32;
let x = (fi * shift / 2.0) + (fj - fi) * shift - centerx;
let y = fi * shift + centery;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 2.5), 5.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]);
testbed.run()
}

101
examples2d/sensor2.rs Normal file
View File

@@ -0,0 +1,101 @@
use na::{Point2, Point3};
use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet, Proximity};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground.
*/
let ground_size = 200.1;
let ground_height = 0.1;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height)
.build();
let ground_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height).build();
colliders.insert(collider, ground_handle, &mut bodies);
/*
* Create some boxes.
*/
let num = 10;
let rad = 0.2;
let shift = rad * 2.0;
let centerx = shift * num as f32 / 2.0;
for i in 0usize..num {
let x = i as f32 * shift - centerx;
let y = 3.0;
// Build the rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, handle, &mut bodies);
testbed.set_body_color(handle, Point3::new(0.5, 0.5, 1.0));
}
/*
* Create a cube that will have a ball-shaped sensor attached.
*/
// Rigid body so that the sensor can move.
let sensor = RigidBodyBuilder::new_dynamic()
.translation(0.0, 10.0)
.build();
let sensor_handle = bodies.insert(sensor);
// Solid cube attached to the sensor which
// other colliders can touch.
let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
colliders.insert(collider, sensor_handle, &mut bodies);
// We create a collider desc without density because we don't
// want it to contribute to the rigid body mass.
let sensor_collider = ColliderBuilder::ball(rad * 5.0).sensor(true).build();
colliders.insert(sensor_collider, sensor_handle, &mut bodies);
testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0));
// Callback that will be executed on the main loop to handle proximities.
testbed.add_callback(move |_, colliders, events, graphics, _| {
while let Ok(prox) = events.proximity_events.try_recv() {
let color = match prox.new_status {
Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0),
Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0),
};
let parent_handle1 = colliders.get(prox.collider1).unwrap().parent();
let parent_handle2 = colliders.get(prox.collider2).unwrap().parent();
if parent_handle1 != ground_handle && parent_handle1 != sensor_handle {
graphics.set_body_color(parent_handle1, color);
}
if parent_handle2 != ground_handle && parent_handle2 != sensor_handle {
graphics.set_body_color(parent_handle2, color);
}
}
});
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 1.0), 100.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Sensor", init_world)]);
testbed.run()
}

View File

@@ -0,0 +1,72 @@
use na::Point2;
use rapier2d::dynamics::{BallJoint, BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let mut joints = JointSet::new();
/*
* Create the balls
*/
// Build the rigid body.
let rad = 0.4;
let numi = 100; // Num vertical nodes.
let numk = 100; // Num horizontal nodes.
let shift = 1.0;
let mut body_handles = Vec::new();
for k in 0..numk {
for i in 0..numi {
let fk = k as f32;
let fi = i as f32;
let status = if k >= numk / 2 - 3 && k <= numk / 2 + 3 && i == 0 {
BodyStatus::Static
} else {
BodyStatus::Dynamic
};
let rigid_body = RigidBodyBuilder::new(status)
.translation(fk * shift, -fi * shift)
.build();
let child_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, child_handle, &mut bodies);
// Vertical joint.
if i > 0 {
let parent_handle = *body_handles.last().unwrap();
let joint = BallJoint::new(Point2::origin(), Point2::new(0.0, shift));
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
// Horizontal joint.
if k > 0 {
let parent_index = body_handles.len() - numi;
let parent_handle = body_handles[parent_index];
let joint = BallJoint::new(Point2::origin(), Point2::new(-shift, 0.0));
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
body_handles.push(child_handle);
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(numk as f32 * rad, numi as f32 * -rad), 5.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Joints", init_world)]);
testbed.run()
}

View File

@@ -0,0 +1,85 @@
use na::{Isometry2, Point2};
use rapier2d::dynamics::{BodyStatus, FixedJoint, JointSet, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let mut joints = JointSet::new();
/*
* Create the balls
*/
// Build the rigid body.
let rad = 0.4;
let num = 30; // Num vertical nodes.
let shift = 1.0;
let mut body_handles = Vec::new();
for xx in 0..4 {
let x = xx as f32 * shift * (num as f32 + 2.0);
for yy in 0..4 {
let y = yy as f32 * shift * (num as f32 + 4.0);
for k in 0..num {
for i in 0..num {
let fk = k as f32;
let fi = i as f32;
let status = if k == 0 {
BodyStatus::Static
} else {
BodyStatus::Dynamic
};
let rigid_body = RigidBodyBuilder::new(status)
.translation(x + fk * shift, y - fi * shift)
.build();
let child_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::ball(rad).density(1.0).build();
colliders.insert(collider, child_handle, &mut bodies);
// Vertical joint.
if i > 0 {
let parent_handle = *body_handles.last().unwrap();
let joint = FixedJoint::new(
Isometry2::identity(),
Isometry2::translation(0.0, shift),
);
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
// Horizontal joint.
if k > 0 {
let parent_index = body_handles.len() - num;
let parent_handle = body_handles[parent_index];
let joint = FixedJoint::new(
Isometry2::identity(),
Isometry2::translation(-shift, 0.0),
);
joints.insert(&mut bodies, parent_handle, child_handle, joint);
}
body_handles.push(child_handle);
}
}
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(50.0, 50.0), 5.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Joints", init_world)]);
testbed.run()
}

View File

@@ -0,0 +1,69 @@
use na::{Point2, Unit, Vector2};
use rapier2d::dynamics::{JointSet, PrismaticJoint, RigidBodyBuilder, RigidBodySet};
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed2d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
/*
* World
*/
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let mut joints = JointSet::new();
/*
* Create the balls
*/
// Build the rigid body.
let rad = 0.4;
let num = 10;
let shift = 1.0;
for l in 0..25 {
let y = l as f32 * shift * (num as f32 + 2.0) * 2.0;
for j in 0..50 {
let x = j as f32 * shift * 4.0;
let ground = RigidBodyBuilder::new_static().translation(x, y).build();
let mut curr_parent = bodies.insert(ground);
let collider = ColliderBuilder::cuboid(rad, rad).build();
colliders.insert(collider, curr_parent, &mut bodies);
for i in 0..num {
let y = y - (i + 1) as f32 * shift;
let density = 1.0;
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let curr_child = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).density(density).build();
colliders.insert(collider, curr_child, &mut bodies);
let axis = if i % 2 == 0 {
Unit::new_normalize(Vector2::new(1.0, 1.0))
} else {
Unit::new_normalize(Vector2::new(-1.0, 1.0))
};
let mut prism =
PrismaticJoint::new(Point2::origin(), axis, Point2::new(0.0, shift), axis);
prism.limits_enabled = true;
prism.limits[0] = -1.5;
prism.limits[1] = 1.5;
joints.insert(&mut bodies, curr_parent, curr_child, prism);
curr_parent = curr_child;
}
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(80.0, 80.0), 15.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Joints", init_world)]);
testbed.run()
}