Add explicit wake_up parameter to method setting the position and velocity of a rigid-body.

This commit is contained in:
Crozet Sébastien
2020-11-19 18:09:55 +01:00
parent 49fd861083
commit 5ce3606582
14 changed files with 154 additions and 48 deletions

View File

@@ -24,7 +24,7 @@ pub fn init_world(testbed: &mut Testbed) {
let to_remove: Vec<_> = physics
.bodies
.iter()
.filter(|(_, b)| b.position.translation.vector.y < -10.0)
.filter(|(_, b)| b.position().translation.vector.y < -10.0)
.map(|e| e.0)
.collect();
for handle in to_remove {

View File

@@ -62,7 +62,7 @@ pub fn init_world(testbed: &mut Testbed) {
*/
testbed.add_callback(move |_, physics, _, _, time| {
let mut platform = physics.bodies.get_mut(platform_handle).unwrap();
let mut next_pos = platform.position;
let mut next_pos = *platform.position();
let dt = 0.016;
next_pos.translation.vector.y += (time * 5.0).sin() * dt;

View File

@@ -3,43 +3,82 @@ use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed3d::Testbed;
const MAX_NUMBER_OF_BODIES: usize = 400;
pub fn init_world(testbed: &mut Testbed) {
let bodies = RigidBodySet::new();
let colliders = ColliderSet::new();
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();
let joints = JointSet::new();
let rad = 0.5;
/*
* 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);
let mut k = 0;
// Callback that will be executed on the main loop to handle proximities.
testbed.add_callback(move |window, physics, _, graphics, _| {
k += 1;
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 10.0, 0.0)
.build();
let handle = physics.bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad, rad).build();
let collider = match k % 3 {
0 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(),
1 => ColliderBuilder::cone(rad, rad).build(),
_ => ColliderBuilder::cuboid(rad, rad, rad).build(),
};
physics
.colliders
.insert(collider, handle, &mut physics.bodies);
graphics.add(window, handle, &physics.bodies, &physics.colliders);
let to_remove: Vec<_> = physics
.bodies
.iter()
.filter(|(_, b)| b.position.translation.vector.y < -10.0)
.map(|e| e.0)
.collect();
for handle in to_remove {
physics
if physics.bodies.len() > MAX_NUMBER_OF_BODIES {
let mut to_remove: Vec<_> = physics
.bodies
.remove(handle, &mut physics.colliders, &mut physics.joints);
graphics.remove_body_nodes(window, handle);
.iter()
.filter(|e| e.1.is_dynamic())
.map(|e| (e.0, e.1.position().translation.vector))
.collect();
to_remove.sort_by(|a, b| {
(a.1.x.abs() + a.1.z.abs())
.partial_cmp(&(b.1.x.abs() + b.1.z.abs()))
.unwrap()
.reverse()
});
let num_to_remove = to_remove.len() - MAX_NUMBER_OF_BODIES;
for (handle, _) in &to_remove[..num_to_remove] {
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);
graphics.remove_body_nodes(window, *handle);
}
}
println!("Num bodies: {}", physics.bodies.len());
});
/*
* Set up the testbed.
*/
testbed.set_world(bodies, colliders, joints);
testbed.look_at(Point3::new(-30.0, -4.0, -30.0), Point3::new(0.0, 1.0, 0.0));
testbed.look_at(Point3::new(-30.0, 4.0, -30.0), Point3::new(0.0, 1.0, 0.0));
}
fn main() {

View File

@@ -12,7 +12,7 @@ pub fn init_world(testbed: &mut Testbed) {
let joints = JointSet::new();
/*
* Create the balls
* Create the cubes
*/
let num = 10;
let rad = 0.2;

View File

@@ -72,7 +72,7 @@ pub fn init_world(testbed: &mut Testbed) {
}
if let Some(mut platform) = physics.bodies.get_mut(platform_handle) {
let mut next_pos = platform.position;
let mut next_pos = *platform.position();
let dt = 0.016;
next_pos.translation.vector.y += (time * 5.0).sin() * dt;

View File

@@ -30,7 +30,7 @@ pub enum BodyStatus {
#[derive(Debug, Clone)]
pub struct RigidBody {
/// The world-space position of the rigid-body.
pub position: Isometry<f32>,
pub(crate) position: Isometry<f32>,
pub(crate) predicted_position: Isometry<f32>,
/// The local mass properties of the rigid-body.
pub mass_properties: MassProperties,
@@ -39,9 +39,9 @@ pub struct RigidBody {
/// The square-root of the inverse angular inertia tensor of the rigid-body.
pub world_inv_inertia_sqrt: AngularInertia<f32>,
/// The linear velocity of the rigid-body.
pub linvel: Vector<f32>,
pub(crate) linvel: Vector<f32>,
/// The angular velocity of the rigid-body.
pub angvel: AngVector<f32>,
pub(crate) angvel: AngVector<f32>,
/// Damping factor for gradually slowing down the translational motion of the rigid-body.
pub linear_damping: f32,
/// Damping factor for gradually slowing down the angular motion of the rigid-body.
@@ -231,18 +231,84 @@ impl RigidBody {
self.position = self.integrate_velocity(dt) * self.position;
}
/// The linear velocity of this rigid-body.
pub fn linvel(&self) -> &Vector<f32> {
&self.linvel
}
/// The angular velocity of this rigid-body.
#[cfg(feature = "dim2")]
pub fn angvel(&self) -> f32 {
self.angvel
}
/// The angular velocity of this rigid-body.
#[cfg(feature = "dim3")]
pub fn angvel(&self) -> &Vector<f32> {
&self.angvel
}
/// The linear velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
pub fn set_linvel(&mut self, linvel: Vector<f32>, wake_up: bool) {
self.linvel = linvel;
if self.is_dynamic() && wake_up {
self.wake_up(true)
}
}
/// The angular velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim2")]
pub fn set_angvel(&mut self, angvel: f32, wake_up: bool) {
self.angvel = angvel;
if self.is_dynamic() && wake_up {
self.wake_up(true)
}
}
/// The angular velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim3")]
pub fn set_angvel(&mut self, angvel: Vector<f32>, wake_up: bool) {
self.angvel = angvel;
if self.is_dynamic() && wake_up {
self.wake_up(true)
}
}
/// The world-space position of this rigid-body.
pub fn position(&self) -> &Isometry<f32> {
&self.position
}
/// Sets the position and `next_kinematic_position` of this rigid body.
///
/// This will teleport the rigid-body to the specified position/orientation,
/// completely ignoring any physics rule. If this body is kinematic, this will
/// also set the next kinematic position to the same value, effectively
/// resetting to zero the next interpolated velocity of the kinematic body.
pub fn set_position(&mut self, pos: Isometry<f32>) {
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
pub fn set_position(&mut self, pos: Isometry<f32>, wake_up: bool) {
self.position = pos;
// TODO: update the predicted position for dynamic bodies too?
if self.is_static() || self.is_kinematic() {
self.predicted_position = pos;
} else if wake_up {
// wake_up is true and the rigid-body is dynamic.
self.wake_up(true);
}
}
@@ -543,7 +609,7 @@ impl RigidBodyBuilder {
pub fn build(&self) -> RigidBody {
let mut rb = RigidBody::new();
rb.predicted_position = self.position; // FIXME: compute the correct value?
rb.set_position(self.position);
rb.set_position(self.position, false);
rb.linvel = self.linvel;
rb.angvel = self.angvel;
rb.body_status = self.body_status;

View File

@@ -250,7 +250,7 @@ impl ParallelIslandSolver {
let batch_size = thread.batch_size;
for handle in active_bodies[thread.position_writeback_index] {
let rb = &mut bodies[*handle];
rb.set_position(positions[rb.active_set_offset]);
rb.set_position(positions[rb.active_set_offset], false);
}
}
})

View File

@@ -120,7 +120,7 @@ impl PositionSolver {
}
bodies.foreach_active_island_body_mut_internal(island_id, |_, rb| {
rb.set_position(self.positions[rb.active_set_offset])
rb.set_position(self.positions[rb.active_set_offset], false)
});
}
}

View File

@@ -18,7 +18,7 @@ use {
/// Enum representing the type of a shape.
pub enum ShapeType {
/// A ball shape.
Ball = 1,
Ball = 0,
/// A convex polygon shape.
Polygon,
/// A cuboid shape.

View File

@@ -72,17 +72,18 @@ impl QueryPipeline {
let mut result = None;
for handle in inter {
let collider = &colliders[handle];
if collider.collision_groups.test(groups) {
if let Some(inter) = collider.shape().toi_and_normal_with_ray(
collider.position(),
ray,
max_toi,
true,
) {
if inter.toi < best {
best = inter.toi;
result = Some((handle, collider, inter));
if let Some(collider) = colliders.get(handle) {
if collider.collision_groups.test(groups) {
if let Some(inter) = collider.shape().toi_and_normal_with_ray(
collider.position(),
ray,
max_toi,
true,
) {
if inter.toi < best {
best = inter.toi;
result = Some((handle, collider, inter));
}
}
}
}

View File

@@ -71,10 +71,10 @@ impl Box2dWorld {
let def = b2::BodyDef {
body_type,
position: na_vec_to_b2_vec(body.position.translation.vector),
angle: body.position.rotation.angle(),
linear_velocity: na_vec_to_b2_vec(body.linvel),
angular_velocity: body.angvel,
position: na_vec_to_b2_vec(body.position().translation.vector),
angle: body.position().rotation.angle(),
linear_velocity: na_vec_to_b2_vec(*body.linvel()),
angular_velocity: body.angvel(),
linear_damping,
angular_damping,
..b2::BodyDef::new()
@@ -223,7 +223,7 @@ impl Box2dWorld {
if let Some(pb2_handle) = self.rapier2box2d.get(&handle) {
let b2_body = self.world.body(*pb2_handle);
let pos = b2_transform_to_na_isometry(b2_body.transform().clone());
body.set_position(pos);
body.set_position(pos, false);
for coll_handle in body.colliders() {
let collider = &mut colliders[*coll_handle];

View File

@@ -46,7 +46,7 @@ impl NPhysicsWorld {
for (rapier_handle, rb) in bodies.iter() {
// let material = physics.create_material(rb.collider.friction, rb.collider.friction, 0.0);
let nphysics_rb = RigidBodyDesc::new().position(rb.position).build();
let nphysics_rb = RigidBodyDesc::new().position(*rb.position()).build();
let nphysics_rb_handle = nphysics_bodies.insert(nphysics_rb);
rapier2nphysics.insert(rapier_handle, nphysics_rb_handle);
@@ -161,7 +161,7 @@ impl NPhysicsWorld {
let mut rb = bodies.get_mut(*rapier_handle).unwrap();
let ra = self.bodies.rigid_body(*nphysics_handle).unwrap();
let pos = *ra.position();
rb.set_position(pos);
rb.set_position(pos, false);
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];

View File

@@ -154,7 +154,7 @@ impl PhysxWorld {
use physx::rigid_static::RigidStatic;
use physx::transform;
let pos = transform::gl_to_px_tf(rb.position.to_homogeneous().into_glam());
let pos = transform::gl_to_px_tf(rb.position().to_homogeneous().into_glam());
if rb.is_dynamic() {
let actor = unsafe {
physx_sys::PxPhysics_createRigidDynamic_mut(physics.get_raw_mut(), &pos)
@@ -406,7 +406,7 @@ impl PhysxWorld {
let ra = self.scene.get_rigid_actor(*physx_handle).unwrap();
let pos = ra.get_global_pose().into_na();
let iso = na::convert_unchecked(pos);
rb.set_position(iso);
rb.set_position(iso, false);
if rb.is_kinematic() {}

View File

@@ -414,7 +414,7 @@ impl Testbed {
{
if self.state.selected_backend == BOX2D_BACKEND {
self.box2d = Some(Box2dWorld::from_rapier(
self.gravity,
self.physics.gravity,
&self.physics.bodies,
&self.physics.colliders,
&self.physics.joints,
@@ -647,7 +647,7 @@ impl Testbed {
if self.state.selected_backend == BOX2D_BACKEND {
self.box2d.as_mut().unwrap().step(
&mut self.physics.pipeline.counters,
&self.integration_parameters,
&self.physics.integration_parameters,
);
self.box2d.as_mut().unwrap().sync(
&mut self.physics.bodies,