From 23bce146353d49ee33392e64a8c98d150864078a Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Tue, 22 Aug 2023 14:55:49 -0500 Subject: [PATCH] cleaned up a bit. --- src/app.ts | 2 +- src/diagram/diagramManager.ts | 78 +----------------------------- src/diagram/diagramShapePhysics.ts | 74 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 78 deletions(-) create mode 100644 src/diagram/diagramShapePhysics.ts diff --git a/src/app.ts b/src/app.ts index a8c0f98..ecb6f3b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -25,7 +25,7 @@ export class App { constructor() { const logger = log.getLogger('App'); - log.setDefaultLevel('debug'); + log.setDefaultLevel('warn'); const canvas = document.createElement("canvas"); canvas.style.width = "100%"; canvas.style.height = "100%"; diff --git a/src/diagram/diagramManager.ts b/src/diagram/diagramManager.ts index 8056bb0..af45e61 100644 --- a/src/diagram/diagramManager.ts +++ b/src/diagram/diagramManager.ts @@ -6,9 +6,7 @@ import { InstancedMesh, Mesh, Observable, - PhysicsAggregate, PhysicsMotionType, - PhysicsShapeType, PlaySoundAction, Scene, Vector3 @@ -22,6 +20,7 @@ import {DiaSounds} from "../util/diaSounds"; import {AppConfig} from "../util/appConfig"; import {TextLabel} from "./textLabel"; import {Toolbox} from "../toolbox/toolbox"; +import {DiagramShapePhysics} from "./diagramShapePhysics"; export class DiagramManager { @@ -186,13 +185,10 @@ export class DiagramManager { if (this.config.current.physicsEnabled) { DiagramShapePhysics.applyPhysics(this.sounds, mesh, this.scene, PhysicsMotionType.DYNAMIC); } - } - } switch (event.type) { case DiagramEventType.CLEAR: - break; case DiagramEventType.DROPPED: break; @@ -252,76 +248,4 @@ export class DiagramManager { break; } } - -} - -class DiagramShapePhysics { - private static logger: log.Logger = log.getLogger('DiagramShapePhysics'); - - public static applyPhysics(sounds: DiaSounds, mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType) { - if (!mesh?.metadata?.template) { - this.logger.error("applyPhysics: mesh.metadata.template is null", mesh); - return; - } - if (mesh.metadata.template == '#connection-template') { - return; - } - if (!scene) { - this.logger.error("applyPhysics: mesh or scene is null"); - return; - } - - if (mesh.physicsBody) { - mesh.physicsBody.dispose(); - } - - let shapeType = PhysicsShapeType.BOX; - switch (mesh.metadata.template) { - case "#sphere-template": - shapeType = PhysicsShapeType.SPHERE; - break; - case "#cylinder-template": - shapeType = PhysicsShapeType.CYLINDER; - break; - case "#cone-template": - shapeType = PhysicsShapeType.CONVEX_HULL; - break; - - } - let mass = mesh.scaling.x * mesh.scaling.y * mesh.scaling.z * 10; - - const aggregate = new PhysicsAggregate(mesh, - shapeType, {mass: mass, restitution: .02, friction: .9}, scene); - const body = aggregate.body; - body.setLinearDamping(1.95); - body.setAngularDamping(1.99); - - if (motionType) { - body - .setMotionType(motionType); - } else { - if (mesh.parent) { - body - .setMotionType(PhysicsMotionType.ANIMATED); - } else { - body - .setMotionType(PhysicsMotionType.DYNAMIC); - } - } - body.setCollisionCallbackEnabled(true); - body.getCollisionObservable().add((event) => { - - if (event.impulse < 10 && event.impulse > 1) { - const sound = sounds.bounce; - sound.setVolume(event.impulse / 10); - sound.attachToMesh(mesh); - sound.play(); - } - }, -1, false, this); - //body.setMotionType(PhysicsMotionType.ANIMATED); - body.setLinearDamping(.95); - body.setAngularDamping(.99); - body.setGravityFactor(0); - - } } \ No newline at end of file diff --git a/src/diagram/diagramShapePhysics.ts b/src/diagram/diagramShapePhysics.ts new file mode 100644 index 0000000..59619f2 --- /dev/null +++ b/src/diagram/diagramShapePhysics.ts @@ -0,0 +1,74 @@ +import log from "loglevel"; +import {DiaSounds} from "../util/diaSounds"; +import {AbstractMesh, PhysicsAggregate, PhysicsMotionType, PhysicsShapeType, Scene} from "@babylonjs/core"; + +export class DiagramShapePhysics { + private static logger: log.Logger = log.getLogger('DiagramShapePhysics'); + + public static applyPhysics(sounds: DiaSounds, mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType) { + if (!mesh?.metadata?.template) { + this.logger.error("applyPhysics: mesh.metadata.template is null", mesh); + return; + } + if (mesh.metadata.template == '#connection-template') { + return; + } + if (!scene) { + this.logger.error("applyPhysics: mesh or scene is null"); + return; + } + + if (mesh.physicsBody) { + mesh.physicsBody.dispose(); + } + + let shapeType = PhysicsShapeType.BOX; + switch (mesh.metadata.template) { + case "#sphere-template": + shapeType = PhysicsShapeType.SPHERE; + break; + case "#cylinder-template": + shapeType = PhysicsShapeType.CYLINDER; + break; + case "#cone-template": + shapeType = PhysicsShapeType.CONVEX_HULL; + break; + + } + let mass = mesh.scaling.x * mesh.scaling.y * mesh.scaling.z * 10; + + const aggregate = new PhysicsAggregate(mesh, + shapeType, {mass: mass, restitution: .02, friction: .9}, scene); + const body = aggregate.body; + body.setLinearDamping(1.95); + body.setAngularDamping(1.99); + + if (motionType) { + body + .setMotionType(motionType); + } else { + if (mesh.parent) { + body + .setMotionType(PhysicsMotionType.ANIMATED); + } else { + body + .setMotionType(PhysicsMotionType.DYNAMIC); + } + } + body.setCollisionCallbackEnabled(true); + body.getCollisionObservable().add((event) => { + + if (event.impulse < 10 && event.impulse > 1) { + const sound = sounds.bounce; + sound.setVolume(event.impulse / 10); + sound.attachToMesh(mesh); + sound.play(); + } + }, -1, false, this); + //body.setMotionType(PhysicsMotionType.ANIMATED); + body.setLinearDamping(.95); + body.setAngularDamping(.99); + body.setGravityFactor(0); + + } +} \ No newline at end of file