Skip to content

Commit

Permalink
Fix all 3D demos.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed May 6, 2018
1 parent 6be50d0 commit 93da7fa
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 128 deletions.
3 changes: 3 additions & 0 deletions examples2d/multibody2.rs
Expand Up @@ -32,6 +32,7 @@ fn main() {
let revo = RevoluteJoint::new(0.0);
let geom = ShapeHandle::new(Ball::new(rad));
let inertia = geom.inertia(1.0);
let center_of_mass = geom.center_of_mass();

for j in 0usize..num {
/*
Expand All @@ -43,6 +44,7 @@ fn main() {
na::zero(),
Vector2::new(-rad * 3.0, 0.0),
inertia,
center_of_mass,
);

/*
Expand All @@ -68,6 +70,7 @@ fn main() {
Vector2::new(-rad * 3.0, 0.0),
na::zero(),
inertia,
center_of_mass
);
world.add_collider(COLLIDER_MARGIN, geom.clone(), parent, Isometry2::identity());
}
Expand Down
138 changes: 86 additions & 52 deletions examples3d/collision_groups3.rs
Expand Up @@ -3,12 +3,16 @@ extern crate ncollide3d;
extern crate nphysics3d;
extern crate nphysics_testbed3d;

use na::{Point3, Vector3, Translation3};
use ncollide3d::shape::{Plane, Cuboid};
use na::{Isometry3, Point3, Vector3};
use ncollide3d::world::CollisionGroups;
use ncollide3d::shape::{Cuboid, ShapeHandle};
use nphysics3d::world::World;
use nphysics3d::object::{RigidBody, RigidBodyCollisionGroups};
use nphysics3d::object::{BodyHandle, Material};
use nphysics3d::volumetric::Volumetric;
use nphysics_testbed3d::Testbed;

const COLLIDER_MARGIN: f32 = 0.01;

fn main() {
let mut testbed = Testbed::new_empty();

Expand All @@ -22,86 +26,116 @@ fn main() {
* Setup groups.
*/
const GREEN_GROUP_ID: usize = 0;
let mut green_static_group = RigidBodyCollisionGroups::new_static();
green_static_group.set_membership(&[ GREEN_GROUP_ID ]);
green_static_group.set_whitelist(&[ GREEN_GROUP_ID ]);

let mut green_dynamic_group = RigidBodyCollisionGroups::new_dynamic();
green_dynamic_group.set_membership(&[ GREEN_GROUP_ID ]);
green_dynamic_group.set_whitelist(&[ GREEN_GROUP_ID ]);

let mut green_group = CollisionGroups::new();
green_group.set_membership(&[GREEN_GROUP_ID]);
green_group.set_whitelist(&[GREEN_GROUP_ID]);

const BLUE_GROUP_ID: usize = 1;
let mut blue_static_group = RigidBodyCollisionGroups::new_static();
blue_static_group.set_membership(&[ BLUE_GROUP_ID ]);
blue_static_group.set_whitelist(&[ BLUE_GROUP_ID ]);

let mut blue_dynamic_group = RigidBodyCollisionGroups::new_dynamic();
blue_dynamic_group.set_membership(&[ BLUE_GROUP_ID ]);
blue_dynamic_group.set_whitelist(&[ BLUE_GROUP_ID ]);
let mut blue_group = CollisionGroups::new();
blue_group.set_membership(&[BLUE_GROUP_ID]);
blue_group.set_whitelist(&[BLUE_GROUP_ID]);

/*
* A floor that will collide with everything (default behaviour).
*/
let geom = Plane::new(Vector3::new(0.0, 1.0, 0.0));
world.add_rigid_body(RigidBody::new_static(geom, 0.3, 0.6));
let ground_size = 5.0;
let ground_shape =
ShapeHandle::new(Cuboid::new(Vector3::repeat(ground_size - COLLIDER_MARGIN)));
let ground_pos = Isometry3::new(Vector3::y() * -ground_size, na::zero());

world.add_collider(
COLLIDER_MARGIN,
ground_shape,
BodyHandle::ground(),
ground_pos,
Material::default(),
);

/*
* A green floor that will collide with the GREEN group only.
*/
let geom = Cuboid::new(Vector3::new(10.0, 1.0, 10.0));
let mut rb = RigidBody::new_static(geom, 0.3, 0.6);
let geom = ShapeHandle::new(Cuboid::new(Vector3::new(1.0, 0.1, 1.0)));
let handle = world.add_collider(
COLLIDER_MARGIN,
geom,
BodyHandle::ground(),
Isometry3::new(Vector3::y() * 1.0, na::zero()),
Material::default(),
);

rb.set_collision_groups(green_static_group);
rb.append_translation(&Translation3::new(0.0, 10.0, 0.0));
world
.collision_world_mut()
.set_collision_groups(handle, green_group);

let handle = world.add_rigid_body(rb);
testbed.set_rigid_body_color(&handle, Point3::new(0.0, 1.0, 0.0));
testbed.set_collider_color(handle, Point3::new(0.0, 1.0, 0.0));

/*
* A blue floor that will collide with the BLUE group only.
*/
let geom = Cuboid::new(Vector3::new(10.0, 1.0, 10.0));
let mut rb = RigidBody::new_static(geom, 0.3, 0.6);
let geom = ShapeHandle::new(Cuboid::new(Vector3::new(1.0, 0.1, 1.0)));
let handle = world.add_collider(
COLLIDER_MARGIN,
geom,
BodyHandle::ground(),
Isometry3::new(Vector3::y() * 2.0, na::zero()),
Material::default(),
);

rb.set_collision_groups(blue_static_group);
rb.append_translation(&Translation3::new(0.0, 20.0, 0.0));
world
.collision_world_mut()
.set_collision_groups(handle, blue_group);

let handle = world.add_rigid_body(rb);
testbed.set_rigid_body_color(&handle, Point3::new(0.0, 0.0, 1.0));
testbed.set_collider_color(handle, Point3::new(0.0, 0.0, 1.0));

/*
* Create the boxes
*/
let num = 8;
let rad = 1.0;
let shift = rad * 2.0;
let num = 8;
let rad = 0.1;
let shift = rad * 2.0;
let centerx = shift * (num as f32) / 2.0;
let centery = 25.0;
let centery = 2.5;
let centerz = shift * (num as f32) / 2.0;

for k in 0usize .. 4 {
for i in 0usize .. num {
for j in 0usize .. num {
let geom = ShapeHandle::new(Cuboid::new(Vector3::repeat(rad - COLLIDER_MARGIN)));
let inertia = geom.inertia(1.0);
let center_of_mass = geom.center_of_mass();

for k in 0usize..4 {
for i in 0usize..num {
for j in 0usize..num {
let x = i as f32 * shift - centerx;
let z = j as f32 * shift - centerz;
let y = k as f32 * shift + centery;

let geom = Cuboid::new(Vector3::new(rad - 0.01, rad - 0.01, rad - 0.01));
let mut rb = RigidBody::new_dynamic(geom, 1.0, 0.3, 0.5);

rb.append_translation(&Translation3::new(x, y, z));
/*
* Create the rigid body.
*/
let pos = Isometry3::new(Vector3::new(x, y, z), na::zero());
let body_handle = world.add_rigid_body(pos, inertia, center_of_mass);

/*
* Create the collider.
*/
let collider_handle = world.add_collider(
COLLIDER_MARGIN,
geom.clone(),
body_handle,
Isometry3::identity(),
Material::default(),
);

// Alternate between the GREEN and BLUE groups.
if k % 2 == 0 {
rb.set_collision_groups(green_dynamic_group);
let handle = world.add_rigid_body(rb);
testbed.set_rigid_body_color(&handle, Point3::new(0.0, 1.0, 0.0));
}
else {
rb.set_collision_groups(blue_dynamic_group);
let handle = world.add_rigid_body(rb);
testbed.set_rigid_body_color(&handle, Point3::new(0.0, 0.0, 1.0));
world
.collision_world_mut()
.set_collision_groups(collider_handle, green_group);
testbed.set_body_color(&world, body_handle, Point3::new(0.0, 1.0, 0.0));
} else {
world
.collision_world_mut()
.set_collision_groups(collider_handle, blue_group);
testbed.set_body_color(&world, body_handle, Point3::new(0.0, 0.0, 1.0));
}
}
}
Expand All @@ -111,6 +145,6 @@ fn main() {
* Set up the testbed.
*/
testbed.set_world(world);
testbed.look_at(Point3::new(-40.0, 40.0, -40.0), Point3::new(0.0, 3.0, 0.0));
testbed.look_at(Point3::new(-4.0, 4.0, -4.0), Point3::new(0.0, 0.3, 0.0));
testbed.run();
}
43 changes: 30 additions & 13 deletions examples3d/fixed_bug_long_thin_box_one_shot_manifold3.rs
Expand Up @@ -25,12 +25,15 @@ extern crate ncollide3d;
extern crate nphysics3d;
extern crate nphysics_testbed3d;

use na::{Point3, Vector3, Translation3};
use ncollide3d::shape::{Plane, Cuboid};
use na::{Isometry3, Point3, Vector3};
use ncollide3d::shape::{Cuboid, Plane, ShapeHandle};
use nphysics3d::world::World;
use nphysics3d::object::RigidBody;
use nphysics3d::object::{BodyHandle, Material};
use nphysics3d::volumetric::Volumetric;
use nphysics_testbed3d::Testbed;

const COLLIDER_MARGIN: f32 = 0.01;

fn main() {
/*
* World
Expand All @@ -41,24 +44,38 @@ fn main() {
/*
* Plane
*/
let geom = Plane::new(Vector3::new(0.0, 1.0, 0.0));
let ground_shape = ShapeHandle::new(Plane::new(Vector3::y_axis()));

world.add_rigid_body(RigidBody::new_static(geom, 0.3, 0.6));
world.add_collider(
COLLIDER_MARGIN,
ground_shape,
BodyHandle::ground(),
Isometry3::identity(),
Material::default(),
);

/*
* Create the boxes
*/
let rad = 1.0;
let x = rad;
let y = rad + 10.0;
let z = rad;
let rad = 0.1;
let x = rad;
let y = rad + 10.0;
let z = rad;

let geom = Cuboid::new(Vector3::new(rad, rad * 10.0, rad));
let mut rb = RigidBody::new_dynamic(geom, 1.0, 0.3, 0.5);
let geom = ShapeHandle::new(Cuboid::new(Vector3::new(rad, rad * 10.0, rad)));
let inertia = geom.inertia(1.0);
let center_of_mass = geom.center_of_mass();

rb.append_translation(&Translation3::new(x, y, z));
let pos = Isometry3::new(Vector3::new(x, y, z), na::zero());
let handle = world.add_rigid_body(pos, inertia, center_of_mass);

world.add_rigid_body(rb);
world.add_collider(
COLLIDER_MARGIN,
geom.clone(),
handle,
Isometry3::identity(),
Material::default(),
);

/*
* Set up the testbed.
Expand Down

0 comments on commit 93da7fa

Please sign in to comment.