diff --git a/packages/sdk/src/actor/physics/physicsBridge.ts b/packages/sdk/src/actor/physics/physicsBridge.ts new file mode 100644 index 000000000..3cf429d46 --- /dev/null +++ b/packages/sdk/src/actor/physics/physicsBridge.ts @@ -0,0 +1,19 @@ +/*! + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { Guid } from "../../util"; +import { TransformLike } from "../transform"; + +export interface PhysicsBridgeTransformInfo { + id: Guid; + transform: Partial; +} + + export interface PhysicsBridgeTransformUpdate { + id: Guid; + time: number; + transforms: Array>; + } + \ No newline at end of file diff --git a/packages/sdk/src/core/contextInternal.ts b/packages/sdk/src/core/contextInternal.ts index abe05228e..e70fb00d6 100644 --- a/packages/sdk/src/core/contextInternal.ts +++ b/packages/sdk/src/core/contextInternal.ts @@ -40,6 +40,7 @@ import { resolveJsonValues, safeAccessPath as safeGet, } from '../internal'; +import { PhysicsBridgeTransformUpdate } from '../actor/physics/physicsBridge'; /** * @hidden @@ -342,6 +343,7 @@ export class ContextInternal { execution.on('protocol.user-left', this.userLeft.bind(this)); execution.on('protocol.update-user', this.updateUser.bind(this)); execution.on('protocol.perform-action', this.performAction.bind(this)); + execution.on('protocol.physicsbridge-update-transforms', this.updatePhysicsBridgeTransforms.bind(this)); execution.on('protocol.receive-rpc', this.receiveRPC.bind(this)); execution.on('protocol.collision-event-raised', this.collisionEventRaised.bind(this)); execution.on('protocol.trigger-event-raised', this.triggerEventRaised.bind(this)); @@ -479,6 +481,12 @@ export class ContextInternal { }); } + public updatePhysicsBridgeTransforms(transforms: Partial) { + if (!transforms) + return; + this.context.emitter.emit('physicsbridge-transforms-update', transforms); + } + public updateAnimations(animPatches: Array>) { if (!animPatches) { return; } const newAnims: Animation[] = []; diff --git a/packages/sdk/src/internal/adapters/multipeer/rules.ts b/packages/sdk/src/internal/adapters/multipeer/rules.ts index 588a6d883..d6e04aeef 100644 --- a/packages/sdk/src/internal/adapters/multipeer/rules.ts +++ b/packages/sdk/src/internal/adapters/multipeer/rules.ts @@ -794,6 +794,53 @@ export const Rules: { [id in Payloads.PayloadType]: Rule } = { } }, + // ======================================================================== + 'physicsbridge-transforms-update': { + ...DefaultRule, + synchronization: { + stage: 'always', + before: 'ignore', + during: 'queue', + after: 'allow' + }, + client: { + ...DefaultRule.client, + beforeQueueMessageForClient: ( + session: Session, + client: Client, + message: Message, + promise: ExportedPromise + ) => { + + return message; + }, + shouldSendToUser: (message: Message, userId, session, client) => { + + return true; + } + }, + session: { + ...DefaultRule.session, + beforeReceiveFromApp: ( + session: Session, + message: Message + ) => { + + return message; + }, + beforeReceiveFromClient: ( + session: Session, + client: Client, + message: Message + ) => { + + session.sendPayloadToClients(message.payload, (value) => value.id !== client.id); + + return message; + } + } + }, + // ======================================================================== 'rigidbody-add-force': { ...DefaultRule, diff --git a/packages/sdk/src/internal/payloads/payloads.ts b/packages/sdk/src/internal/payloads/payloads.ts index 00db84f06..e78dd8d5c 100644 --- a/packages/sdk/src/internal/payloads/payloads.ts +++ b/packages/sdk/src/internal/payloads/payloads.ts @@ -51,6 +51,7 @@ export type PayloadType | 'object-spawned' | 'operation-result' | 'perform-action' + | 'physicsbridge-transforms-update' | 'rigidbody-add-force' | 'rigidbody-add-force-at-position' | 'rigidbody-add-relative-torque' diff --git a/packages/sdk/src/internal/payloads/physics.ts b/packages/sdk/src/internal/payloads/physics.ts index 2efcee357..065a9dc55 100644 --- a/packages/sdk/src/internal/payloads/physics.ts +++ b/packages/sdk/src/internal/payloads/physics.ts @@ -12,6 +12,9 @@ import { QuaternionLike, Vector3Like } from '../..'; +import { + PhysicsBridgeTransformUpdate +} from '../../actor/physics/physicsBridge'; /** * @hidden @@ -99,3 +102,8 @@ export type TriggerEventRaised = Payloads.Payload & { eventType: TriggerEventType; otherActorId: Guid; }; + +export type PhysicsBridgeUpdate = Payloads.Payload & { + type: 'physicsbridge-transforms-update'; + transforms: Partial; +} diff --git a/packages/sdk/src/internal/protocols/execution.ts b/packages/sdk/src/internal/protocols/execution.ts index 84169846d..68300f22a 100644 --- a/packages/sdk/src/internal/protocols/execution.ts +++ b/packages/sdk/src/internal/protocols/execution.ts @@ -115,6 +115,11 @@ export class Execution extends Protocol { } as ActionEvent); }; + /** @private */ + public 'recv-physicsbridge-transforms-update' = (payload: Payloads.PhysicsBridgeUpdate) => { + this.emit('protocol.physicsbridge-update-transforms', [payload.transforms]); + }; + /** @private */ public 'recv-collision-event-raised' = (payload: Payloads.CollisionEventRaised) => { this.emit('protocol.collision-event-raised', {