use-cannon
use-cannon copied to clipboard
Adding multiple bodies to a single mesh
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.
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>
)
}
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. 🤔
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]} ... />
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.
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.