From 6544cd866d15884347d33c9a02464c3d9a240de9 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Thu, 3 Aug 2023 14:56:25 -0500 Subject: [PATCH] Cleanup, fixed dynamic body logic in prep for physics toggle. --- src/controllers/base.ts | 7 +++++++ src/diagram/diagramManager.ts | 12 +++++++----- src/util/appConfig.ts | 36 ++++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/controllers/base.ts b/src/controllers/base.ts index d076d90..31f40b1 100644 --- a/src/controllers/base.ts +++ b/src/controllers/base.ts @@ -14,6 +14,7 @@ import {DiagramManager} from "../diagram/diagramManager"; import {DiagramEvent, DiagramEventType} from "../diagram/diagramEntity"; import log from "loglevel"; import {Controllers} from "./controllers"; +import {AppConfig} from "../util/appConfig"; export class Base { static stickVector = Vector3.Zero(); @@ -191,6 +192,8 @@ export class Base { if (parent) { this.grabbedMeshParentId = null; parent.dispose(); + } else { + mesh.setParent(null); } } } @@ -205,6 +208,10 @@ export class Base { } this.reparent(mesh); + if (!mesh.physicsBody) { + mesh.position = AppConfig.config.snapGridVal(mesh.position); + mesh.rotation = AppConfig.config.snapRotateVal(mesh.rotation); + } this.previousParentId = null; this.previousScaling = null; this.previousRotation = null; diff --git a/src/diagram/diagramManager.ts b/src/diagram/diagramManager.ts index 3811cff..125336c 100644 --- a/src/diagram/diagramManager.ts +++ b/src/diagram/diagramManager.ts @@ -7,7 +7,6 @@ import { Mesh, Observable, PhysicsAggregate, - PhysicsBody, PhysicsMotionType, PhysicsShapeType, PlaySoundAction, @@ -174,14 +173,17 @@ export class DiagramManager { class DiagramShapePhysics { private static logger: log.Logger = log.getLogger('DiagramShapePhysics'); - public static applyPhysics(mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType): PhysicsBody { + public static applyPhysics(mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType) { if (!mesh?.metadata?.template) { this.logger.error("applyPhysics: mesh.metadata.template is null", mesh); - return null; + return; } if (!scene) { this.logger.error("applyPhysics: mesh or scene is null"); - return null; + return; + } + if (!AppConfig.config.physicsEnabled) { + return; } if (mesh.physicsBody) { mesh.physicsBody.dispose(); @@ -228,6 +230,6 @@ class DiagramShapePhysics { body.setLinearDamping(.95); body.setAngularDamping(.99); body.setGravityFactor(0); - return aggregate.body; + } } \ No newline at end of file diff --git a/src/util/appConfig.ts b/src/util/appConfig.ts index 86ddec3..88495c9 100644 --- a/src/util/appConfig.ts +++ b/src/util/appConfig.ts @@ -1,5 +1,6 @@ -import {Vector3} from "@babylonjs/core"; +import {Angle, Vector3} from "@babylonjs/core"; import log from "loglevel"; +import round from "round"; import {IPersistenceManager} from "../integration/iPersistenceManager"; import {AppConfigType} from "./appConfigType"; @@ -14,7 +15,7 @@ export class AppConfig { private gridSnap = 1; private rotateSnap = 0; private createSnap = 0; - _physicsEnabled = true; + _physicsEnabled = false; private readonly defaultGridSnapIndex = 1; private persistenceManager: IPersistenceManager = null; private gridSnapArray: SnapValue[] = @@ -40,7 +41,7 @@ export class AppConfig { } public get physicsEnabled(): boolean { - return this.physicsEnabled; + return this._physicsEnabled; } public set phsyicsEnabled(val: boolean) { @@ -117,6 +118,35 @@ export class AppConfig { return this.rotateSnapArray; } + public snapGridVal(value: Vector3): Vector3 { + if (this.currentGridSnapIndex == 0) { + return value; + } + const position = value.clone(); + position.x = round(position.x, this.currentGridSnap.value); + position.y = round(position.y, this.currentGridSnap.value); + position.z = round(position.z, this.currentGridSnap.value); + return position; + } + + public snapRotateVal(value: Vector3): Vector3 { + if (this.currentRotateSnapIndex == 0) { + return value; + } + const rotation = new Vector3(); + rotation.x = this.snapAngle(value.x); + rotation.y = this.snapAngle(value.y); + rotation.z = this.snapAngle(value.z); + return rotation; + } + + 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(); + } + private save() { this.persistenceManager.setConfig( {