Closed raphaelswiggy closed 10 months ago
This is more of a React-problem than a Rapier problem!
You can absolutely attach refs
to those boxes, using an array of refs for example.
const [refs] = useState(() => [] as RapierRigidBody[])
const addRef = (ref) => refs.push(ref)
useFrame(() => {
refs.forEach(body => {
// do something with body
})
})
...
setAllBoxes([
...allBoxes,
<RigidBody key={allBoxes.length} ref={addRef}>
<mesh
Here's a sloppy example using a map to keep track of the refs. https://codesandbox.io/p/sandbox/relaxed-chandrasekhar-6f4prv?file=%2Fsrc%2FApp.tsx%3A18%2C33
It seems like we need a ref to apply forces to a RigidBody. But what can we do for dynamically created RigidBodies, for which we don't have a predetermined count and cannot create refs in advance? For example, when we want to create a RigidBody on the click of a button.
Here is the code. There is also a link to a codesandbox to test it. What can be done in the allBoxesJump function? codesandbox