From 51de0a9cc8dc238f27e2f56bb766aff997f0e599 Mon Sep 17 00:00:00 2001 From: Anselm Hook Date: Tue, 28 Nov 2023 21:45:08 +0000 Subject: [PATCH] making sure that things that collide do get a collision component and that these are deleted once things are no longer colliding --- packages/engine/src/physics/classes/Physics.ts | 7 +++++-- packages/engine/src/physics/systems/PhysicsSystem.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/engine/src/physics/classes/Physics.ts b/packages/engine/src/physics/classes/Physics.ts index 5325cc7486..0dd92f4309 100644 --- a/packages/engine/src/physics/classes/Physics.ts +++ b/packages/engine/src/physics/classes/Physics.ts @@ -541,8 +541,11 @@ const drainCollisionEventQueue = (physicsWorld: World) => (handle1: number, hand const entity1 = (rigidBody1?.userData as any)['entity'] const entity2 = (rigidBody2?.userData as any)['entity'] - const collisionComponent1 = getOptionalComponent(entity1, CollisionComponent) - const collisionComponent2 = getOptionalComponent(entity2, CollisionComponent) + setComponent(entity1, CollisionComponent) + setComponent(entity2, CollisionComponent) + + const collisionComponent1 = getComponent(entity1, CollisionComponent) + const collisionComponent2 = getComponent(entity2, CollisionComponent) if (started) { const type = isTriggerEvent ? CollisionEvents.TRIGGER_START : CollisionEvents.COLLISION_START diff --git a/packages/engine/src/physics/systems/PhysicsSystem.ts b/packages/engine/src/physics/systems/PhysicsSystem.ts index d7c2e52329..fe34c565a1 100755 --- a/packages/engine/src/physics/systems/PhysicsSystem.ts +++ b/packages/engine/src/physics/systems/PhysicsSystem.ts @@ -32,7 +32,7 @@ import { getMutableState, getState, none } from '@etherealengine/hyperflux' import { EngineState } from '../../ecs/classes/EngineState' import { Entity } from '../../ecs/classes/Entity' -import { defineQuery, getComponent } from '../../ecs/functions/ComponentFunctions' +import { defineQuery, getComponent, removeComponent } from '../../ecs/functions/ComponentFunctions' import { SimulationSystemGroup } from '../../ecs/functions/EngineFunctions' import { defineSystem } from '../../ecs/functions/SystemFunctions' import { NetworkState } from '../../networking/NetworkState' @@ -204,6 +204,13 @@ const execute = () => { RigidBodyComponent.angularVelocity.y[entity] = angvel.y RigidBodyComponent.angularVelocity.z[entity] = angvel.z } + + for (const collisionEntity of collisionQuery()) { + const collisionComponent = getComponent(collisionEntity, CollisionComponent) + if (!collisionComponent.size) { + removeComponent(collisionEntity, CollisionComponent) + } + } } const reactor = () => {