From c0139461659a50e178c7bea44abdbd839863261f Mon Sep 17 00:00:00 2001 From: Josh Field Date: Sat, 3 Aug 2024 11:05:50 +1000 Subject: [PATCH] fix physics rotation and scaling (#10823) --- .../physics/systems/PhysicsPreTransformSystem.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/spatial/src/physics/systems/PhysicsPreTransformSystem.ts b/packages/spatial/src/physics/systems/PhysicsPreTransformSystem.ts index c261ebe7af..00e139c75f 100644 --- a/packages/spatial/src/physics/systems/PhysicsPreTransformSystem.ts +++ b/packages/spatial/src/physics/systems/PhysicsPreTransformSystem.ts @@ -29,7 +29,7 @@ import { defineQuery, defineSystem, Entity, getComponent } from '@etherealengine import { ECSState } from '@etherealengine/ecs/src/ECSState' import { getState } from '@etherealengine/hyperflux' -import { Vector3_Zero } from '../../common/constants/MathConstants' +import { Vector3_One, Vector3_Zero } from '../../common/constants/MathConstants' import { SceneComponent } from '../../renderer/components/SceneComponents' import { EntityTreeComponent, getAncestorWithComponent, iterateEntityNode } from '../../transform/components/EntityTree' import { TransformComponent } from '../../transform/components/TransformComponent' @@ -38,9 +38,8 @@ import { Physics } from '../classes/Physics' import { ColliderComponent } from '../components/ColliderComponent' import { RigidBodyComponent } from '../components/RigidBodyComponent' -const _vec3 = new Vector3() -const _quat = new Quaternion() - +const localMatrix = new Matrix4() +const parentMatrixInverse = new Matrix4() const position = new Vector3() const rotation = new Quaternion() const scale = new Vector3() @@ -82,10 +81,10 @@ export const lerpTransformFromRigidbody = (entity: Entity, alpha: number) => { const sceneEntity = getAncestorWithComponent(entity, SceneComponent) const sceneTransform = getComponent(sceneEntity, TransformComponent) - // if the entity has a parent, we need to use the scene space - TransformComponent.getMatrixRelativeToScene(entity, mat4) - mat4.decompose(_vec3, _quat, scale) - transform.matrix.compose(position, rotation, scale) + parentMatrixInverse.copy(sceneTransform.matrixWorld).invert() + localMatrix.compose(position, rotation, Vector3_One).premultiply(parentMatrixInverse) + localMatrix.decompose(position, rotation, scale) + transform.matrix.compose(position, rotation, transform.scale) transform.matrixWorld.multiplyMatrices(sceneTransform.matrixWorld, transform.matrix) /** set all children dirty deeply, but set this entity to clean */