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

Respect parent position, rotation[, scale?]

Open mattblackdev opened this issue 3 years ago • 2 comments

Sometimes you want to do this:

function Thing({ position, rotation }) {
  const [ref] = useBox(() => ({ position, rotation }));
  return (
    <Box ref={ref} />
  );
}

function CoolThing({ position, rotation }) {
  return (
    <group position={position} rotation={rotation}>
      <Thing position={offset1} />
      <Thing position={offset2} />
      <Thing />
      <Thing rotation={offsetRot} />
    </group>
  );
}

But then you learn that the physics world has no consideration of the scene hierarchy.

And you might even try to do this:

<Box onUpdate={self => {
  const v = new Vector3()
  self.getWorldPosition(v)
  api.position.copy(v)
}} />

But in the end you learn that this is a harder problem to solve indeed...

So you create a repro of the issue: https://codesandbox.io/s/use-cannon-not-respecting-parent-rchms?file=/src/App.tsx

And you go back to manually managing world positions:

function CoolThing({ position, rotation }) {
  return (
    <>
      <Thing position={add(position, offset1)} rotation={rotation} />
      <Thing position={add(position, offset2)} rotation={rotation} />
      <Thing position={position} rotation={rotation} />
      <Thing position={position} rotation={add(rotation, offsetRot)} />
    </>
  );
}

mattblackdev avatar Dec 16 '21 01:12 mattblackdev

I don't really understand the problem you are reporting.

You have 4 different physics objects inside of a group, but the group has no physics representation.

What is it supposed to be doing? Is it affecting the positions and rotations of the 4 physics bodies?

This looks to me like a single CompoundBody with 4 shapes. Have you tried using that?

bjornstar avatar Dec 18 '21 08:12 bjornstar

I see what you mean. I implemented parts of structures (walls, floors, etc.) and interior objects as separate components that could be composed interchangeably.

When it came time to position different compositions I tried putting them inside groups to only manage one world position per building. Problem was the groups' position didn't affect the children's colliders, only their meshes.

I get the compound body approach but it doesn't seem ideal for composing components that have their own colliders.

Thanks for giving this some thought. Cheers!

mattblackdev avatar Dec 24 '21 01:12 mattblackdev