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

View File

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

View File

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