Cleanup, fixed dynamic body logic in prep for physics toggle.

This commit is contained in:
Michael Mainguy 2023-08-03 14:28:34 -05:00
parent 5fbd8f5648
commit a0f1a1774b
3 changed files with 36 additions and 24 deletions

View File

@ -46,6 +46,7 @@ export class DiagramManager {
private readonly actionManager: ActionManager; private readonly actionManager: ActionManager;
private config: AppConfig; private config: AppConfig;
constructor(scene: Scene, xr: WebXRExperienceHelper) { constructor(scene: Scene, xr: WebXRExperienceHelper) {
this.scene = scene; this.scene = scene;
this.xr = xr; this.xr = xr;
@ -108,9 +109,7 @@ export class DiagramManager {
if (event.parent) { if (event.parent) {
mesh.parent = this.scene.getMeshById(event.parent); mesh.parent = this.scene.getMeshById(event.parent);
} }
DiagramShapePhysics.applyPhysics(mesh, this.scene) DiagramShapePhysics.applyPhysics(mesh, this.scene, PhysicsMotionType.DYNAMIC);
.setMotionType(PhysicsMotionType.DYNAMIC);
} }
private onDiagramEvent(event: DiagramEvent) { private onDiagramEvent(event: DiagramEvent) {
@ -175,7 +174,7 @@ export class DiagramManager {
class DiagramShapePhysics { class DiagramShapePhysics {
private static logger: log.Logger = log.getLogger('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) { if (!mesh?.metadata?.template) {
this.logger.error("applyPhysics: mesh.metadata.template is null", mesh); this.logger.error("applyPhysics: mesh.metadata.template is null", mesh);
return null; return null;
@ -205,22 +204,27 @@ class DiagramShapePhysics {
const aggregate = new PhysicsAggregate(mesh, const aggregate = new PhysicsAggregate(mesh,
shapeType, {mass: mass, restitution: .02, friction: .9}, scene); shapeType, {mass: mass, restitution: .02, friction: .9}, scene);
if (mesh.parent) { const body = aggregate.body;
aggregate.body if (motionType) {
.setMotionType(PhysicsMotionType.ANIMATED); body
.setMotionType(motionType);
} else { } else {
aggregate.body if (mesh.parent) {
.setMotionType(PhysicsMotionType.DYNAMIC); body
.setMotionType(PhysicsMotionType.ANIMATED);
} else {
body
.setMotionType(PhysicsMotionType.DYNAMIC);
}
} }
aggregate.body.setCollisionCallbackEnabled(true); body.setCollisionCallbackEnabled(true);
aggregate.body.getCollisionObservable().add((event, state) => { body.getCollisionObservable().add((event, state) => {
if (event.distance > .001 && !DiaSounds.instance.low.isPlaying) { if (event.distance > .001 && !DiaSounds.instance.low.isPlaying) {
this.logger.debug(event, state); this.logger.debug(event, state);
DiaSounds.instance.low.play(); DiaSounds.instance.low.play();
} }
}, -1, false, this); }, -1, false, this);
const body = aggregate.body; //body.setMotionType(PhysicsMotionType.ANIMATED);
body.setMotionType(PhysicsMotionType.ANIMATED);
body.setLinearDamping(.95); body.setLinearDamping(.95);
body.setAngularDamping(.99); body.setAngularDamping(.99);
body.setGravityFactor(0); body.setGravityFactor(0);

View File

@ -1,5 +1,4 @@
import {Angle, Vector3} from "@babylonjs/core"; import {Vector3} from "@babylonjs/core";
import round from "round";
import log from "loglevel"; import log from "loglevel";
import {IPersistenceManager} from "../integration/iPersistenceManager"; import {IPersistenceManager} from "../integration/iPersistenceManager";
import {AppConfigType} from "./appConfigType"; import {AppConfigType} from "./appConfigType";
@ -15,6 +14,7 @@ export class AppConfig {
private gridSnap = 1; private gridSnap = 1;
private rotateSnap = 0; private rotateSnap = 0;
private createSnap = 0; private createSnap = 0;
_physicsEnabled = true;
private readonly defaultGridSnapIndex = 1; private readonly defaultGridSnapIndex = 1;
private persistenceManager: IPersistenceManager = null; private persistenceManager: IPersistenceManager = null;
private gridSnapArray: SnapValue[] = private gridSnapArray: SnapValue[] =
@ -39,6 +39,15 @@ export class AppConfig {
return this.gridSnapArray[this.gridSnap]; 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; private static _config: AppConfig;
public static get config() { public static get config() {
@ -113,12 +122,17 @@ export class AppConfig {
{ {
gridSnap: this.currentGridSnap.value, gridSnap: this.currentGridSnap.value,
rotateSnap: this.currentRotateSnap.value, rotateSnap: this.currentRotateSnap.value,
createSnap: this.currentCreateSnap.value createSnap: this.currentCreateSnap.value,
physicsEnabled: this._physicsEnabled
}); });
} }
private configObserver(config: AppConfigType) { private configObserver(config: AppConfigType) {
if (config) { 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 || if (config.createSnap != this.currentCreateSnap.value ||
config.gridSnap != this.currentGridSnap.value || config.gridSnap != this.currentGridSnap.value ||
config.rotateSnap != this.currentRotateSnap.value) { config.rotateSnap != this.currentRotateSnap.value) {
@ -138,11 +152,4 @@ export class AppConfig {
this.logger.debug("Config not set"); 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();
}
} }

View File

@ -2,5 +2,6 @@ export type AppConfigType = {
id?: number, id?: number,
gridSnap: number, gridSnap: number,
rotateSnap: number, rotateSnap: number,
createSnap: number createSnap: number,
physicsEnabled: boolean
} }