use-cannon icon indicating copy to clipboard operation
use-cannon copied to clipboard

Adding multiple bodies to a single mesh

Open nevernotsean opened this issue 4 years ago • 5 comments

For some cases attaching multiple cannon bodies to a single mesh is necessary. As of now, use-cannon only lets you pass the ref from a single mesh or an instanced mesh.

For #6 , a single THREE.PlaneBufferGeometry would get widthSegments * heightSegments CANNON.Particle bodies. For Ragdoll physics, a single THREE.SkinnedMesh would get a body for each THREE.Bone.

In both examples, bodies would change the positions of certain vertex groups.

How could we add an option to link multiple bodies to a single mesh? Similarly to how THREE.InstancedMesh is handled?

At the moment, I am creating an example that uses an extra THREE.InstancedMesh to 'trick' use-cannon into creating multiple bodies, then copying the position and rotation data over to the vertex groups.

nevernotsean avatar Mar 27 '20 20:03 nevernotsean

isn't this what useCompoundBody does? I#ve just updated the ragdoll demo with it and @codynova made a super simple two-body demo. or is this something else?

function CompoundBody(props) {
  const [ref] = useCompoundBody(() => ({
    mass: 12,
    ...props,
    shapes: [
      { type: 'Box', position: [0, 0, 0], rotation: [0, 0, 0], args: [0.5, 0.5, 0.5] },
      { type: 'Sphere', position: [1, 0, 0], rotation: [0, 0, 0], args: [0.65] },
    ],
  }))
  return (
    <group ref={ref}>
      <mesh castShadow>
        <boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
        <meshNormalMaterial attach="material" />
      </mesh>
      <mesh castShadow position={[1, 0, 0]}>
        <sphereBufferGeometry attach="geometry" args={[0.65, 16, 16]} />
        <meshNormalMaterial attach="material" />
      </mesh>
    </group>
  )
}

drcmda avatar Mar 27 '20 20:03 drcmda

of course in a particle set or a cloth simulation i guess this wouldnt work b/c each body has to have some freedom of movement albeit constrained to the other nodes - and be able to move its individual attached body properly. 🤔

drcmda avatar Mar 27 '20 20:03 drcmda

we could maybe learn from react-springs playbook here, esp useSprings: https://www.react-spring.io/docs/hooks/use-springs

const [refs, api] = use?????(10000, () => ({ ... }))
return data.map((id, index) => <mesh key={id} ref={refs[index]} ... />

drcmda avatar Mar 27 '20 20:03 drcmda

Right, Compound Shapes is multiple collider shapes on one body and is perfectly fine. But multiple bodies added to one mesh is something different.

In testing I also found that Constraints could use something like useSprings too.

nevernotsean avatar Mar 28 '20 02:03 nevernotsean

Another use case: I have a game character that I want physics on, but a larger cannon body that acts as a trigger if another entity gets within a certain range.

wuharvey avatar Aug 27 '21 03:08 wuharvey