cleaned up a bit.

This commit is contained in:
Michael Mainguy 2023-08-22 14:55:49 -05:00
parent ad75d5b7f3
commit 23bce14635
3 changed files with 76 additions and 78 deletions

View File

@ -25,7 +25,7 @@ export class App {
constructor() { constructor() {
const logger = log.getLogger('App'); const logger = log.getLogger('App');
log.setDefaultLevel('debug'); log.setDefaultLevel('warn');
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
canvas.style.width = "100%"; canvas.style.width = "100%";
canvas.style.height = "100%"; canvas.style.height = "100%";

View File

@ -6,9 +6,7 @@ import {
InstancedMesh, InstancedMesh,
Mesh, Mesh,
Observable, Observable,
PhysicsAggregate,
PhysicsMotionType, PhysicsMotionType,
PhysicsShapeType,
PlaySoundAction, PlaySoundAction,
Scene, Scene,
Vector3 Vector3
@ -22,6 +20,7 @@ import {DiaSounds} from "../util/diaSounds";
import {AppConfig} from "../util/appConfig"; import {AppConfig} from "../util/appConfig";
import {TextLabel} from "./textLabel"; import {TextLabel} from "./textLabel";
import {Toolbox} from "../toolbox/toolbox"; import {Toolbox} from "../toolbox/toolbox";
import {DiagramShapePhysics} from "./diagramShapePhysics";
export class DiagramManager { export class DiagramManager {
@ -186,13 +185,10 @@ export class DiagramManager {
if (this.config.current.physicsEnabled) { if (this.config.current.physicsEnabled) {
DiagramShapePhysics.applyPhysics(this.sounds, mesh, this.scene, PhysicsMotionType.DYNAMIC); DiagramShapePhysics.applyPhysics(this.sounds, mesh, this.scene, PhysicsMotionType.DYNAMIC);
} }
} }
} }
switch (event.type) { switch (event.type) {
case DiagramEventType.CLEAR: case DiagramEventType.CLEAR:
break; break;
case DiagramEventType.DROPPED: case DiagramEventType.DROPPED:
break; break;
@ -252,76 +248,4 @@ export class DiagramManager {
break; 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);
}
} }

View File

@ -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);
}
}