From 8a7fcf3082a6d057b6e6ef25d2f60fdaa006cca7 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 16 Dec 2022 15:59:45 +0100 Subject: [PATCH 1/3] Physics V2 plugin iteration --- packages/dev/core/src/Debug/physicsViewer.ts | 55 +++++++++++++++++++ .../src/Physics/v2/IPhysicsEnginePlugin.ts | 8 ++- packages/dev/core/src/Physics/v2/index.ts | 1 + .../dev/core/src/Physics/v2/physicsBody.ts | 35 ++++++++++-- .../dev/core/src/Physics/v2/physicsEngine.ts | 32 ++++++++--- .../dev/core/src/Physics/v2/physicsShape.ts | 6 +- 6 files changed, 119 insertions(+), 18 deletions(-) diff --git a/packages/dev/core/src/Debug/physicsViewer.ts b/packages/dev/core/src/Debug/physicsViewer.ts index 6f70bb70a57..0dcbda14b1f 100644 --- a/packages/dev/core/src/Debug/physicsViewer.ts +++ b/packages/dev/core/src/Debug/physicsViewer.ts @@ -17,6 +17,8 @@ import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder"; import type { ICreateCapsuleOptions } from "../Meshes/Builders/capsuleBuilder"; import { CreateCapsule } from "../Meshes/Builders/capsuleBuilder"; import { Logger } from "../Misc/logger"; +import type { PhysicsBody } from "../Physics/v2/physicsBody"; +import { VertexData } from "../Meshes/mesh.vertexData"; /** * Used to show the physics impostor around the specific mesh @@ -27,10 +29,16 @@ export class PhysicsViewer { /** @internal */ protected _meshes: Array> = []; /** @internal */ + protected _bodies: Array> = []; + /** @internal */ + protected _bodyMeshes: Array> = []; + /** @internal */ protected _scene: Nullable; /** @internal */ protected _numMeshes = 0; /** @internal */ + protected _numBodies = 0; + /** @internal */ protected _physicsEnginePlugin: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null; private _renderFunction: () => void; private _utilityLayer: Nullable; @@ -122,6 +130,36 @@ export class PhysicsViewer { return debugMesh; } + /** + * + */ + public showBody(body: PhysicsBody): Nullable { + if (!this._scene) { + return null; + } + + for (let i = 0; i < this._numBodies; i++) { + if (this._bodies[i] == body) { + return null; + } + } + + const debugMesh = this._getDebugBodyMesh(body); + + if (debugMesh) { + this._bodies[this._numBodies] = body; + this._bodyMeshes[this._numBodies] = debugMesh; + + if (this._numBodies === 0) { + this._renderFunction = this._updateDebugMeshes.bind(this); + this._scene.registerBeforeRender(this._renderFunction); + } + + this._numBodies++; + } + + return debugMesh; + } /** * Hides a specified physic impostor * @param impostor defines the impostor to hide @@ -331,6 +369,23 @@ export class PhysicsViewer { return mesh; } + private _getDebugBodyMesh(body: PhysicsBody): Nullable { + if (!this._utilityLayer) { + return null; + } + + const utilityLayerScene = this._utilityLayer.utilityLayerScene; + + const mesh = new Mesh("custom", utilityLayerScene); + const vertexData = new VertexData(); + const geometry = body.getGeometry() as any; + vertexData.positions = geometry.positions; + vertexData.indices = geometry.indices; + vertexData.applyToMesh(mesh); + mesh.material = this._getDebugMaterial(utilityLayerScene); + return mesh; + } + /** Releases all resources */ public dispose() { const count = this._numMeshes; diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index ed9cf1aa8a9..d5503c2b3b3 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -7,6 +7,7 @@ import type { PhysicsConstraint } from "./physicsConstraint"; import type { BoundingBox } from "../../Culling/boundingBox"; import type { TransformNode } from "../../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; +import type { Mesh } from "../../Meshes/mesh"; /** @internal */ export enum ConstraintAxisLimitMode { @@ -45,6 +46,7 @@ export enum ShapeType { CONVEX_HULL, CONTAINER, MESH, + HEIGHTFIELD, } /** @internal */ @@ -109,11 +111,12 @@ export interface IPhysicsEnginePlugin { setGravity(gravity: Vector3): void; setTimeStep(timeStep: number): void; getTimeStep(): number; - executeStep(delta: number): void; //not forgetting pre and post events + executeStep(delta: number, bodies: Array): void; //not forgetting pre and post events getPluginVersion(): number; // body - initBody(body: PhysicsBody): void; + initBody(body: PhysicsBody, position: Vector3, orientation: Quaternion): void; + initBodyInstances(body: PhysicsBody, mesh: Mesh): void; setShape(body: PhysicsBody, shape: PhysicsShape): void; getShape(body: PhysicsBody): PhysicsShape; setFilterGroup(body: PhysicsBody, group: number): void; @@ -131,6 +134,7 @@ export interface IPhysicsEnginePlugin { applyImpulse(body: PhysicsBody, location: Vector3, impulse: Vector3): void; setAngularVelocity(body: PhysicsBody, angVel: Vector3): void; getAngularVelocityToRef(body: PhysicsBody, angVel: Vector3): void; + getBodyGeometry(body: PhysicsBody): {}; disposeBody(body: PhysicsBody): void; // shape diff --git a/packages/dev/core/src/Physics/v2/index.ts b/packages/dev/core/src/Physics/v2/index.ts index 14c3f37c93a..ae80bfdc5ea 100644 --- a/packages/dev/core/src/Physics/v2/index.ts +++ b/packages/dev/core/src/Physics/v2/index.ts @@ -5,3 +5,4 @@ export * from "./physicsShape"; export * from "./physicsConstraint"; export * from "./physicsMaterial"; export * from "./physicsAggregate"; +export * from "./Plugins/index"; diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index ab7daf93465..08377e056df 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -1,7 +1,10 @@ import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEnginePlugin"; import type { PhysicsShape } from "./physicsShape"; import type { Vector3 } from "../../Maths/math.vector"; +import { Quaternion } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; +import type { PhysicsEngine } from "./physicsEngine"; +import type { Mesh, TransformNode } from "../../Meshes"; /** * @@ -10,19 +13,25 @@ import type { Scene } from "../../scene"; export class PhysicsBody { /** @internal */ public _pluginData: any = undefined; - + /** + * + */ + public _pluginDataInstances: Array = []; private _physicsPlugin: IPhysicsEnginePlugin; - + /** + * + */ + node: TransformNode; /** * * @param scene * @returns */ - constructor(scene: Scene) { + constructor(node: TransformNode, scene: Scene) { if (!scene) { return; } - const physicsEngine = scene.getPhysicsEngine(); + const physicsEngine = scene.getPhysicsEngine() as PhysicsEngine; if (!physicsEngine) { throw new Error("No Physics Engine available."); } @@ -35,7 +44,19 @@ export class PhysicsBody { } this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin; - this._physicsPlugin.initBody(this); + if (!node.rotationQuaternion) { + node.rotationQuaternion = Quaternion.FromEulerAngles(node.rotation.x, node.rotation.y, node.rotation.z); + } + // instances? + const m = node as Mesh; + if (m.hasThinInstances) { + this._physicsPlugin.initBodyInstances(this, m); + } else { + // single instance + this._physicsPlugin.initBody(this, node.position, node.rotationQuaternion); + } + this.node = node; + physicsEngine.addBody(this); } /** * @@ -174,6 +195,10 @@ export class PhysicsBody { this._physicsPlugin.applyImpulse(this, location, impulse); } + public getGeometry(): {} { + return this._physicsPlugin.getBodyGeometry(this); + } + /** * */ diff --git a/packages/dev/core/src/Physics/v2/physicsEngine.ts b/packages/dev/core/src/Physics/v2/physicsEngine.ts index cf625dec093..c0cd6a0ff9c 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngine.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngine.ts @@ -4,6 +4,7 @@ import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; import { PhysicsRaycastResult } from "../physicsRaycastResult"; import { _WarnImport } from "../../Misc/devTools"; +import type { PhysicsBody } from "./physicsBody"; /** * Class used to control physics engine @@ -12,13 +13,7 @@ import { _WarnImport } from "../../Misc/devTools"; /** @internal */ export class PhysicsEngine implements IPhysicsEngine { /** @internal */ - /** - * Global value used to control the smallest number supported by the simulation - */ - public static Epsilon = 0.001; - - //private _impostors: Array = []; - //private _joints: Array = []; + private _physicsBodies: Array = []; private _subTimeStep: number = 0; //private _uniqueIdCounter = 0; @@ -27,6 +22,10 @@ export class PhysicsEngine implements IPhysicsEngine { */ public gravity: Vector3; + /** + * + * @returns physics plugin version + */ public getPluginVersion(): number { return this._physicsPlugin.getPluginVersion(); } @@ -134,7 +133,24 @@ export class PhysicsEngine implements IPhysicsEngine { delta = 1.0 / 60.0; } - this._physicsPlugin.executeStep(delta); + this._physicsPlugin.executeStep(delta, this._physicsBodies); + } + + /** + * + * @param body + */ + public addBody(physicsBody: PhysicsBody): void { + this._physicsBodies.push(physicsBody); + } + /** + * + */ + public removeBody(physicsBody: PhysicsBody): void { + const index = this._physicsBodies.indexOf(physicsBody); + if (index > -1) { + /*const removed =*/ this._physicsBodies.splice(index, 1); + } } /** diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index 6d6a311f682..e2541af96be 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -76,8 +76,8 @@ export class PhysicsShape { * * @param materialId */ - public setMaterial(materialId: PhysicsMaterial): void { - this._physicsPlugin.setMaterial(this, materialId); + public setMaterial(material: PhysicsMaterial): void { + this._physicsPlugin.setMaterial(this, material); } /** @@ -156,7 +156,7 @@ export class PhysicsShapeSphere extends PhysicsShape { * @param scene */ constructor(center: Vector3, radius: number, scene: Scene) { - super(ShapeType.BOX, { center: center, radius: radius }, scene); + super(ShapeType.SPHERE, { center: center, radius: radius }, scene); } } From eb5f82fdb3974157a9859b3119dba1b25abddfc1 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 16 Dec 2022 17:00:51 +0100 Subject: [PATCH 2/3] linting and build fix --- packages/dev/core/src/Debug/physicsViewer.ts | 4 ++-- packages/dev/core/src/Physics/v2/index.ts | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/dev/core/src/Debug/physicsViewer.ts b/packages/dev/core/src/Debug/physicsViewer.ts index 0dcbda14b1f..3297e0d4855 100644 --- a/packages/dev/core/src/Debug/physicsViewer.ts +++ b/packages/dev/core/src/Debug/physicsViewer.ts @@ -131,7 +131,7 @@ export class PhysicsViewer { } /** - * + * */ public showBody(body: PhysicsBody): Nullable { if (!this._scene) { @@ -373,7 +373,7 @@ export class PhysicsViewer { if (!this._utilityLayer) { return null; } - + const utilityLayerScene = this._utilityLayer.utilityLayerScene; const mesh = new Mesh("custom", utilityLayerScene); diff --git a/packages/dev/core/src/Physics/v2/index.ts b/packages/dev/core/src/Physics/v2/index.ts index ae80bfdc5ea..14c3f37c93a 100644 --- a/packages/dev/core/src/Physics/v2/index.ts +++ b/packages/dev/core/src/Physics/v2/index.ts @@ -5,4 +5,3 @@ export * from "./physicsShape"; export * from "./physicsConstraint"; export * from "./physicsMaterial"; export * from "./physicsAggregate"; -export * from "./Plugins/index"; From 72085a3b81eb71097999d44f1ae24ac2e0bf8422 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 3 Jan 2023 17:13:36 +0100 Subject: [PATCH 3/3] PR feedback --- packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index 5a6e687847c..f5e0419ab9e 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -111,7 +111,7 @@ export interface IPhysicsEnginePluginV2 { setGravity(gravity: Vector3): void; setTimeStep(timeStep: number): void; getTimeStep(): number; - executeStep(delta: number, bodies: Array): void; //not forgetting pre and post events + executeStep(delta: number, bodies: Array): void; //not forgetting pre and post events getPluginVersion(): number; // body