pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.76k stars 155 forks source link

Expose body's collisionResponse property #125

Closed abbazabacto closed 3 years ago

abbazabacto commented 3 years ago

Each body has a collisionResponse property which can be set after instantiation.

Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled. http://schteppe.github.io/cannon.js/docs/classes/Body.html#property_collisionResponse

Setting the collisionResponse to 0 will keep the collision detection, without applying contact forces on collision.

abbazabacto commented 3 years ago

Was not able to push a branch on this repo, here my local git diff if that will help you in any way adding this feature request (lines with + are added).

diff --git a/src/hooks.ts b/src/hooks.ts
index aa15c9c..5662c66 100644
--- a/src/hooks.ts
+++ b/src/hooks.ts
@@ -15,6 +15,7 @@ export type AtomicProps = {
   sleepTimeLimit?: number
   collisionFilterGroup?: number
   collisionFilterMask?: number
+  collisionResponse?: number
   fixedRotation?: boolean
 }

@@ -269,6 +270,7 @@ function useBody(
         collisionFilterMask: makeAtomic('collisionFilterMask', index),
+        collisionResponse: makeAtomic('collisionResponse', index),
         fixedRotation: makeAtomic('fixedRotation', index),
         // Apply functions
         applyForce(force: number[], worldPoint: number[]) {
diff --git a/src/worker.js b/src/worker.js
index 68fa155..d44c45e 100644
--- a/src/worker.js
+++ b/src/worker.js
@@ -144,6 +144,7 @@ self.onmessage = (e) => {
           material,
           shapes,
           onCollide,
+          collisionResponse,
           ...extra
         } = props[i]

@@ -155,6 +156,10 @@ self.onmessage = (e) => {
         })
         body.uuid = uuid[i]

+        if (collisionResponse !== undefined) {
+          body.collisionResponse = collisionResponse
+        }
+
         if (type === 'Compound') {
           shapes.forEach(({ type, args, position, rotation, material, ...extra }) => {
             const shapeBody = body.addShape(
@@ -262,6 +267,9 @@ self.onmessage = (e) => {
     case 'setCollisionFilterMask':
       bodies[uuid].collisionFilterMask = props
       break
+    case 'setCollisionResponse':
+      bodies[uuid].collisionResponse = props
+      break
     case 'setFixedRotation':
       bodies[uuid].fixedRotation = props
       break
nwpointer commented 3 years ago

would also love this feature :)