From baea3158929d9b83cd6794bb9a5512a0806f2361 Mon Sep 17 00:00:00 2001 From: HexaField Date: Tue, 6 Aug 2024 10:22:55 +1000 Subject: [PATCH] rigidbody initialization quick fix --- packages/spatial/src/physics/components/ColliderComponent.tsx | 4 ++-- packages/spatial/src/physics/components/RigidBodyComponent.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/spatial/src/physics/components/ColliderComponent.tsx b/packages/spatial/src/physics/components/ColliderComponent.tsx index f6c3604a40..ad84c9889b 100644 --- a/packages/spatial/src/physics/components/ColliderComponent.tsx +++ b/packages/spatial/src/physics/components/ColliderComponent.tsx @@ -89,7 +89,7 @@ export const ColliderComponent = defineComponent({ const hasCollider = useState(false) useLayoutEffect(() => { - if (!rigidbodyComponent || !physicsWorld) return + if (!rigidbodyComponent?.initialized?.value || !physicsWorld) return const colliderDesc = Physics.createColliderDesc(physicsWorld, entity, rigidbodyEntity) @@ -102,7 +102,7 @@ export const ColliderComponent = defineComponent({ Physics.removeCollider(physicsWorld, entity) hasCollider.set(false) } - }, [physicsWorld, component.shape, rigidbodyEntity, !!rigidbodyComponent, transform.scale]) + }, [physicsWorld, component.shape, !!rigidbodyComponent?.initialized?.value, transform.scale]) useLayoutEffect(() => { if (!physicsWorld) return diff --git a/packages/spatial/src/physics/components/RigidBodyComponent.ts b/packages/spatial/src/physics/components/RigidBodyComponent.ts index e868092bc9..2c0706dd38 100644 --- a/packages/spatial/src/physics/components/RigidBodyComponent.ts +++ b/packages/spatial/src/physics/components/RigidBodyComponent.ts @@ -67,6 +67,8 @@ export const RigidBodyComponent = defineComponent({ canSleep: true, gravityScale: 1, // internal + /** @deprecated @todo make the physics api properly reactive to remove this property */ + initialized: false, previousPosition: proxifyVector3(this.previousPosition, entity), previousRotation: proxifyQuaternion(this.previousRotation, entity), position: proxifyVector3(this.position, entity), @@ -118,8 +120,10 @@ export const RigidBodyComponent = defineComponent({ useEffect(() => { if (!physicsWorld) return Physics.createRigidBody(physicsWorld, entity) + component.initialized.set(true) return () => { Physics.removeRigidbody(physicsWorld, entity) + component.initialized.set(false) } }, [physicsWorld])