Allow using polylines as a collider shape.

This commit is contained in:
Crozet Sébastien
2021-01-26 16:41:21 +01:00
parent e1f50eb6e8
commit 23a86c294e
10 changed files with 294 additions and 53 deletions

View File

@@ -12,12 +12,14 @@ use std::cmp::Ordering;
mod add_remove2;
mod collision_groups2;
mod convex_polygons2;
mod damping2;
mod debug_box_ball2;
mod heightfield2;
mod joints2;
mod locked_rotations2;
mod platform2;
mod polyline2;
mod pyramid2;
mod restitution2;
mod sensor2;
@@ -58,11 +60,13 @@ pub fn main() {
let mut builders: Vec<(_, fn(&mut Testbed))> = vec![
("Add remove", add_remove2::init_world),
("Collision groups", collision_groups2::init_world),
("Convex polygons", convex_polygons2::init_world),
("Damping", damping2::init_world),
("Heightfield", heightfield2::init_world),
("Joints", joints2::init_world),
("Locked rotations", locked_rotations2::init_world),
("Platform", platform2::init_world),
("Polyline", polyline2::init_world),
("Pyramid", pyramid2::init_world),
("Restitution", restitution2::init_world),
("Sensor", sensor2::init_world),

View File

@@ -0,0 +1,86 @@
use na::Point2;
use rand::distributions::{Distribution, Standard};
use rand::{rngs::StdRng, SeedableRng};
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 = 30.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 convex polygons
*/
let num = 14;
let scale = 4.0;
let border_rad = 0.0;
let shift = border_rad * 2.0 + scale;
let centerx = shift * (num as f32) / 2.0;
let centery = shift / 2.0;
let mut rng = StdRng::seed_from_u64(0);
let distribution = Standard;
for i in 0..num {
for j in 0usize..num * 4 {
let x = i as f32 * shift - centerx;
let y = j as f32 * shift * 2.0 + centery + 2.0;
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let handle = bodies.insert(rigid_body);
let mut points = Vec::new();
for _ in 0..10 {
let pt: Point2<f32> = distribution.sample(&mut rng);
points.push(pt * scale);
}
let collider = ColliderBuilder::convex_hull(&points).unwrap().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()
}

74
examples2d/polyline2.rs Normal file
View File

@@ -0,0 +1,74 @@
use na::{ComplexField, 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 = 50.0;
let nsubdivs = 2000;
let step_size = ground_size / (nsubdivs as f32);
let mut points = Vec::new();
points.push(Point2::new(-ground_size / 2.0, 40.0));
for i in 1..nsubdivs - 1 {
let x = -ground_size / 2.0 + i as f32 * step_size;
let y = ComplexField::cos(i as f32 * step_size) * 2.0;
points.push(Point2::new(x, y));
}
points.push(Point2::new(ground_size / 2.0, 40.0));
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::polyline(points, None).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the cubes
*/
let num = 20;
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 {
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).build();
colliders.insert(collider, handle, &mut bodies);
} else {
let collider = ColliderBuilder::ball(rad).build();
colliders.insert(collider, handle, &mut bodies);
}
}
}
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point2::new(0.0, 0.0), 10.0);
}
fn main() {
let testbed = Testbed::from_builders(0, vec![("Heightfield", init_world)]);
testbed.run()
}