Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 737d20b

Browse files
authored
fix collider reactivity (#10877)
* fix collider reactivity * fix tests * optimize
1 parent 42b5150 commit 737d20b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

packages/spatial/src/physics/components/ColliderComponent.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
2323
Ethereal Engine. All Rights Reserved.
2424
*/
2525

26-
import { useEffect, useLayoutEffect } from 'react'
2726
import { Vector3 } from 'three'
2827

2928
import { defineComponent, useComponent, useEntityContext, useOptionalComponent } from '@etherealengine/ecs'
3029
import { useState } from '@etherealengine/hyperflux'
3130

31+
import { useLayoutEffect } from 'react'
3232
import { useAncestorWithComponent } from '../../transform/components/EntityTree'
3333
import { TransformComponent } from '../../transform/components/TransformComponent'
3434
import { Physics } from '../classes/Physics'
@@ -88,10 +88,11 @@ export const ColliderComponent = defineComponent({
8888
const triggerComponent = useOptionalComponent(entity, TriggerComponent)
8989
const hasCollider = useState(false)
9090

91-
useEffect(() => {
91+
useLayoutEffect(() => {
9292
if (!rigidbodyComponent || !physicsWorld) return
9393

9494
const colliderDesc = Physics.createColliderDesc(physicsWorld, entity, rigidbodyEntity)
95+
9596
if (!colliderDesc) return
9697

9798
Physics.attachCollider(physicsWorld, colliderDesc, rigidbodyEntity, entity)
@@ -132,7 +133,7 @@ export const ColliderComponent = defineComponent({
132133
Physics.setCollisionMask(physicsWorld, entity, component.collisionMask.value)
133134
}, [physicsWorld, component.collisionMask])
134135

135-
useEffect(() => {
136+
useLayoutEffect(() => {
136137
if (!physicsWorld || !triggerComponent?.value || !hasCollider.value) return
137138

138139
Physics.setTrigger(physicsWorld, entity, true)

packages/spatial/src/transform/components/EntityTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export function useTreeQuery(entity: Entity) {
384384
* @returns
385385
*/
386386
export function useAncestorWithComponent(entity: Entity, component: ComponentType<any>) {
387-
const result = useHookstate(UndefinedEntity)
387+
const result = useHookstate(() => getAncestorWithComponent(entity, component))
388388

389389
useImmediateEffect(() => {
390390
let unmounted = false

0 commit comments

Comments
 (0)