From b13d78a6941b5ee461e446d701b09f68b68a614f Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Thu, 24 Jun 2021 17:42:04 +0200 Subject: [PATCH 1/2] Upgrade to Rapier master branch (0.9.2) --- Cargo.lock | 135 ++++++++++++++++++----------------- rapier3d/Cargo.toml | 2 +- rapier3d/src/area.rs | 8 +-- rapier3d/src/body.rs | 48 +++++++------ rapier3d/src/server/body.rs | 18 ++--- rapier3d/src/server/mod.rs | 12 ++-- rapier3d/src/server/shape.rs | 10 +-- rapier3d/src/space.rs | 89 ++++++++++++++--------- 8 files changed, 179 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6238b3..800525f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ahash" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f200cbb1e856866d9eade941cf3aa0c5d7dd36f74311c4273b494f4ef036957" +checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98" dependencies = [ "getrandom", "once_cell", @@ -22,11 +22,20 @@ dependencies = [ "num-traits", ] +[[package]] +name = "approx" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "072df7202e63b127ab55acfe16ce97013d5b97bf160489336d3f1840fd78e99e" +dependencies = [ + "num-traits", +] + [[package]] name = "arrayvec" -version = "0.6.1" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "269d0f5e68353a7cab87f81e7c736adc008d279a36ebc6a05dfe01193a89f0c9" +checksum = "be4dc07131ffa69b8072d35f5007352af944213cde02545e2103680baed38fcd" [[package]] name = "autocfg" @@ -73,9 +82,9 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "bumpalo" -version = "3.6.1" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" +checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" [[package]] name = "cexpr" @@ -117,9 +126,9 @@ checksum = "f92cfa0fd5690b3cf8c1ef2cabbd9b7ef22fa53cf5e1f92b05103f6d5d1cf6e7" [[package]] name = "crossbeam" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd01a6eb3daaafa260f6fc94c3a6c36390abc2080e38e3e34ced87393fb77d80" +checksum = "4ae5588f6b3c3cb05239e90bd110f257254aecd01e4635400391aeae07497845" dependencies = [ "cfg-if 1.0.0", "crossbeam-channel", @@ -152,9 +161,9 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52fb27eab85b17fbb9f6fd667089e07d6a2eb8743d02639ee7f6a7a7729c9c94" +checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" dependencies = [ "cfg-if 1.0.0", "crossbeam-utils", @@ -165,9 +174,9 @@ dependencies = [ [[package]] name = "crossbeam-queue" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f6cb3c7f5b8e51bc3ebb73a2327ad4abdbd119dc13223f14f961d2f38486756" +checksum = "9b10ddc024425c88c2ad148c1b0fd53f4c6d38db9697c9f1588381212fa657c9" dependencies = [ "cfg-if 1.0.0", "crossbeam-utils", @@ -175,11 +184,10 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" +checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" dependencies = [ - "autocfg", "cfg-if 1.0.0", "lazy_static", ] @@ -204,9 +212,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "euclid" -version = "0.22.4" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b9b0b0868386d989090b2431c4c2a4d31228490a132ca5db3a36184331782d3" +checksum = "da96828553a086d7b18dcebfc579bd9628b016f86590d7453c115e490fa74b80" dependencies = [ "num-traits", ] @@ -243,7 +251,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d26a9076a40e568e6a29180985a111290e060f33581f37cb7695b1187d3bb7" dependencies = [ "ahash", - "approx", + "approx 0.4.0", "bitflags", "euclid", "gdnative-impl-proc-macros", @@ -303,16 +311,6 @@ dependencies = [ "unindent", ] -[[package]] -name = "generic-array" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.3" @@ -358,9 +356,9 @@ checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" [[package]] name = "heck" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" dependencies = [ "unicode-segmentation", ] @@ -411,9 +409,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.94" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" +checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" [[package]] name = "libloading" @@ -466,9 +464,9 @@ checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" [[package]] name = "memoffset" -version = "0.6.3" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" dependencies = [ "autocfg", ] @@ -497,21 +495,31 @@ dependencies = [ [[package]] name = "nalgebra" -version = "0.25.4" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c70c9e8c5f213c8e93fc8c112ade4edd3ee62062fb897776c23dcebac7932900" +checksum = "462fffe4002f4f2e1f6a9dcf12cc1a6fc0e15989014efc02a941d3e0f5dc2120" dependencies = [ - "approx", - "generic-array", + "approx 0.5.0", "matrixmultiply", + "nalgebra-macros", "num-complex", "num-rational", "num-traits", - "serde", "simba", "typenum", ] +[[package]] +name = "nalgebra-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "nom" version = "5.1.2" @@ -524,9 +532,9 @@ dependencies = [ [[package]] name = "num-complex" -version = "0.3.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" +checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085" dependencies = [ "num-traits", ] @@ -554,9 +562,9 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" dependencies = [ "autocfg", "num-integer", @@ -574,9 +582,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.7.2" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" [[package]] name = "packed_simd_2" @@ -615,11 +623,11 @@ dependencies = [ [[package]] name = "parry3d" -version = "0.3.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35bcebccb9e10a94a78f159e686bcdcd2c058c250575da67f7e7e7c9eabdf3eb" +checksum = "70409213da33840548c12f25c6eb31e2d5af8156eae94472c105b5c4d09bd1e1" dependencies = [ - "approx", + "approx 0.5.0", "bitflags", "downcast-rs", "either", @@ -670,10 +678,10 @@ dependencies = [ [[package]] name = "rapier3d" -version = "0.7.2" -source = "git+https://github.com/Demindiro/rapier?branch=godot#34b5289dd82fb8f76da790ad9843d82f7d9c1374" +version = "0.9.2" +source = "git+https://github.com/Demindiro/rapier?branch=godot-0.9#a9485e13346620d73511f84deae8a5a65624c055" dependencies = [ - "approx", + "approx 0.5.0", "arrayvec", "bit-vec", "bitflags", @@ -697,9 +705,9 @@ checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" dependencies = [ "bitflags", ] @@ -775,9 +783,6 @@ name = "serde" version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" -dependencies = [ - "serde_derive", -] [[package]] name = "serde_derive" @@ -815,11 +820,11 @@ checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" [[package]] name = "simba" -version = "0.4.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5132a955559188f3d13c9ba831e77c802ddc8782783f050ed0c52f5988b95f4c" +checksum = "8e82063457853d00243beda9952e910b82593e4b07ae9f721b9278a99a0d3d5c" dependencies = [ - "approx", + "approx 0.5.0", "num-complex", "num-traits", "packed_simd_2", @@ -898,9 +903,9 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" [[package]] name = "syn" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" dependencies = [ "proc-macro2", "quote", @@ -909,9 +914,9 @@ dependencies = [ [[package]] name = "time" -version = "0.2.26" +version = "0.2.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a8cbfbf47955132d0202d1662f49b2423ae35862aee471f3ba4b133358f372" +checksum = "4752a97f8eebd6854ff91f1c1824cd6160626ac4bd44287f7f4ea2035a02a242" dependencies = [ "const_fn", "libc", @@ -934,9 +939,9 @@ dependencies = [ [[package]] name = "time-macros-impl" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5c3be1edfad6027c69f5491cf4cb310d1a71ecd6af742788c6ff8bced86b8fa" +checksum = "fd3c141a1b43194f3f56a1411225df8646c55781d5f26db825b3d98507eb482f" dependencies = [ "proc-macro-hack", "proc-macro2", diff --git a/rapier3d/Cargo.toml b/rapier3d/Cargo.toml index 77b3999..1c974c6 100644 --- a/rapier3d/Cargo.toml +++ b/rapier3d/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" name = "godot_rapier3d" [dependencies] -rapier3d = { git = "https://github.com/Demindiro/rapier", branch = "godot", version = "*", features = ["simd-nightly"] } +rapier3d = { git = "https://github.com/Demindiro/rapier", branch = "godot-0.9", version = "*", features = ["simd-nightly"] } gdnative = "*" lazy_static = "*" diff --git a/rapier3d/src/area.rs b/rapier3d/src/area.rs index 7590fc5..9978c23 100644 --- a/rapier3d/src/area.rs +++ b/rapier3d/src/area.rs @@ -307,7 +307,7 @@ impl Area { /// Sets the space of this area. This removes the area from it's current space, if any. pub fn set_space(&mut self, space: Option) { // Judging by the API areas are intended to be moved around, so use kinematic - let instance = Instance::Loose(RigidBodyBuilder::new_kinematic().build()); + let instance = Instance::Loose(RigidBodyBuilder::new_kinematic_position_based().build()); let instance = mem::replace(&mut self.instance, instance); let body = match instance { Instance::Attached((body, _), space) => space @@ -493,12 +493,12 @@ impl Area { /// Sets the collision layer of this area pub fn set_layer(&mut self, layer: u32) { - self.interaction_groups = self.interaction_groups.with_groups(layer); + self.interaction_groups = self.interaction_groups.with_memberships(layer); } - /// Sets the collision masj of this area + /// Sets the collision mask of this area pub fn set_mask(&mut self, mask: u32) { - self.interaction_groups = self.interaction_groups.with_mask(mask); + self.interaction_groups = self.interaction_groups.with_filter(mask); } /// Stores the area's index in the given [`Collider`] diff --git a/rapier3d/src/body.rs b/rapier3d/src/body.rs index afa0d78..986ce82 100644 --- a/rapier3d/src/body.rs +++ b/rapier3d/src/body.rs @@ -6,9 +6,7 @@ use crate::space::Space; use crate::util::*; use core::convert::{TryFrom, TryInto}; use gdnative::core_types::*; -use rapier3d::dynamics::{ - Axis, BodyStatus, MassProperties, RigidBody, RigidBodyHandle, RigidBodySet, -}; +use rapier3d::dynamics::{MassProperties, RigidBody, RigidBodyHandle, RigidBodySet, RigidBodyType}; use rapier3d::geometry::{ Collider, ColliderBuilder, ColliderHandle, InteractionGroups, SharedShape, }; @@ -244,7 +242,7 @@ impl Body { for (i, e) in self.create_shapes().into_iter().enumerate() { if let Some((shape, transform)) = e { let mut collider = ColliderBuilder::new(shape) - .position_wrt_parent(*transform) + .position(*transform) .collision_groups(self.collision_groups) .restitution(self.restitution) .friction(self.friction) @@ -374,7 +372,7 @@ impl Body { } else { body.set_gravity_scale(1.0, current_gravity_scale != 1.0); } - body.linear_damping = if let Some((d, i)) = self.area_linear_damp { + body.set_linear_damping(if let Some((d, i)) = self.area_linear_damp { let i = i as f32; if self.area_replace { d / i @@ -387,8 +385,8 @@ impl Body { space_linear_damp } else { self.linear_damp - }; - body.angular_damping = if let Some((d, i)) = self.area_angular_damp { + }); + body.set_angular_damping(if let Some((d, i)) = self.area_angular_damp { let i = i as f32; if self.area_replace { d / i @@ -401,7 +399,7 @@ impl Body { space_linear_damp } else { self.linear_damp - }; + }); self.area_gravity = None; self.area_linear_damp = None; self.area_angular_damp = None; @@ -512,13 +510,13 @@ impl Body { /// Sets whether this body should sleep or not pub fn set_sleeping(&mut self, sleep: bool) { - self.map_rigidbody_mut(|body| body.activation.sleeping = sleep); + self.map_rigidbody_mut(|body| body.activation_mut().sleeping = sleep); } /// Sets the energy threshold when this body will wake up or sleep. /// A negative value disables sleep. pub fn set_sleep_threshold(&mut self, threshold: f32) { - self.map_rigidbody_mut(|body| body.activation.threshold = threshold); + self.map_rigidbody_mut(|body| body.activation_mut().threshold = threshold); } /// Attach or remove a object id to this body. @@ -671,7 +669,7 @@ impl Body { } /// Sets whether this body is static, kinematic or dynamic - pub fn set_body_status(&mut self, status: BodyStatus) { + pub fn set_body_type(&mut self, status: RigidBodyType) { match &mut self.body { Instance::Attached((rb, _), space) => { space @@ -679,25 +677,25 @@ impl Body { s.bodies_mut() .get_mut(*rb) .expect("Invalid body handle") - .set_body_status(status) + .set_body_type(status) }) .expect("Invalid space"); } Instance::Loose(body) => { - body.set_body_status(status); + body.set_body_type(status); } } } /// Sets the groups of this body pub fn set_groups(&mut self, groups: u32) { - self.collision_groups = self.collision_groups.with_groups(groups); + self.collision_groups = self.collision_groups.with_memberships(groups); self.update_interaction_groups(); } /// Sets the mask of this body pub fn set_mask(&mut self, mask: u32) { - self.collision_groups = self.collision_groups.with_mask(mask); + self.collision_groups = self.collision_groups.with_filter(mask); self.update_interaction_groups(); } @@ -720,12 +718,12 @@ impl Body { /// Sets the linear damp of this body pub fn set_linear_damp(&mut self, damp: f32) { - self.map_rigidbody_mut(|body| body.linear_damping = damp); + self.map_rigidbody_mut(|body| body.set_linear_damping(damp)); } /// Sets the angular damp of this body pub fn set_angular_damp(&mut self, damp: f32) { - self.map_rigidbody_mut(|body| body.angular_damping = damp); + self.map_rigidbody_mut(|body| body.set_angular_damping(damp)); } /// Sets the gravity scale of this body. This wakes up the body. @@ -736,13 +734,13 @@ impl Body { /// Sets the restitution of this body and it's colliders, if any. pub fn set_restitution(&mut self, restitution: f32) { self.restitution = restitution; - self.map_colliders(|collider| collider.restitution = restitution); + self.map_colliders(|collider| collider.set_restitution(restitution)); } /// Sets the friction of this body and it's colliders, if any. pub fn set_friction(&mut self, friction: f32) { self.friction = friction; - self.map_colliders(|collider| collider.friction = friction); + self.map_colliders(|collider| collider.set_friction(friction)); } /// Removes the given shape from this body and any colliders @@ -820,13 +818,21 @@ impl Body { /// Locks this body in place at it's current position, which prevents it from being pushed /// by external forces. It may still rotate around it's origin. pub fn set_translation_lock(&mut self, lock: bool) { - self.map_rigidbody_mut(|body| body.set_translation_locked(lock)); + self.map_rigidbody_mut(|body| body.lock_translations(lock, false)); } /// Prevents this body from rotating due to external forces. It can /// still be translated however. The axis is defined in global space. pub fn set_rotation_lock(&mut self, axis: Axis, lock: bool) { - self.map_rigidbody_mut(|body| body.set_rotation_locked(axis, lock)); + self.map_rigidbody_mut(|body| { + let [mut x, mut y, mut z] = body.is_rotation_locked(); + match axis { + Axis::X => x = lock, + Axis::Y => y = lock, + Axis::Z => z = lock, + } + body.restrict_rotations(x, y, z, false); + }); } /// Returns whether this body is locked in place diff --git a/rapier3d/src/server/body.rs b/rapier3d/src/server/body.rs index 3b96769..8fcc4d0 100644 --- a/rapier3d/src/server/body.rs +++ b/rapier3d/src/server/body.rs @@ -4,7 +4,7 @@ use crate::body::Body; use crate::util::*; use gdnative::core_types::*; use gdnative::godot_error; -use rapier3d::dynamics::{ActivationStatus, Axis, BodyStatus, RigidBody, RigidBodyBuilder}; +use rapier3d::dynamics::{RigidBody, RigidBodyActivation, RigidBodyBuilder, RigidBodyType}; #[derive(Debug)] enum Type { @@ -71,7 +71,7 @@ impl Type { fn create_body(&self, sleep: bool) -> RigidBody { match self { Type::Static => RigidBodyBuilder::new_static(), - Type::Kinematic => RigidBodyBuilder::new_kinematic(), + Type::Kinematic => RigidBodyBuilder::new_kinematic_position_based(), Type::Rigid => RigidBodyBuilder::new_dynamic(), Type::Character => RigidBodyBuilder::new_dynamic(), } @@ -260,8 +260,8 @@ fn get_direct_state(body: Index, state: &mut ffi::PhysicsBodyState) { state.set_linear_velocity(vec_na_to_gd(*rb.linvel())); state.set_angular_velocity(vec_na_to_gd(*rb.angvel())); state.set_sleeping(rb.is_sleeping()); - state.set_linear_damp(rb.linear_damping); - state.set_angular_damp(rb.angular_damping); + state.set_linear_damp(rb.linear_damping()); + state.set_angular_damp(rb.angular_damping()); let mp = rb.mass_properties(); state.set_inv_mass(mp.inv_mass); let inv_inertia_sqrt = vec_na_to_gd(mp.inv_principal_inertia_sqrt); @@ -326,15 +326,15 @@ fn set_mode(body: Index, mode: i32) { match Mode::new(mode) { Ok(mode) => { let mode = match mode { - Mode::Static => BodyStatus::Static, - Mode::Kinematic => BodyStatus::Kinematic, - Mode::Rigid => BodyStatus::Dynamic, + Mode::Static => RigidBodyType::Static, + Mode::Kinematic => RigidBodyType::KinematicPositionBased, + Mode::Rigid => RigidBodyType::Dynamic, Mode::Character => { godot_error!("Character mode is not supported"); return; } }; - map_or_err!(body, map_body_mut, |body, _| body.set_body_status(mode)); + map_or_err!(body, map_body_mut, |body, _| body.set_body_type(mode)); } Err(_) => godot_error!("Invalid mode"), } @@ -379,7 +379,7 @@ fn set_state(body: Index, state: i32, value: &Variant) { State::AngularVelocity(vel) => body.set_angular_velocity(vel), State::Sleeping(sleep) => body.set_sleeping(sleep), State::CanSleep(sleep) => body.set_sleep_threshold(if sleep { - ActivationStatus::default_threshold() + RigidBodyActivation::default_threshold() } else { -1.0 }), diff --git a/rapier3d/src/server/mod.rs b/rapier3d/src/server/mod.rs index 17f6746..c3fced0 100644 --- a/rapier3d/src/server/mod.rs +++ b/rapier3d/src/server/mod.rs @@ -296,14 +296,18 @@ fn free(index: Index) { } fn get_process_info(info: i32) -> i32 { - const INFO_ACTIVE_OBJECTS: i32 = 0; - const INFO_COLLISION_PAIRS: i32 = 1; - const INFO_ISLAND_COUNT: i32 = 2; + const INFO_ACTIVE_OBJECTS: i32 = 0; + const INFO_COLLISION_PAIRS: i32 = 1; + const INFO_ISLAND_COUNT: i32 = 2; match info { INFO_ACTIVE_OBJECTS => { let mut total = 0; for (_, space) in SpaceIndex::read_all().iter() { - total += space.bodies().iter().filter(|(_, b)| !b.is_sleeping()).count(); + total += space + .bodies() + .iter() + .filter(|(_, b)| !b.is_sleeping()) + .count(); } total.try_into().unwrap_or(i32::MAX) } diff --git a/rapier3d/src/server/shape.rs b/rapier3d/src/server/shape.rs index b414c61..00a1c4b 100644 --- a/rapier3d/src/server/shape.rs +++ b/rapier3d/src/server/shape.rs @@ -1,7 +1,7 @@ use super::*; use crate::util::*; -use core::mem; use core::convert::TryFrom; +use core::mem; use gdnative::core_types::*; use rapier3d::geometry::{Collider, ColliderBuilder, SharedShape}; use rapier3d::math::Point; @@ -326,7 +326,7 @@ impl Shape { /// Creates a new collider based on the given position and scale pub fn build(&self, position: Isometry3, scale: Vector3, sensor: bool) -> Collider { ColliderBuilder::new(self.build_shape(position, scale)) - .position_wrt_parent(position) + .position(position) .sensor(sensor) .build() } @@ -388,7 +388,7 @@ fn set_data(shape: Index, data: &Variant) { } fn get_data(shape: Index) -> gdnative::sys::godot_variant { - map_or_err!(shape, map_shape, |shape, _| { - shape.data() - }).unwrap_or(Variant::new()).forget() + map_or_err!(shape, map_shape, |shape, _| { shape.data() }) + .unwrap_or(Variant::new()) + .forget() } diff --git a/rapier3d/src/space.rs b/rapier3d/src/space.rs index 59d091b..0cee00d 100644 --- a/rapier3d/src/space.rs +++ b/rapier3d/src/space.rs @@ -11,13 +11,14 @@ use rapier3d::dynamics::{ }; use rapier3d::geometry::{ BroadPhase, Collider, ColliderHandle, ColliderSet, ContactEvent, InteractionGroups, - InteractionTestMode, IntersectionEvent, NarrowPhase, Ray, SolverFlags, + IntersectionEvent, NarrowPhase, Ray, SolverFlags, }; use rapier3d::na::Point3; use rapier3d::pipeline::{ - ContactModificationContext, EventHandler, PairFilterContext, PhysicsHooks, PhysicsHooksFlags, - PhysicsPipeline, QueryPipeline, + ContactModificationContext, EventHandler, PairFilterContext, PhysicsHooks, PhysicsPipeline, + QueryPipeline, }; +use rapier3d::prelude::*; use std::collections::BTreeMap; pub struct Space { @@ -34,6 +35,7 @@ pub struct Space { broad_phase: BroadPhase, narrow_phase: NarrowPhase, + islands: IslandManager, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet, @@ -81,11 +83,8 @@ impl Space { pub fn new() -> Self { let (intersection_send, intersection_recv) = channel::unbounded(); let (contact_send, contact_recv) = channel::unbounded(); - let mut query_pipeline = QueryPipeline::new(); - query_pipeline.set_interaction_test_mode(InteractionTestMode::OR); - let mut narrow_phase = NarrowPhase::new(); - narrow_phase.set_collision_interaction_test_mode(InteractionTestMode::OR); - narrow_phase.set_solver_interaction_test_mode(InteractionTestMode::OR); + let query_pipeline = QueryPipeline::new(); + let narrow_phase = NarrowPhase::new(); Space { physics_pipeline: PhysicsPipeline::new(), query_pipeline, @@ -93,9 +92,12 @@ impl Space { integration_parameters: IntegrationParameters::default(), broad_phase: BroadPhase::new(), narrow_phase, + + islands: IslandManager::new(), bodies: RigidBodySet::new(), colliders: ColliderSet::new(), joints: JointSet::new(), + ccd_solver: CCDSolver::new(), body_exclusions: BodyExclusionHooks::new(contact_send), event_handler: IntersectionEventCollector { @@ -135,6 +137,7 @@ impl Space { self.physics_pipeline.step( &vec_gd_to_na(self.gravity), &self.integration_parameters, + &mut self.islands, &mut self.broad_phase, &mut self.narrow_phase, &mut self.bodies, @@ -308,7 +311,8 @@ impl Space { /// Ensure the pipeline is up to date. pub fn update_query_pipeline(&mut self) { if self.query_pipeline_out_of_date { - self.query_pipeline.update(&self.bodies, &self.colliders); + self.query_pipeline + .update(&self.islands, &self.bodies, &self.colliders); self.query_pipeline_out_of_date = false; } } @@ -320,18 +324,24 @@ impl Space { /// Removes the body with the given handle. Returns the body if it existed pub fn remove_body(&mut self, body: RigidBodyHandle) -> Option { - self.bodies - .remove(body, &mut self.colliders, &mut self.joints) + self.bodies.remove( + body, + &mut self.islands, + &mut self.colliders, + &mut self.joints, + ) } /// Adds a collider and returns a handle to it. pub fn add_collider(&mut self, collider: Collider, body: RigidBodyHandle) -> ColliderHandle { - self.colliders.insert(collider, body, &mut self.bodies) + self.colliders + .insert_with_parent(collider, body, &mut self.bodies) } /// Removes the collider with the given handle. Returns the collider if it existed. pub fn remove_collider(&mut self, collider: ColliderHandle) -> Option { - self.colliders.remove(collider, &mut self.bodies, true) + self.colliders + .remove(collider, &mut self.islands, &mut self.bodies, true) } /// Adds a joint and returns a handle to it. @@ -346,7 +356,8 @@ impl Space { /// Removes the joint with the given handle. Returns the joint if it existed pub fn remove_joint(&mut self, joint: JointHandle) -> Option { - self.joints.remove(joint, &mut self.bodies, true) + self.joints + .remove(joint, &mut self.islands, &mut self.bodies, true) } pub fn cast_ray( @@ -362,8 +373,10 @@ impl Space { let dir = to - from; let (dir, max_toi) = (dir.normalize(), dir.length()); // TODO avoid box + let s = &*self; let filter = match (exclude_bodies, exclude_areas) { - (Some(bodies), Some(areas)) => Box::new(move |_, c: &'_ _| { + (Some(bodies), Some(areas)) => Box::new(move |c| { + let c = s.colliders.get(c).unwrap(); if let Ok(ud) = body::ColliderUserdata::try_from(c) { (ud.ray_pickable() || !ray_pickable_only) && !bodies.contains(&ud.index()) } else { @@ -371,21 +384,23 @@ impl Space { area::ColliderUserdata::try_from(c).expect("Invalid collider userdata"); (ud.ray_pickable() || !ray_pickable_only) && !areas.contains(&ud.index()) } - }) as Box bool>, - (Some(bodies), None) => Box::new(move |_, c: &'_ _| { + }) as Box bool>, + (Some(bodies), None) => Box::new(move |c| { + let c = s.colliders.get(c).unwrap(); if let Ok(ud) = body::ColliderUserdata::try_from(c) { (ud.ray_pickable() || !ray_pickable_only) && !bodies.contains(&ud.index()) } else { false } - }) as Box bool>, - (None, Some(areas)) => Box::new(move |_, c: &'_ _| { + }) as Box bool>, + (None, Some(areas)) => Box::new(move |c| { + let c = s.colliders.get(c).unwrap(); if let Ok(ud) = area::ColliderUserdata::try_from(c) { (ud.ray_pickable() || !ray_pickable_only) && !areas.contains(&ud.index()) } else { false } - }) as Box bool>, + }) as Box bool>, (None, None) => return None, }; let intersection = self.query_pipeline.cast_ray_and_get_normal( @@ -488,7 +503,7 @@ impl Space { .narrow_phase .contact_pairs() .flat_map(|cp| { - let pos = *self.colliders[cp.pair.collider1].position(); + let pos = *self.colliders[cp.collider1].position(); cp.manifolds.iter().map(move |cm| (cm, pos)) }) .flat_map(|(cm, pos)| { @@ -558,15 +573,16 @@ impl BodyExclusionHooks { } } -impl PhysicsHooks for BodyExclusionHooks { - fn active_hooks(&self) -> PhysicsHooksFlags { - PhysicsHooksFlags::FILTER_CONTACT_PAIR | PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS - } - - fn filter_contact_pair(&self, context: &PairFilterContext) -> Option { +impl PhysicsHooks for BodyExclusionHooks { + fn filter_contact_pair( + &self, + context: &PairFilterContext, + ) -> Option { let mut monitor = false; - if let Ok(a) = body::RigidBodyUserdata::try_from(context.rigid_body1) { - if let Ok(b) = body::RigidBodyUserdata::try_from(context.rigid_body2) { + let rb1 = &context.bodies[context.rigid_body1.unwrap()]; + if let Ok(a) = body::RigidBodyUserdata::try_from(rb1) { + let rb2 = &context.bodies[context.rigid_body2.unwrap()]; + if let Ok(b) = body::RigidBodyUserdata::try_from(rb2) { monitor = a.monitoring() || b.monitoring(); let (a, b) = (a.index(), b.index()); let (a, b) = if a.index() < b.index() { @@ -590,13 +606,18 @@ impl PhysicsHooks for BodyExclusionHooks { }) } - fn filter_intersection_pair(&self, _: &PairFilterContext) -> bool { + fn filter_intersection_pair(&self, _: &PairFilterContext) -> bool { false } - fn modify_solver_contacts(&self, context: &mut ContactModificationContext) { - if let Ok(a) = body::ColliderUserdata::try_from(context.collider1) { - if let Ok(b) = body::ColliderUserdata::try_from(context.collider2) { + fn modify_solver_contacts( + &self, + context: &mut ContactModificationContext, + ) { + let c1 = &context.colliders[context.collider1]; + if let Ok(a) = body::ColliderUserdata::try_from(c1) { + let c2 = &context.colliders[context.collider2]; + if let Ok(b) = body::ColliderUserdata::try_from(c2) { for c in context.solver_contacts.iter() { if a.monitoring() { let contact = body::ContactEvent::new( @@ -629,5 +650,5 @@ impl EventHandler for IntersectionEventCollector { let _ = self.sender.try_send(event); } - fn handle_contact_event(&self, _: ContactEvent) {} + fn handle_contact_event(&self, _: ContactEvent, _: &ContactPair) {} } From 56034568a0b52df05e3c9ae014e1d1900d241028 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Thu, 24 Jun 2021 20:44:18 +0200 Subject: [PATCH 2/2] Fix contact hooks. They're now per-collider instead of per-pipeline. This may allows some extra optimizations, which should be looked into. --- rapier3d/src/body.rs | 7 ++++++- rapier3d/src/server/joint.rs | 20 +++++++++----------- rapier3d/src/space.rs | 20 +++++++++++++------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/rapier3d/src/body.rs b/rapier3d/src/body.rs index 986ce82..ec8aba5 100644 --- a/rapier3d/src/body.rs +++ b/rapier3d/src/body.rs @@ -6,6 +6,7 @@ use crate::space::Space; use crate::util::*; use core::convert::{TryFrom, TryInto}; use gdnative::core_types::*; +use rapier3d::prelude::*; use rapier3d::dynamics::{MassProperties, RigidBody, RigidBodyHandle, RigidBodySet, RigidBodyType}; use rapier3d::geometry::{ Collider, ColliderBuilder, ColliderHandle, InteractionGroups, SharedShape, @@ -177,7 +178,9 @@ impl Body { let shape_scale = iso.rotation * vec_gd_to_na(scl); let shape_scale = vec_gd_to_na(self_scale).component_mul(&shape_scale); let shape_scale = vec_na_to_gd(shape_scale); - let collider = shape.build(body_shape.transform, shape_scale, false); + let mut collider = shape.build(body_shape.transform, shape_scale, false); + collider.set_active_hooks(ActiveHooks::FILTER_CONTACT_PAIRS | ActiveHooks::MODIFY_SOLVER_CONTACTS); + collider.set_active_events(ActiveEvents::INTERSECTION_EVENTS); let collider = space.add_collider(collider, *rb); colliders.push(Some(collider)); }) @@ -246,6 +249,8 @@ impl Body { .collision_groups(self.collision_groups) .restitution(self.restitution) .friction(self.friction) + .active_hooks(ActiveHooks::FILTER_CONTACT_PAIRS | ActiveHooks::MODIFY_SOLVER_CONTACTS) + .active_events(ActiveEvents::INTERSECTION_EVENTS) .build(); collider.user_data = ColliderUserdata::new( index, diff --git a/rapier3d/src/server/joint.rs b/rapier3d/src/server/joint.rs index d7b59b2..8ac232b 100644 --- a/rapier3d/src/server/joint.rs +++ b/rapier3d/src/server/joint.rs @@ -148,6 +148,7 @@ fn create_hinge( body_b: Index, transform_b: &Transform, ) -> Option { + dbg!("CREATE MEEEEE"); let body_a = if let Index::Body(index) = body_a { index } else { @@ -191,8 +192,11 @@ fn create_hinge( } fn disable_collisions_between_bodies(joint: Index, disable: bool) { - let enable = !disable; + dbg!("I live you shit"); + // *disable* collisions = *enable* exclusion + let enable = disable; map_or_err!(joint, map_joint_mut, |joint, _| { + dbg!(joint.exclude_bodies, enable); if joint.exclude_bodies != enable { joint.exclude_bodies = enable; if let Instance::Attached(joint, space) = joint.joint { @@ -204,17 +208,11 @@ fn disable_collisions_between_bodies(joint: Index, disable: bool) { let b = space.bodies().get(joint.body2).expect("Invalid body B"); let a = body::RigidBodyUserdata::try_from(a).unwrap().index(); let b = body::RigidBodyUserdata::try_from(b).unwrap().index(); - let result = if enable { - space.add_body_exclusion(a, b) + // Adding an exclusion multiple times is valid. + if enable { + let _ = space.add_body_exclusion(a, b); } else { - space.remove_body_exclusion(a, b) - }; - if let Err(e) = result { - if enable { - godot_error!("Failed to add bodies: {:?}", e); - } else { - godot_error!("Failed to remove bodies: {:?}", e); - } + let _ = space.remove_body_exclusion(a, b); } }); } diff --git a/rapier3d/src/space.rs b/rapier3d/src/space.rs index 0cee00d..9c47d44 100644 --- a/rapier3d/src/space.rs +++ b/rapier3d/src/space.rs @@ -294,7 +294,7 @@ impl Space { /// Make two bodies not interact with each other. /// Returns `Ok` if the bodies did not exclude each other already, `Err` otherwise. - pub fn add_body_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ()> { + pub fn add_body_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ExclusionAlreadyExists> { self.body_exclusions.add_exclusion(index_a, index_b) } @@ -304,7 +304,7 @@ impl Space { &mut self, index_a: BodyIndex, index_b: BodyIndex, - ) -> Result<(), ()> { + ) -> Result<(), ExclusionDoesntExist> { self.body_exclusions.remove_exclusion(index_a, index_b) } @@ -524,6 +524,12 @@ impl Space { } } +#[derive(Debug)] +pub struct ExclusionAlreadyExists; + +#[derive(Debug)] +pub struct ExclusionDoesntExist; + impl BodyExclusionHooks { fn new(contacts_sender: Sender<(BodyIndex, body::ContactEvent)>) -> Self { Self { @@ -532,7 +538,7 @@ impl BodyExclusionHooks { } } - fn add_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ()> { + fn add_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ExclusionAlreadyExists> { // TODO how should we handle self-exclusions? (index_a == index_b) let (a, b) = (index_a, index_b); let (a, b) = if a.index() < b.index() { @@ -542,7 +548,7 @@ impl BodyExclusionHooks { }; if let Some(vec) = self.exclusions.get_mut(a.index() as usize) { if vec.contains(&b) { - Err(()) + Err(ExclusionAlreadyExists) } else { vec.push(b); Ok(()) @@ -557,7 +563,7 @@ impl BodyExclusionHooks { // TODO implement body_remove_exception and remove the allow #[allow(dead_code)] - fn remove_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ()> { + fn remove_exclusion(&mut self, index_a: BodyIndex, index_b: BodyIndex) -> Result<(), ExclusionDoesntExist> { let (a, b) = (index_a, index_b); let (a, b) = if a.index() < b.index() { (a, b) @@ -565,10 +571,10 @@ impl BodyExclusionHooks { (b, a) }; if let Some(vec) = self.exclusions.get_mut(a.index() as usize) { - vec.swap_remove(vec.iter().position(|&e| e == b).ok_or(())?); + vec.swap_remove(vec.iter().position(|&e| e == b).ok_or(ExclusionDoesntExist)?); Ok(()) } else { - Err(()) + Err(ExclusionDoesntExist) } } }