From a0f1a1774bc7e1e330337cbae582c62110695023 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Thu, 3 Aug 2023 14:28:34 -0500 Subject: [PATCH] Cleanup, fixed dynamic body logic in prep for physics toggle. --- src/diagram/diagramManager.ts | 30 +++++++++++++++++------------- src/util/appConfig.ts | 27 +++++++++++++++++---------- src/util/appConfigType.ts | 3 ++- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/diagram/diagramManager.ts b/src/diagram/diagramManager.ts index c068522..3811cff 100644 --- a/src/diagram/diagramManager.ts +++ b/src/diagram/diagramManager.ts @@ -46,6 +46,7 @@ export class DiagramManager { private readonly actionManager: ActionManager; private config: AppConfig; + constructor(scene: Scene, xr: WebXRExperienceHelper) { this.scene = scene; this.xr = xr; @@ -108,9 +109,7 @@ export class DiagramManager { if (event.parent) { mesh.parent = this.scene.getMeshById(event.parent); } - DiagramShapePhysics.applyPhysics(mesh, this.scene) - .setMotionType(PhysicsMotionType.DYNAMIC); - + DiagramShapePhysics.applyPhysics(mesh, this.scene, PhysicsMotionType.DYNAMIC); } private onDiagramEvent(event: DiagramEvent) { @@ -175,7 +174,7 @@ export class DiagramManager { class DiagramShapePhysics { private static logger: log.Logger = log.getLogger('DiagramShapePhysics'); - public static applyPhysics(mesh: AbstractMesh, scene: Scene): PhysicsBody { + public static applyPhysics(mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType): PhysicsBody { if (!mesh?.metadata?.template) { this.logger.error("applyPhysics: mesh.metadata.template is null", mesh); return null; @@ -205,22 +204,27 @@ class DiagramShapePhysics { const aggregate = new PhysicsAggregate(mesh, shapeType, {mass: mass, restitution: .02, friction: .9}, scene); - if (mesh.parent) { - aggregate.body - .setMotionType(PhysicsMotionType.ANIMATED); + const body = aggregate.body; + if (motionType) { + body + .setMotionType(motionType); } else { - aggregate.body - .setMotionType(PhysicsMotionType.DYNAMIC); + if (mesh.parent) { + body + .setMotionType(PhysicsMotionType.ANIMATED); + } else { + body + .setMotionType(PhysicsMotionType.DYNAMIC); + } } - aggregate.body.setCollisionCallbackEnabled(true); - aggregate.body.getCollisionObservable().add((event, state) => { + body.setCollisionCallbackEnabled(true); + body.getCollisionObservable().add((event, state) => { if (event.distance > .001 && !DiaSounds.instance.low.isPlaying) { this.logger.debug(event, state); DiaSounds.instance.low.play(); } }, -1, false, this); - const body = aggregate.body; - body.setMotionType(PhysicsMotionType.ANIMATED); + //body.setMotionType(PhysicsMotionType.ANIMATED); body.setLinearDamping(.95); body.setAngularDamping(.99); body.setGravityFactor(0); diff --git a/src/util/appConfig.ts b/src/util/appConfig.ts index cf741d4..86ddec3 100644 --- a/src/util/appConfig.ts +++ b/src/util/appConfig.ts @@ -1,5 +1,4 @@ -import {Angle, Vector3} from "@babylonjs/core"; -import round from "round"; +import {Vector3} from "@babylonjs/core"; import log from "loglevel"; import {IPersistenceManager} from "../integration/iPersistenceManager"; import {AppConfigType} from "./appConfigType"; @@ -15,6 +14,7 @@ export class AppConfig { private gridSnap = 1; private rotateSnap = 0; private createSnap = 0; + _physicsEnabled = true; private readonly defaultGridSnapIndex = 1; private persistenceManager: IPersistenceManager = null; private gridSnapArray: SnapValue[] = @@ -39,6 +39,15 @@ export class AppConfig { return this.gridSnapArray[this.gridSnap]; } + public get physicsEnabled(): boolean { + return this.physicsEnabled; + } + + public set phsyicsEnabled(val: boolean) { + this._physicsEnabled = val; + this.save(); + } + private static _config: AppConfig; public static get config() { @@ -113,12 +122,17 @@ export class AppConfig { { gridSnap: this.currentGridSnap.value, rotateSnap: this.currentRotateSnap.value, - createSnap: this.currentCreateSnap.value + createSnap: this.currentCreateSnap.value, + physicsEnabled: this._physicsEnabled }); } private configObserver(config: AppConfigType) { if (config) { + if (config.physicsEnabled && config.physicsEnabled != this._physicsEnabled) { + this._physicsEnabled = config.physicsEnabled; + this.logger.debug("Physics enabled changed to " + this._physicsEnabled); + } if (config.createSnap != this.currentCreateSnap.value || config.gridSnap != this.currentGridSnap.value || config.rotateSnap != this.currentRotateSnap.value) { @@ -138,11 +152,4 @@ export class AppConfig { this.logger.debug("Config not set"); } } - - private snapAngle(val: number): number { - const deg = Angle.FromRadians(val).degrees(); - const snappedDegrees = round(deg, this.currentRotateSnap.value); - this.logger.debug("deg", val, deg, snappedDegrees, this.currentRotateSnap.value); - return Angle.FromDegrees(snappedDegrees).radians(); - } } \ No newline at end of file diff --git a/src/util/appConfigType.ts b/src/util/appConfigType.ts index 36156e2..838d1f2 100644 --- a/src/util/appConfigType.ts +++ b/src/util/appConfigType.ts @@ -2,5 +2,6 @@ export type AppConfigType = { id?: number, gridSnap: number, rotateSnap: number, - createSnap: number + createSnap: number, + physicsEnabled: boolean } \ No newline at end of file