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

Commit

Permalink
Fix scaling rigidbodies and collider updates (#10757)
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField authored Jul 30, 2024
1 parent 4a09fdc commit 19d407b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
18 changes: 11 additions & 7 deletions packages/spatial/src/physics/systems/PhysicsPreTransformSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ import { Vector3_Zero } from '../../common/constants/MathConstants'
import { SceneComponent } from '../../renderer/components/SceneComponents'
import { EntityTreeComponent, getAncestorWithComponent } from '../../transform/components/EntityTree'
import { TransformComponent } from '../../transform/components/TransformComponent'
import { computeTransformMatrix, isDirty, TransformSystem } from '../../transform/systems/TransformSystem'
import { computeTransformMatrix, isDirty, TransformDirtyUpdateSystem } from '../../transform/systems/TransformSystem'
import { Physics } from '../classes/Physics'
import { ColliderComponent } from '../components/ColliderComponent'
import { RigidBodyComponent } from '../components/RigidBodyComponent'

const _vec3 = new Vector3()
const _quat = new Quaternion()

const position = new Vector3()
const rotation = new Quaternion()
const scale = new Vector3()
Expand Down Expand Up @@ -86,9 +89,9 @@ export const lerpTransformFromRigidbody = (entity: Entity, alpha: number) => {
if (parentEntity) {
const sceneEntity = getAncestorWithComponent(entity, SceneComponent)
const sceneTransform = getComponent(sceneEntity, TransformComponent)
// todo: figure out proper scale support
const scale = getComponent(entity, TransformComponent).scale
// 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)
transform.matrixWorld.multiplyMatrices(sceneTransform.matrixWorld, transform.matrix)

Expand Down Expand Up @@ -165,9 +168,10 @@ const copyTransformToCollider = (entity: Entity) => {
computeTransformMatrix(entity)
const rigidbodyEntity = getAncestorWithComponent(entity, RigidBodyComponent)
if (!rigidbodyEntity) return
TransformComponent.getMatrixRelativeToEntity(entity, rigidbodyEntity, mat4)
mat4.decompose(position, rotation, scale)
Physics.setColliderPose(world, entity, position, rotation)
const colliderDesc = Physics.createColliderDesc(world, entity, rigidbodyEntity)
if (!colliderDesc) return
Physics.removeCollider(world, entity)
Physics.attachCollider(world, colliderDesc, rigidbodyEntity, entity)
}

const rigidbodyQuery = defineQuery([TransformComponent, RigidBodyComponent])
Expand Down Expand Up @@ -209,6 +213,6 @@ export const execute = () => {

export const PhysicsPreTransformSystem = defineSystem({
uuid: 'ee.engine.PhysicsPreTransformSystem',
insert: { before: TransformSystem },
insert: { after: TransformDirtyUpdateSystem },
execute
})
15 changes: 12 additions & 3 deletions packages/spatial/src/transform/systems/TransformSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const isDirty = (entity: Entity) => TransformComponent.dirtyTransforms[en

const sortedTransformEntities = [] as Entity[]

const execute = () => {
const sortAndMakeDirtyEntities = () => {
// TODO: move entity tree mutation logic here for more deterministic and less redundant calculations

// if transform order is dirty, sort by reference depth
Expand All @@ -143,7 +143,6 @@ const execute = () => {
/**
* Sort transforms if needed
*/
const xrFrame = getState(XRState).xrFrame

let needsSorting = TransformComponent.transformsNeedSorting

Expand Down Expand Up @@ -173,7 +172,9 @@ const execute = () => {
TransformComponent.dirtyTransforms[getOptionalComponent(entity, EntityTreeComponent)?.parentEntity ?? -1] ||
false
}
}

const execute = () => {
const dirtySortedTransformEntities = sortedTransformEntities.filter(isDirtyNonRigidbody)
for (const entity of dirtySortedTransformEntities) computeTransformMatrix(entity)

Expand All @@ -186,6 +187,8 @@ const execute = () => {
const viewerEntity = getState(EngineState).viewerEntity
const cameraEntities = cameraQuery()

const xrFrame = getState(XRState).xrFrame

for (const entity of cameraEntities) {
if (xrFrame && entity === viewerEntity) continue
const camera = getComponent(entity, CameraComponent)
Expand Down Expand Up @@ -244,10 +247,16 @@ export const TransformSystem = defineSystem({
reactor
})

export const TransformDirtyUpdateSystem = defineSystem({
uuid: 'ee.engine.TransformDirtyUpdateSystem',
insert: { before: TransformSystem },
execute: sortAndMakeDirtyEntities
})

export const TransformDirtyCleanupSystem = defineSystem({
uuid: 'ee.engine.TransformDirtyCleanupSystem',
insert: { after: TransformSystem },
execute: () => {
for (const entity in TransformComponent.dirtyTransforms) TransformComponent.dirtyTransforms[entity] = false
for (const entity in TransformComponent.dirtyTransforms) delete TransformComponent.dirtyTransforms[entity]
}
})

0 comments on commit 19d407b

Please sign in to comment.