Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding multiple bodies to a single mesh #48

Open
nevernotsean opened this issue Mar 27, 2020 · 5 comments
Open

Adding multiple bodies to a single mesh #48

nevernotsean opened this issue Mar 27, 2020 · 5 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@nevernotsean
Copy link
Contributor

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 nevernotsean added enhancement New feature or request question Further information is requested labels Mar 27, 2020
@drcmda
Copy link
Member

drcmda commented Mar 27, 2020

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
Copy link
Member

drcmda commented Mar 27, 2020

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
Copy link
Member

drcmda commented Mar 27, 2020

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]} ... />

@nevernotsean
Copy link
Contributor Author

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.

@wuharvey
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants