From 6ed8d76668c2fa08487af6097ea0e61de62a05de Mon Sep 17 00:00:00 2001 From: Gheric Speiginer Date: Tue, 29 Nov 2022 15:11:51 -0800 Subject: [PATCH] Add setRigidBodyType (#7286) * Add setRigidBodyType * make setRigidBodyType actually set rigidbody type Co-authored-by: HexaField --- .../physics/components/RigidBodyComponent.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/engine/src/physics/components/RigidBodyComponent.ts b/packages/engine/src/physics/components/RigidBodyComponent.ts index f19f974a12..741b041c85 100644 --- a/packages/engine/src/physics/components/RigidBodyComponent.ts +++ b/packages/engine/src/physics/components/RigidBodyComponent.ts @@ -4,7 +4,14 @@ import { Quaternion, Vector3 } from 'three' import { proxifyQuaternion, proxifyVector3 } from '../../common/proxies/createThreejsProxy' import { Engine } from '../../ecs/classes/Engine' -import { createMappedComponent, defineComponent, removeComponent } from '../../ecs/functions/ComponentFunctions' +import { Entity } from '../../ecs/classes/Entity' +import { + createMappedComponent, + defineComponent, + getComponent, + removeComponent, + setComponent +} from '../../ecs/functions/ComponentFunctions' const { f64 } = Types const Vector3Schema = { x: f64, y: f64, z: f64 } @@ -84,3 +91,12 @@ export const getTagComponentForRigidBody = (type: RigidBodyType): RigidBodyTypes return RigidBodyKinematicVelocityBasedTagComponent } } + +export const setRigidBodyType = (entity: Entity, type: RigidBodyType) => { + const rigidbody = getComponent(entity, RigidBodyComponent) + const oldTypeTag = getTagComponentForRigidBody(rigidbody.body.bodyType()) + removeComponent(entity, oldTypeTag) + rigidbody.body.setBodyType(type) + const typeTag = getTagComponentForRigidBody(type) + setComponent(entity, typeTag) +}