Fix the last few bugs and unbounded memory usage.

This commit is contained in:
Crozet Sébastien
2021-03-17 09:34:56 +01:00
parent d82a675b46
commit 326469a1df
10 changed files with 211 additions and 135 deletions

View File

@@ -1,6 +1,6 @@
use na::Point3;
use na::{Point3, Vector3};
use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
use rapier3d::geometry::{ColliderBuilder, ColliderSet, HalfSpace, SharedShape};
use rapier_testbed3d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
@@ -14,30 +14,27 @@ pub fn init_world(testbed: &mut Testbed) {
/*
* Ground
*/
let ground_size = 100.0;
let ground_height = 0.1;
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size)
.friction(1.5)
.build();
let halfspace = SharedShape::new(HalfSpace::new(Vector3::y_axis()));
let collider = ColliderBuilder::new(halfspace).build();
colliders.insert(collider, handle, &mut bodies);
let mut curr_y = 0.0;
let mut curr_width = 1_000.0;
let mut curr_width = 10_000.0;
for _ in 0..6 {
curr_y += curr_width;
for _ in 0..12 {
let curr_height = 0.1f32.min(curr_width);
curr_y += curr_height * 4.0;
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, curr_y, 0.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(curr_width, curr_width, curr_width).build();
let collider = ColliderBuilder::cuboid(curr_width, curr_height, curr_width).build();
colliders.insert(collider, handle, &mut bodies);
curr_width /= 10.0;
curr_width /= 5.0;
}
/*

View File

@@ -1,3 +1,4 @@
use na::Point3;
use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed3d::Testbed;
@@ -17,7 +18,7 @@ pub fn init_world(testbed: &mut Testbed) {
let ground_height = 2.1;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height, 0.0)
.translation(0.0, 4.0, 0.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size).build();
@@ -26,7 +27,7 @@ pub fn init_world(testbed: &mut Testbed) {
let rad = 1.0;
// Build the dynamic box rigid body.
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 3.0 * rad, 0.0)
.translation(0.0, 7.0 * rad, 0.0)
.can_sleep(false)
.build();
let handle = bodies.insert(rigid_body);
@@ -34,7 +35,7 @@ pub fn init_world(testbed: &mut Testbed) {
colliders.insert(collider, handle, &mut bodies);
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 5.0 * rad, 0.0)
.translation(0.0, 2.0 * rad, 0.0)
.can_sleep(false)
.build();
let handle = bodies.insert(rigid_body);
@@ -44,6 +45,7 @@ pub fn init_world(testbed: &mut Testbed) {
/*
* Set up the testbed.
*/
testbed.look_at(Point3::new(100.0, -10.0, 100.0), Point3::origin());
testbed.set_world(bodies, colliders, joints);
}

View File

@@ -15,7 +15,7 @@ pub fn init_world(testbed: &mut Testbed) {
* Ground
*/
let ground_size = 100.1;
let ground_height = 2.1;
let ground_height = 2.1; // 16.0;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height, 0.0)
@@ -64,10 +64,6 @@ pub fn init_world(testbed: &mut Testbed) {
physics
.bodies
.remove(*handle, &mut physics.colliders, &mut physics.joints);
physics.broad_phase.maintain(&mut physics.colliders);
physics
.narrow_phase
.maintain(&mut physics.colliders, &mut physics.bodies);
if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) {
graphics.remove_body_nodes(window, *handle);
@@ -80,10 +76,10 @@ pub fn init_world(testbed: &mut Testbed) {
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed
.physics_state_mut()
.integration_parameters
.velocity_based_erp = 0.2;
// testbed
// .physics_state_mut()
// .integration_parameters
// .velocity_based_erp = 0.2;
testbed.look_at(Point3::new(-30.0, 4.0, -30.0), Point3::new(0.0, 1.0, 0.0));
}

View File

@@ -11,6 +11,19 @@ pub fn init_world(testbed: &mut Testbed) {
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
/*
* Ground
*/
let ground_size = 100.1;
let ground_height = 2.1;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height, 0.0)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size).build();
colliders.insert(collider, handle, &mut bodies);
/*
* Create the cubes
*/
@@ -26,11 +39,11 @@ pub fn init_world(testbed: &mut Testbed) {
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
for j in 0usize..1 {
for i in 0..1 {
for k in 0usize..1 {
for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shiftx - centerx + offset;
let y = j as f32 * shifty + centery - rad;
let y = j as f32 * shifty + centery + 3.0;
let z = k as f32 * shiftz - centerz + offset;
// Build the rigid body.
@@ -54,37 +67,10 @@ pub fn init_world(testbed: &mut Testbed) {
offset -= 0.05 * rad * (num as f32 - 1.0);
}
/*
* Ground
*/
testbed.add_callback(
move |mut window, mut graphics, physics, events, run_state| {
if run_state.timestep_id == 10 {
let ground_size = 100.1;
let ground_height = 2.1;
let rigid_body = RigidBodyBuilder::new_static()
.translation(0.0, -ground_height, 0.0)
.build();
let handle = physics.bodies.insert(rigid_body);
let collider =
ColliderBuilder::cuboid(ground_size, ground_height, ground_size).build();
physics
.colliders
.insert(collider, handle, &mut physics.bodies);
if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) {
graphics.add(window, handle, &physics.bodies, &physics.colliders);
}
}
},
);
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.physics_state_mut().gravity.fill(0.0);
testbed.look_at(Point3::new(100.0, 100.0, 100.0), Point3::origin());
}