From 3d15c07727ab22dd95fec7a54a76c833c68da24e Mon Sep 17 00:00:00 2001 From: Nikola Nikolic Date: Thu, 16 Apr 2020 14:32:57 +0200 Subject: [PATCH] Add physics bridge (#533) --- .../sdk/src/actor/physics/physicsBridge.ts | 19 ++++++++ packages/sdk/src/core/contextInternal.ts | 8 ++++ .../src/internal/adapters/multipeer/rules.ts | 47 +++++++++++++++++++ .../sdk/src/internal/payloads/payloads.ts | 1 + packages/sdk/src/internal/payloads/physics.ts | 8 ++++ .../sdk/src/internal/protocols/execution.ts | 5 ++ 6 files changed, 88 insertions(+) create mode 100644 packages/sdk/src/actor/physics/physicsBridge.ts 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 c2ccdb370..5ed4b9cae 100644 --- a/packages/sdk/src/core/contextInternal.ts +++ b/packages/sdk/src/core/contextInternal.ts @@ -35,6 +35,7 @@ import { Payloads, Protocols, } from '../internal'; +import { PhysicsBridgeTransformUpdate } from '../actor/physics/physicsBridge'; /** * @hidden @@ -267,6 +268,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)); @@ -403,6 +405,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 451ec8d86..54cd83d5d 100644 --- a/packages/sdk/src/internal/adapters/multipeer/rules.ts +++ b/packages/sdk/src/internal/adapters/multipeer/rules.ts @@ -776,6 +776,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 1ceb3082a..7062f0322 100644 --- a/packages/sdk/src/internal/payloads/payloads.ts +++ b/packages/sdk/src/internal/payloads/payloads.ts @@ -49,6 +49,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 2df7ecdf6..1d5e88a84 100644 --- a/packages/sdk/src/internal/protocols/execution.ts +++ b/packages/sdk/src/internal/protocols/execution.ts @@ -116,6 +116,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', {