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

This commit is contained in:
Michael Mainguy 2023-08-03 14:56:25 -05:00
parent a0f1a1774b
commit 6544cd866d
3 changed files with 47 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import {DiagramManager} from "../diagram/diagramManager";
import {DiagramEvent, DiagramEventType} from "../diagram/diagramEntity"; import {DiagramEvent, DiagramEventType} from "../diagram/diagramEntity";
import log from "loglevel"; import log from "loglevel";
import {Controllers} from "./controllers"; import {Controllers} from "./controllers";
import {AppConfig} from "../util/appConfig";
export class Base { export class Base {
static stickVector = Vector3.Zero(); static stickVector = Vector3.Zero();
@ -191,6 +192,8 @@ export class Base {
if (parent) { if (parent) {
this.grabbedMeshParentId = null; this.grabbedMeshParentId = null;
parent.dispose(); parent.dispose();
} else {
mesh.setParent(null);
} }
} }
} }
@ -205,6 +208,10 @@ export class Base {
} }
this.reparent(mesh); this.reparent(mesh);
if (!mesh.physicsBody) {
mesh.position = AppConfig.config.snapGridVal(mesh.position);
mesh.rotation = AppConfig.config.snapRotateVal(mesh.rotation);
}
this.previousParentId = null; this.previousParentId = null;
this.previousScaling = null; this.previousScaling = null;
this.previousRotation = null; this.previousRotation = null;

View File

@ -7,7 +7,6 @@ import {
Mesh, Mesh,
Observable, Observable,
PhysicsAggregate, PhysicsAggregate,
PhysicsBody,
PhysicsMotionType, PhysicsMotionType,
PhysicsShapeType, PhysicsShapeType,
PlaySoundAction, PlaySoundAction,
@ -174,14 +173,17 @@ 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, motionType?: PhysicsMotionType): PhysicsBody { public static applyPhysics(mesh: AbstractMesh, scene: Scene, motionType?: PhysicsMotionType) {
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;
} }
if (!scene) { if (!scene) {
this.logger.error("applyPhysics: mesh or scene is null"); this.logger.error("applyPhysics: mesh or scene is null");
return null; return;
}
if (!AppConfig.config.physicsEnabled) {
return;
} }
if (mesh.physicsBody) { if (mesh.physicsBody) {
mesh.physicsBody.dispose(); mesh.physicsBody.dispose();
@ -228,6 +230,6 @@ class DiagramShapePhysics {
body.setLinearDamping(.95); body.setLinearDamping(.95);
body.setAngularDamping(.99); body.setAngularDamping(.99);
body.setGravityFactor(0); body.setGravityFactor(0);
return aggregate.body;
} }
} }

View File

@ -1,5 +1,6 @@
import {Vector3} from "@babylonjs/core"; import {Angle, Vector3} from "@babylonjs/core";
import log from "loglevel"; import log from "loglevel";
import round from "round";
import {IPersistenceManager} from "../integration/iPersistenceManager"; import {IPersistenceManager} from "../integration/iPersistenceManager";
import {AppConfigType} from "./appConfigType"; import {AppConfigType} from "./appConfigType";
@ -14,7 +15,7 @@ export class AppConfig {
private gridSnap = 1; private gridSnap = 1;
private rotateSnap = 0; private rotateSnap = 0;
private createSnap = 0; private createSnap = 0;
_physicsEnabled = true; _physicsEnabled = false;
private readonly defaultGridSnapIndex = 1; private readonly defaultGridSnapIndex = 1;
private persistenceManager: IPersistenceManager = null; private persistenceManager: IPersistenceManager = null;
private gridSnapArray: SnapValue[] = private gridSnapArray: SnapValue[] =
@ -40,7 +41,7 @@ export class AppConfig {
} }
public get physicsEnabled(): boolean { public get physicsEnabled(): boolean {
return this.physicsEnabled; return this._physicsEnabled;
} }
public set phsyicsEnabled(val: boolean) { public set phsyicsEnabled(val: boolean) {
@ -117,6 +118,35 @@ export class AppConfig {
return this.rotateSnapArray; 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() { private save() {
this.persistenceManager.setConfig( this.persistenceManager.setConfig(
{ {