From bd9e60ce41882cf6a75944da8a505bd1cec366d0 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 23 Nov 2022 19:05:56 +0100 Subject: [PATCH 01/28] PhysicsPlugin refactor --- .../dev/core/src/Physics2/IPhysicsEngine.ts | 292 ++++++++++++++++++ packages/dev/core/src/Physics2/index.ts | 8 + packages/dev/core/src/Physics2/physicsBody.ts | 157 ++++++++++ .../dev/core/src/Physics2/physicsEngine.ts | 254 +++++++++++++++ .../dev/core/src/Physics2/physicsImpostor.ts | 17 + .../dev/core/src/Physics2/physicsJoint.ts | 212 +++++++++++++ .../dev/core/src/Physics2/physicsMaterial.ts | 52 ++++ .../core/src/Physics2/physicsRaycastResult.ts | 119 +++++++ .../dev/core/src/Physics2/physicsShape.ts | 109 +++++++ 9 files changed, 1220 insertions(+) create mode 100644 packages/dev/core/src/Physics2/IPhysicsEngine.ts create mode 100644 packages/dev/core/src/Physics2/index.ts create mode 100644 packages/dev/core/src/Physics2/physicsBody.ts create mode 100644 packages/dev/core/src/Physics2/physicsEngine.ts create mode 100644 packages/dev/core/src/Physics2/physicsImpostor.ts create mode 100644 packages/dev/core/src/Physics2/physicsJoint.ts create mode 100644 packages/dev/core/src/Physics2/physicsMaterial.ts create mode 100644 packages/dev/core/src/Physics2/physicsRaycastResult.ts create mode 100644 packages/dev/core/src/Physics2/physicsShape.ts diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics2/IPhysicsEngine.ts new file mode 100644 index 00000000000..0d035c01e0a --- /dev/null +++ b/packages/dev/core/src/Physics2/IPhysicsEngine.ts @@ -0,0 +1,292 @@ +import type { Vector3, Quaternion } from "../Maths/math.vector"; +import type { AbstractMesh } from "../Meshes/abstractMesh"; +import type { PhysicsImpostor } from "./physicsImpostor"; +import type { PhysicsRaycastResult } from "./physicsRaycastResult"; +import type { PhysicsBody } from "./physicsBody"; +import type { PhysicsShape } from "./physicsShape"; +import type { PhysicsJoint } from "./physicsJoint"; +import type { BoundingBox } from "../culling/boundingBox"; +import type { TransformNode } from "../Meshes/transformNode"; +import type { PhysicsMaterial } from "./physicsMaterial"; +/** + * Interface used to describe a physics joint + */ +export interface PhysicsImpostorJoint { + /** Defines the main impostor to which the joint is linked */ + mainImpostor: PhysicsImpostor; + /** Defines the impostor that is connected to the main impostor using this joint */ + connectedImpostor: PhysicsImpostor; + /** Defines the joint itself */ + joint: PhysicsJoint; +} + +export enum JointAxisLimitMode { + FREE, + LIMITED, + LOCKED, +} + +export enum JointAxis { + LINEAR_X, + LINEAR_Y, + LINEAR_Z, + ANGULAR_X, + ANGULAR_Y, + ANGULAR_Z, + LINEAR_DISTANCE, +} +export enum JointType { + BALL_AND_SOCKET, + DISTANCE, + HINGE, + SLIDER, + LOCK, +} + +export enum ShapeType { + SPHERE, + CAPSULE, + CYLINDER, + BOX, + CONVEX_HULL, + MESH, +} + +export enum JointMotorType { + NONE, + VELOCITY, + POSITION, +} +/** + * + */ +export interface MassProperties { + /** + * + */ + centerOfMass: Vector3; + /** + * + */ + mass: number; + /** + * + */ + intertia: Vector3; + /** + * + */ + inertiaOrientation: Quaternion; +} + +/** @internal */ +export interface IPhysicsEnginePlugin { + /** + * + */ + world: any; + /** + * + */ + name: string; + setGravity(gravity: Vector3): void; + setTimeStep(timeStep: number): void; + getTimeStep(): number; + executeStep(delta: number, impostors: Array): void; //not forgetting pre and post events + + // body + createBody(): PhysicsBody; + setShape(body: PhysicsBody, shape: PhysicsShape): void; + getShape(body: PhysicsBody): PhysicsShape; + setFilterGroup(body: PhysicsBody, group: number): void; + getFilterGroup(body: PhysicsBody): number; + setEventMask(body: PhysicsBody, eventMask: number): void; + getEventMask(body: PhysicsBody): number; + setMassProperties(body: PhysicsBody, massProps: MassProperties): void; + getMassProperties(body: PhysicsBody): MassProperties; + setLinearDamping(body: PhysicsBody, damping: number): void; + getLinearDamping(body: PhysicsBody): number; + setAngularDamping(body: PhysicsBody, damping: number): void; + getAngularDamping(body: PhysicsBody): number; + setLinearVelocity(body: PhysicsBody, linVel: Vector3): void; + getLinearVelocity(body: PhysicsBody): Vector3; + applyImpulse(body: PhysicsBody, location: Vector3, impulse: Vector3): void; + setAngularVelocity(body: PhysicsBody, angVel: Vector3): void; + getAngularVelocity(body: PhysicsBody): Vector3; + disposeBody(body: PhysicsBody): void; + + // shape + createShapeSphere(center: Vector3, radius: number): PhysicsShape; + createShapeCapsule(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; + createShapeCylinder(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; + createShapeBox(center: Vector3, rotation: Quaternion, extents: Vector3): PhysicsShape; + createShapeConvexHull(mesh: AbstractMesh): PhysicsShape; + createShapeMesh(mesh: AbstractMesh): PhysicsShape; + createShapeContainer(): PhysicsShape; + + setFilterLayer(shape: PhysicsShape, layer: number): void; + getFilterLayer(shape: PhysicsShape): number; + setMaterial(shape: PhysicsShape, material: PhysicsMaterial): void; + getMaterial(shape: PhysicsShape): PhysicsMaterial; + setDensity(shape: PhysicsShape, density: number): void; + getDensity(shape: PhysicsShape): number; + addChild(shape: PhysicsShape, newChild: PhysicsShape, childTransform: TransformNode): void; + removeChild(shape: PhysicsShape, childIndex: number): void; + getNumChildren(shape: PhysicsShape): number; + getBoundingBox(shape: PhysicsShape): BoundingBox; + disposeShape(shape: PhysicsShape): void; + + // material + setFriction(material: PhysicsMaterial, friction: number): void; + getFriction(material: PhysicsMaterial): number; + setRestitution(material: PhysicsMaterial, restitution: number): void; + getRestitution(material: PhysicsMaterial): number; + disposeMaterial(material: PhysicsMaterial): void; + + // joint + createJointBallAndSocket(): PhysicsJoint; + createJointDistance(): PhysicsJoint; + createJointHinge(): PhysicsJoint; + createJointSlider(): PhysicsJoint; + createJointLock(): PhysicsJoint; + setParentBody(joint: PhysicsJoint, body: PhysicsBody): void; + getParentBody(joint: PhysicsJoint): PhysicsBody; + setChildBody(joint: PhysicsJoint, body: PhysicsBody): void; + getChildBody(joint: PhysicsJoint): PhysicsBody; + setAnchorInParent(joint: PhysicsJoint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; + setAnchorInChild(joint: PhysicsJoint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; + setEnabled(joint: PhysicsJoint, isEnabled: boolean): void; + getEnabled(joint: PhysicsJoint): boolean; + setCollisionsEnabled(joint: PhysicsJoint, isEnabled: boolean): void; + getCollisionsEnabled(joint: PhysicsJoint): boolean; + setAxisFriction(joint: PhysicsJoint, axis: JointAxis, friction: number): void; + getAxisFriction(joint: PhysicsJoint, axis: JointAxis): number; + setAxisMode(joint: PhysicsJoint, axis: JointAxis, limitMode: JointAxisLimitMode): void; + getAxisMode(joint: PhysicsJoint, axis: JointAxis): JointAxisLimitMode; + setAxisMinLimit(joint: PhysicsJoint, axis: JointAxis, minLimit: number): void; + getAxisMinLimit(joint: PhysicsJoint, axis: JointAxis): number; + setAxisMaxLimit(joint: PhysicsJoint, axis: JointAxis, limit: number): void; + getAxisMaxLimit(joint: PhysicsJoint, axis: JointAxis): number; + setAxisMotorType(joint: PhysicsJoint, axis: JointAxis, motorType: JointMotorType): void; + getAxisMotorType(joint: PhysicsJoint, axis: JointAxis): JointMotorType; + setAxisMotorTarget(joint: PhysicsJoint, axis: JointAxis, target: number): void; + getAxisMotorTarget(joint: PhysicsJoint, axis: JointAxis): number; + setAxisMotorMaxForce(joint: PhysicsJoint, axis: JointAxis, maxForce: number): void; + getAxisMotorMaxForce(joint: PhysicsJoint, axis: JointAxis): number; + disposeJoint(joint: PhysicsJoint): void; + + // raycast + raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; + + dispose(): void; +} + +/** + * Interface used to define a physics engine + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +export interface IPhysicsEngine { + /** + * Gets the gravity vector used by the simulation + */ + gravity: Vector3; + + /** + * Sets the gravity vector used by the simulation + * @param gravity defines the gravity vector to use + */ + setGravity(gravity: Vector3): void; + + /** + * Set the time step of the physics engine. + * Default is 1/60. + * To slow it down, enter 1/600 for example. + * To speed it up, 1/30 + * @param newTimeStep the new timestep to apply to this world. + */ + setTimeStep(newTimeStep: number): void; + + /** + * Get the time step of the physics engine. + * @returns the current time step + */ + getTimeStep(): number; + + /** + * Set the sub time step of the physics engine. + * Default is 0 meaning there is no sub steps + * To increase physics resolution precision, set a small value (like 1 ms) + * @param subTimeStep defines the new sub timestep used for physics resolution. + */ + setSubTimeStep(subTimeStep: number): void; + + /** + * Get the sub time step of the physics engine. + * @returns the current sub time step + */ + getSubTimeStep(): number; + + /** + * Release all resources + */ + dispose(): void; + + /** + * Gets the name of the current physics plugin + * @returns the name of the plugin + */ + getPhysicsPluginName(): string; + + //*********************************************************** + createShapeSphere(center: Vector3, radius: number): PhysicsShape; + createShapeCapsule(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; + createShapeCylinder(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; + createShapeBox(center: Vector3, rotation: Quaternion, extents: Vector3): PhysicsShape; + createShapeConvexHull(mesh: AbstractMesh): PhysicsShape; + createShapeMesh(mesh: AbstractMesh): PhysicsShape; + createShapeContainer(): PhysicsShape; + + createJointBallAndSocket(): PhysicsJoint; + createJointDistance(): PhysicsJoint; + createJointHinge(): PhysicsJoint; + createJointSlider(): PhysicsJoint; + createJointLock(): PhysicsJoint; + + createBody(): PhysicsBody; + + addBody(body: PhysicsBody): void; + removeBody(body: PhysicsBody): void; + + // Helpers + getBodies(): Array; + addImpostor(impostor: PhysicsImpostor): void; + removeImpostor(impostor: PhysicsImpostor): void; + + /** + * Gets the current plugin used to run the simulation + * @returns current plugin + */ + getPhysicsPlugin(): IPhysicsEnginePlugin; + + /** + * Gets the list of physic impostors + * @returns an array of PhysicsImpostor + */ + getImpostors(): Array; + + //**************************************************************************** + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @returns PhysicsRaycastResult + */ + raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; + + /** + * Called by the scene. No need to call it. + * @param delta defines the timespan between frames + */ + _step(delta: number): void; +} diff --git a/packages/dev/core/src/Physics2/index.ts b/packages/dev/core/src/Physics2/index.ts new file mode 100644 index 00000000000..0fa1396214a --- /dev/null +++ b/packages/dev/core/src/Physics2/index.ts @@ -0,0 +1,8 @@ +/* eslint-disable import/no-internal-modules */ +export * from "./IPhysicsEngine"; +export * from "./physicsEngine"; +export * from "./physicsBody"; +export * from "./physicsRaycastResult"; +export * from "./physicsShape"; +export * from "./physicsJoint"; +export * from "./physicsMaterial"; diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics2/physicsBody.ts new file mode 100644 index 00000000000..2acbe9126ad --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsBody.ts @@ -0,0 +1,157 @@ +import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEngine"; +import type { PhysicsShape } from "./PhysicsShape"; +import type { Vector3 } from "../Maths/math.vector"; + +/** + * + */ +export class PhysicsBody { + /** @internal */ + public _pluginData: any = {}; + + private _physicsPlugin: IPhysicsEnginePlugin; + + /** + * + * @param shape + */ + public setShape(shape: PhysicsShape): void { + this._physicsPlugin.setShape(this, shape); + } + + /** + * + * @returns + */ + public getShape(): PhysicsShape { + return this._physicsPlugin.getShape(this); + } + + /** + * + * @param group + */ + public setFilterGroup(group: number): void { + this._physicsPlugin.setFilterGroup(this, group); + } + + /** + * + * @returns + */ + public getFilterGroup(): number { + return this._physicsPlugin.getFilterGroup(this); + } + + /** + * + * @param eventMask + */ + public setEventMask(eventMask: number): void { + this._physicsPlugin.setEventMask(this, eventMask); + } + + /** + * + * @returns + */ + public getEventMask(): number { + return this._physicsPlugin.getEventMask(this); + } + + /** + * + * @param massProps + */ + public setMassProperties(massProps: MassProperties): void { + this._physicsPlugin.setMassProperties(this, massProps); + } + + /** + * + * @returns + */ + public getMassProperties(): MassProperties { + return this._physicsPlugin.getMassProperties(this); + } + + /** + * + * @param damping + */ + public setLinearDamping(damping: number): void { + this._physicsPlugin.setLinearDamping(this, damping); + } + + /** + * + * @returns + */ + public getLinearDamping(): number { + return this._physicsPlugin.getLinearDamping(this); + } + + /** + * + * @param damping + */ + public setAngularDamping(damping: number): void { + this._physicsPlugin.setAngularDamping(this, damping); + } + + /** + * + * @returns + */ + public getAngularDamping(): number { + return this._physicsPlugin.getAngularDamping(this); + } + + /** + * + * @param linVel + */ + public setLinearVelocity(linVel: Vector3): void { + this._physicsPlugin.setLinearVelocity(this, linVel); + } + + /** + * + * @returns + */ + public getLinearVelocity(): Vector3 { + return this._physicsPlugin.getLinearVelocity(this); + } + + /** + * + * @param angVel + */ + public setAngularVelocity(angVel: Vector3): void { + this._physicsPlugin.setAngularVelocity(this, angVel); + } + + /** + * + * @returns + */ + public getAngularVelocity(): Vector3 { + return this._physicsPlugin.getAngularVelocity(this); + } + + /** + * + * @param location + * @param impulse + */ + public applyImpulse(location: Vector3, impulse: Vector3): void { + this._physicsPlugin.applyImpulse(this, location, impulse); + } + + /** + * + */ + public dispose() { + this._physicsPlugin.disposeBody(this); + } +} diff --git a/packages/dev/core/src/Physics2/physicsEngine.ts b/packages/dev/core/src/Physics2/physicsEngine.ts new file mode 100644 index 00000000000..d966cf87989 --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsEngine.ts @@ -0,0 +1,254 @@ +import type { Nullable } from "../types"; +import { Vector3 } from "../Maths/math.vector"; +import type { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "./IPhysicsEngine"; +import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +import type { PhysicsJoint } from "./physicsJoint"; +import type { PhysicsRaycastResult } from "./physicsRaycastResult"; +import { _WarnImport } from "../Misc/devTools"; + +/** + * Class used to control physics engine + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +export class PhysicsEngine implements IPhysicsEngine { + /** + * Global value used to control the smallest number supported by the simulation + */ + public static Epsilon = 0.001; + + private _impostors: Array = []; + private _joints: Array = []; + private _subTimeStep: number = 0; + private _uniqueIdCounter = 0; + + /** + * Gets the gravity vector used by the simulation + */ + public gravity: Vector3; + + /** + * Factory used to create the default physics plugin. + * @returns The default physics plugin + */ + public static DefaultPluginFactory(): IPhysicsEnginePlugin { + throw _WarnImport("CannonJSPlugin"); + } + + /** + * Creates a new Physics Engine + * @param gravity defines the gravity vector used by the simulation + * @param _physicsPlugin defines the plugin to use (CannonJS by default) + */ + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { + if (!this._physicsPlugin.isSupported()) { + throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); + } + gravity = gravity || new Vector3(0, -9.807, 0); + this.setGravity(gravity); + this.setTimeStep(); + } + + /** + * Sets the gravity vector used by the simulation + * @param gravity defines the gravity vector to use + */ + public setGravity(gravity: Vector3): void { + this.gravity = gravity; + this._physicsPlugin.setGravity(this.gravity); + } + + /** + * Set the time step of the physics engine. + * Default is 1/60. + * To slow it down, enter 1/600 for example. + * To speed it up, 1/30 + * @param newTimeStep defines the new timestep to apply to this world. + */ + public setTimeStep(newTimeStep: number = 1 / 60) { + this._physicsPlugin.setTimeStep(newTimeStep); + } + + /** + * Get the time step of the physics engine. + * @returns the current time step + */ + public getTimeStep(): number { + return this._physicsPlugin.getTimeStep(); + } + + /** + * Set the sub time step of the physics engine. + * Default is 0 meaning there is no sub steps + * To increase physics resolution precision, set a small value (like 1 ms) + * @param subTimeStep defines the new sub timestep used for physics resolution. + */ + public setSubTimeStep(subTimeStep: number = 0) { + this._subTimeStep = subTimeStep; + } + + /** + * Get the sub time step of the physics engine. + * @returns the current sub time step + */ + public getSubTimeStep() { + return this._subTimeStep; + } + + /** + * Release all resources + */ + public dispose(): void { + this._impostors.forEach(function (impostor) { + impostor.dispose(); + }); + this._physicsPlugin.dispose(); + } + + /** + * Gets the name of the current physics plugin + * @returns the name of the plugin + */ + public getPhysicsPluginName(): string { + return this._physicsPlugin.name; + } + + /** + * Adding a new impostor for the impostor tracking. + * This will be done by the impostor itself. + * @param impostor the impostor to add + */ + public addImpostor(impostor: PhysicsImpostor) { + this._impostors.push(impostor); + impostor.uniqueId = this._uniqueIdCounter++; + //if no parent, generate the body + if (!impostor.parent) { + this._physicsPlugin.generatePhysicsBody(impostor); + } + } + + /** + * Remove an impostor from the engine. + * This impostor and its mesh will not longer be updated by the physics engine. + * @param impostor the impostor to remove + */ + public removeImpostor(impostor: PhysicsImpostor) { + const index = this._impostors.indexOf(impostor); + if (index > -1) { + const removed = this._impostors.splice(index, 1); + //Is it needed? + if (removed.length) { + this.getPhysicsPlugin().removePhysicsBody(impostor); + } + } + } + + /** + * Add a joint to the physics engine + * @param mainImpostor defines the main impostor to which the joint is added. + * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint + * @param joint defines the joint that will connect both impostors. + */ + public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { + const impostorJoint = { + mainImpostor: mainImpostor, + connectedImpostor: connectedImpostor, + joint: joint, + }; + joint.physicsPlugin = this._physicsPlugin; + this._joints.push(impostorJoint); + this._physicsPlugin.generateJoint(impostorJoint); + } + + /** + * Removes a joint from the simulation + * @param mainImpostor defines the impostor used with the joint + * @param connectedImpostor defines the other impostor connected to the main one by the joint + * @param joint defines the joint to remove + */ + public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { + const matchingJoints = this._joints.filter(function (impostorJoint) { + return impostorJoint.connectedImpostor === connectedImpostor && impostorJoint.joint === joint && impostorJoint.mainImpostor === mainImpostor; + }); + if (matchingJoints.length) { + this._physicsPlugin.removeJoint(matchingJoints[0]); + //TODO remove it from the list as well + } + } + + /** + * Called by the scene. No need to call it. + * @param delta defines the timespan between frames + */ + public _step(delta: number) { + //check if any mesh has no body / requires an update + this._impostors.forEach((impostor) => { + if (impostor.isBodyInitRequired()) { + this._physicsPlugin.generatePhysicsBody(impostor); + } + }); + + if (delta > 0.1) { + delta = 0.1; + } else if (delta <= 0) { + delta = 1.0 / 60.0; + } + + this._physicsPlugin.executeStep(delta, this._impostors); + } + + /** + * Gets the current plugin used to run the simulation + * @returns current plugin + */ + public getPhysicsPlugin(): IPhysicsEnginePlugin { + return this._physicsPlugin; + } + + /** + * Gets the list of physic impostors + * @returns an array of PhysicsImpostor + */ + public getImpostors(): Array { + return this._impostors; + } + + /** + * Gets the impostor for a physics enabled object + * @param object defines the object impersonated by the impostor + * @returns the PhysicsImpostor or null if not found + */ + public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable { + for (let i = 0; i < this._impostors.length; ++i) { + if (this._impostors[i].object === object) { + return this._impostors[i]; + } + } + + return null; + } + + /** + * Gets the impostor for a physics body object + * @param body defines physics body used by the impostor + * @returns the PhysicsImpostor or null if not found + */ + public getImpostorWithPhysicsBody(body: any): Nullable { + for (let i = 0; i < this._impostors.length; ++i) { + if (this._impostors[i].physicsBody === body) { + return this._impostors[i]; + } + } + + return null; + } + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @returns PhysicsRaycastResult + */ + public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { + return this._physicsPlugin.raycast(from, to); + } +} diff --git a/packages/dev/core/src/Physics2/physicsImpostor.ts b/packages/dev/core/src/Physics2/physicsImpostor.ts new file mode 100644 index 00000000000..2a7019b2659 --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsImpostor.ts @@ -0,0 +1,17 @@ +import type { PhysicsBody } from "./physicsBody"; +import type { PhysicsShape } from "./PhysicsShape"; + +/** + * + */ +export class PhysicsImpostor { + /** + * + */ + public body: PhysicsBody; + + /** + * + */ + public shape: PhysicsShape; +} diff --git a/packages/dev/core/src/Physics2/physicsJoint.ts b/packages/dev/core/src/Physics2/physicsJoint.ts new file mode 100644 index 00000000000..6d4b6b8a43f --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsJoint.ts @@ -0,0 +1,212 @@ +import type { Vector3 } from "../Maths/math.vector"; +import type { IPhysicsEnginePlugin, JointAxis, JointAxisLimitMode, JointMotorType } from "./IPhysicsEngine"; +import type { PhysicsBody } from "./physicsBody"; + +/** + * This is a holder class for the physics joint created by the physics plugin + * It holds a set of functions to control the underlying joint + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +export class PhysicsJoint { + protected _physicsPlugin: IPhysicsEnginePlugin; + + //Joint Types + /** + * + */ + + /** + * + * @param body + */ + public setParentBody(body: PhysicsBody): void { + this._physicsPlugin.setParentBody(this, body); + } + + /** + * + * @returns + */ + public getParentBody(): PhysicsBody { + return this._physicsPlugin.getParentBody(this); + } + + /** + * + * @param body + */ + public setChildBody(body: PhysicsBody): void { + this._physicsPlugin.setChildBody(this, body); + } + + /** + * + * @returns + */ + public getChildBody(): PhysicsBody { + return this._physicsPlugin.getChildBody(this); + } + + /** + * + * @param pivot + + * @param axisX + * @param axisY + */ + public setAnchorInParent(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { + this._physicsPlugin.setAnchorInParent(this, pivot, axisX, axisY); + } + + /** + * + * @param pivot + * @param axisX + * @param axisY + */ + public setAnchorInChild(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { + this._physicsPlugin.setAnchorInChild(this, pivot, axisX, axisY); + } + + /** + * + * @param isEnabled + */ + public setEnabled(isEnabled: boolean): void { + this._physicsPlugin.setEnabled(this, isEnabled); + } + + /** + * + * @returns + */ + public getEnabled(): boolean { + return this._physicsPlugin.getEnabled(this); + } + + /** + * + * @param isEnabled + */ + public setCollisionsEnabled(isEnabled: boolean): void { + this._physicsPlugin.setCollisionsEnabled(this, isEnabled); + } + + /** + * + * @returns + */ + public getCollisionsEnabled(): boolean { + return this._physicsPlugin.getCollisionsEnabled(this); + } + + /** + * + * @param axis + * @param friction + */ + public setAxisFriction(axis: JointAxis, friction: number): void { + this._physicsPlugin.setAxisFriction(this, axis, friction); + } + + /** + * + * @param axis + * @returns + */ + public getAxisFriction(axis: JointAxis): number { + return this._physicsPlugin.getAxisFriction(this, axis); + } + + /** + * + * @param axis + * @param limitMode + */ + public setAxisMode(axis: JointAxis, limitMode: JointAxisLimitMode): void { + this._physicsPlugin.setAxisMode(this, axis, limitMode); + } + /** + * + * @param axis + */ + public getAxisMode(axis: JointAxis): JointAxisLimitMode { + return this._physicsPlugin.getAxisMode(this, axis); + } + + /** + * + */ + public setAxisMinLimit(axis: JointAxis, minLimit: number): void { + this._physicsPlugin.setAxisMinLimit(this, axis, minLimit); + } + + /** + * + */ + public getAxisMinLimit(axis: JointAxis): number { + return this._physicsPlugin.getAxisMinLimit(this, axis); + } + + /** + * + */ + public setAxisMaxLimit(axis: JointAxis, limit: number): void { + this._physicsPlugin.setAxisMaxLimit(this, axis, limit); + } + + /** + * + */ + public getAxisMaxLimit(axis: JointAxis): number { + return this._physicsPlugin.getAxisMaxLimit(this, axis); + } + + /** + * + */ + public setAxisMotorType(axis: JointAxis, motorType: JointMotorType): void { + this._physicsPlugin.setAxisMotorType(this, axis, motorType); + } + + /** + * + */ + public getAxisMotorType(axis: JointAxis): JointMotorType { + return this._physicsPlugin.getAxisMotorType(this, axis); + } + + /** + * + */ + public setAxisMotorTarget(axis: JointAxis, target: number): void { + this._physicsPlugin.setAxisMotorTarget(this, axis, target); + } + + /** + * + */ + public getAxisMotorTarget(axis: JointAxis): number { + return this._physicsPlugin.getAxisMotorTarget(this, axis); + } + + /** + * + */ + public setAxisMotorMaxForce(axis: JointAxis, maxForce: number): void { + this._physicsPlugin.setAxisMotorMaxForce(this, axis, maxForce); + } + + /** + * + */ + public getAxisMotorMaxForce(axis: JointAxis): number { + return this._physicsPlugin.getAxisMotorMaxForce(this, axis); + } + + /** + * + */ + public dispose(): void { + this._physicsPlugin.disposeJoint(this); + } +} diff --git a/packages/dev/core/src/Physics2/physicsMaterial.ts b/packages/dev/core/src/Physics2/physicsMaterial.ts new file mode 100644 index 00000000000..698ac8899f7 --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsMaterial.ts @@ -0,0 +1,52 @@ +import type { IPhysicsEnginePlugin } from "./IPhysicsEngine"; + +/** + * + */ +export class PhysicsMaterial { + /** + * + */ + public _pluginData: any = {}; + + protected _physicsPlugin: IPhysicsEnginePlugin; + + /** + * + * @param friction + */ + public setFriction(friction: number): void { + this._physicsPlugin.setFriction(this, friction); + } + + /** + * + * @returns + */ + public getFriction(): number { + return this._physicsPlugin.getFriction(this); + } + + /** + * + * @param restitution + */ + public setRestitution(restitution: number): void { + this._physicsPlugin.setRestitution(this, restitution); + } + + /** + * + * @returns + */ + public getRestitution(): number { + return this._physicsPlugin.getRestitution(this); + } + + /** + * + */ + public dispose(): void { + this._physicsPlugin.disposeMaterial(this); + } +} diff --git a/packages/dev/core/src/Physics2/physicsRaycastResult.ts b/packages/dev/core/src/Physics2/physicsRaycastResult.ts new file mode 100644 index 00000000000..8443538c0b2 --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsRaycastResult.ts @@ -0,0 +1,119 @@ +import { Vector3 } from "../Maths/math.vector"; + +/** + * Holds the data for the raycast result + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +export class PhysicsRaycastResult { + private _hasHit: boolean = false; + + private _hitDistance: number = 0; + private _hitNormalWorld: Vector3 = Vector3.Zero(); + private _hitPointWorld: Vector3 = Vector3.Zero(); + private _rayFromWorld: Vector3 = Vector3.Zero(); + private _rayToWorld: Vector3 = Vector3.Zero(); + + /** + * Gets if there was a hit + */ + get hasHit(): boolean { + return this._hasHit; + } + + /** + * Gets the distance from the hit + */ + get hitDistance(): number { + return this._hitDistance; + } + + /** + * Gets the hit normal/direction in the world + */ + get hitNormalWorld(): Vector3 { + return this._hitNormalWorld; + } + + /** + * Gets the hit point in the world + */ + get hitPointWorld(): Vector3 { + return this._hitPointWorld; + } + + /** + * Gets the ray "start point" of the ray in the world + */ + get rayFromWorld(): Vector3 { + return this._rayFromWorld; + } + + /** + * Gets the ray "end point" of the ray in the world + */ + get rayToWorld(): Vector3 { + return this._rayToWorld; + } + + /** + * Sets the hit data (normal & point in world space) + * @param hitNormalWorld defines the normal in world space + * @param hitPointWorld defines the point in world space + */ + public setHitData(hitNormalWorld: IXYZ, hitPointWorld: IXYZ) { + this._hasHit = true; + this._hitNormalWorld = new Vector3(hitNormalWorld.x, hitNormalWorld.y, hitNormalWorld.z); + this._hitPointWorld = new Vector3(hitPointWorld.x, hitPointWorld.y, hitPointWorld.z); + } + + /** + * Sets the distance from the start point to the hit point + * @param distance + */ + public setHitDistance(distance: number) { + this._hitDistance = distance; + } + + /** + * Calculates the distance manually + */ + public calculateHitDistance() { + this._hitDistance = Vector3.Distance(this._rayFromWorld, this._hitPointWorld); + } + + /** + * Resets all the values to default + * @param from The from point on world space + * @param to The to point on world space + */ + public reset(from: Vector3 = Vector3.Zero(), to: Vector3 = Vector3.Zero()) { + this._rayFromWorld = from; + this._rayToWorld = to; + + this._hasHit = false; + this._hitDistance = 0; + + this._hitNormalWorld = Vector3.Zero(); + this._hitPointWorld = Vector3.Zero(); + } +} + +/** + * Interface for the size containing width and height + */ +interface IXYZ { + /** + * X + */ + x: number; + + /** + * Y + */ + y: number; + + /** + * Z + */ + z: number; +} diff --git a/packages/dev/core/src/Physics2/physicsShape.ts b/packages/dev/core/src/Physics2/physicsShape.ts new file mode 100644 index 00000000000..78e8e8cf432 --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsShape.ts @@ -0,0 +1,109 @@ +import type { TransformNode } from "../Meshes/transformNode"; +import type { BoundingBox } from "../Culling/boundingBox"; +import type { IPhysicsEnginePlugin, ShapeType } from "./IPhysicsEngine"; +import type { PhysicsMaterial } from "./physicsMaterial"; + +/** + * + */ +export class PhysicsShape { + /** @internal */ + public _pluginData: any = {}; + + protected _physicsPlugin: IPhysicsEnginePlugin; + + private _type: ShapeType; + + /** + * + */ + public get type(): ShapeType { + return this._type; + } + + /** + * + * @param layer + */ + public setFilterLayer(layer: number): void { + this._physicsPlugin.setFilterLayer(this, layer); + } + + /** + * + * @returns + */ + public getFilterLayer(): number { + return this._physicsPlugin.getFilterLayer(this); + } + + /** + * + * @param materialId + */ + public setMaterial(materialId: PhysicsMaterial): void { + this._physicsPlugin.setMaterial(this, materialId); + } + + /** + * + * @returns + */ + public getMaterial(): PhysicsMaterial { + return this._physicsPlugin.getMaterial(this); + } + + /** + * + * @param density + */ + public setDensity(density: number): void { + this._physicsPlugin.setDensity(this, density); + } + + /** + * + */ + public getDensity(): number { + return this._physicsPlugin.getDensity(this); + } + + /** + * + * @param newChild + * @param childTransform + */ + public addChild(newChild: PhysicsShape, childTransform: TransformNode): void { + this._physicsPlugin.addChild(this, newChild, childTransform); + } + + /** + * + * @param childIndex + */ + public removeChild(childIndex: number): void { + this._physicsPlugin.removeChild(this, childIndex); + } + + /** + * + * @returns + */ + public getNumChildren(): number { + return this._physicsPlugin.getNumChildren(this); + } + + /** + * + */ + public getBoundingBox(): BoundingBox { + return this._physicsPlugin.getBoundingBox(this); + } + + /** + * + */ + public dispose() { + this._physicsPlugin.disposeShape(this); + } +} From 8bf793a4553b79ecb97e730bb020ac0bbb6f4be1 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 24 Nov 2022 14:02:04 +0100 Subject: [PATCH 02/28] ctor --- .../dev/core/src/Physics2/IPhysicsEngine.ts | 20 +++--- packages/dev/core/src/Physics2/physicsBody.ts | 11 +++- .../dev/core/src/Physics2/physicsImpostor.ts | 33 +++++++++- .../dev/core/src/Physics2/physicsShape.ts | 63 ++++++++++++++++++- 4 files changed, 116 insertions(+), 11 deletions(-) diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics2/IPhysicsEngine.ts index 0d035c01e0a..8c7e5ffa4fc 100644 --- a/packages/dev/core/src/Physics2/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics2/IPhysicsEngine.ts @@ -49,6 +49,7 @@ export enum ShapeType { CYLINDER, BOX, CONVEX_HULL, + CONTAINER, MESH, } @@ -57,6 +58,17 @@ export enum JointMotorType { VELOCITY, POSITION, } + +export interface PhysicsShapeParameters { + center?: Vector3; + radius?: number; + pointA?: Vector3; + pointB?: Vector3; + rotation?: Quaternion; + extents?: Vector3; + mesh?: AbstractMesh; +} + /** * */ @@ -116,13 +128,7 @@ export interface IPhysicsEnginePlugin { disposeBody(body: PhysicsBody): void; // shape - createShapeSphere(center: Vector3, radius: number): PhysicsShape; - createShapeCapsule(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; - createShapeCylinder(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; - createShapeBox(center: Vector3, rotation: Quaternion, extents: Vector3): PhysicsShape; - createShapeConvexHull(mesh: AbstractMesh): PhysicsShape; - createShapeMesh(mesh: AbstractMesh): PhysicsShape; - createShapeContainer(): PhysicsShape; + createShape(type: ShapeType, options: PhysicsShapeParameters): PhysicsShape; setFilterLayer(shape: PhysicsShape, layer: number): void; getFilterLayer(shape: PhysicsShape): number; diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics2/physicsBody.ts index 2acbe9126ad..a0bcc96c1e8 100644 --- a/packages/dev/core/src/Physics2/physicsBody.ts +++ b/packages/dev/core/src/Physics2/physicsBody.ts @@ -1,6 +1,8 @@ import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEngine"; import type { PhysicsShape } from "./PhysicsShape"; import type { Vector3 } from "../Maths/math.vector"; +import type { Scene } from "../scene"; +import { Nullable } from ".."; /** * @@ -9,8 +11,15 @@ export class PhysicsBody { /** @internal */ public _pluginData: any = {}; - private _physicsPlugin: IPhysicsEnginePlugin; + private _physicsPlugin: Nullable; + constructor(scene: Scene) { + if (!scene) { + return; + } + const physicsEngine = scene.getPhysicsEngine(); + this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); + } /** * * @param shape diff --git a/packages/dev/core/src/Physics2/physicsImpostor.ts b/packages/dev/core/src/Physics2/physicsImpostor.ts index 2a7019b2659..42a08961ed1 100644 --- a/packages/dev/core/src/Physics2/physicsImpostor.ts +++ b/packages/dev/core/src/Physics2/physicsImpostor.ts @@ -1,6 +1,7 @@ import type { PhysicsBody } from "./physicsBody"; +import type { PhysicsMaterial } from "./physicsMaterial"; import type { PhysicsShape } from "./PhysicsShape"; - +import { Logger } from "../Misc/logger"; /** * */ @@ -14,4 +15,34 @@ export class PhysicsImpostor { * */ public shape: PhysicsShape; + + public material: PhysicsMaterial; + + constructor( + /** + * The physics-enabled object used as the physics imposter + */ + public object: IPhysicsEnabledObject, + /** + * The type of the physics imposter + */ + public type: number, + private _options: PhysicsImpostorParameters = { mass: 0 }, + private _scene?: Scene + ) { + //sanity check! + if (!this.object) { + Logger.Error("No object was provided. A physics object is obligatory"); + return; + } + if (this.object.parent && _options.mass !== 0) { + Logger.Warn("A physics impostor has been created for an object which has a parent. Babylon physics currently works in local space so unexpected issues may occur."); + } + + // Legacy support for old syntax. + if (!this._scene && object.getScene) { + this._scene = object.getScene(); + } + // TODO + } } diff --git a/packages/dev/core/src/Physics2/physicsShape.ts b/packages/dev/core/src/Physics2/physicsShape.ts index 78e8e8cf432..53cff2890ac 100644 --- a/packages/dev/core/src/Physics2/physicsShape.ts +++ b/packages/dev/core/src/Physics2/physicsShape.ts @@ -1,7 +1,11 @@ import type { TransformNode } from "../Meshes/transformNode"; import type { BoundingBox } from "../Culling/boundingBox"; -import type { IPhysicsEnginePlugin, ShapeType } from "./IPhysicsEngine"; +import { IPhysicsEnginePlugin, ShapeType, PhysicsShapeParameters } from "./IPhysicsEngine"; import type { PhysicsMaterial } from "./physicsMaterial"; +import { Quaternion, Vector3 } from "../Maths/math.vector"; +import { AbstractMesh } from "../Meshes/abstractMesh"; +import { Nullable, Scene } from ".."; + /** * @@ -10,10 +14,23 @@ export class PhysicsShape { /** @internal */ public _pluginData: any = {}; - protected _physicsPlugin: IPhysicsEnginePlugin; + protected _physicsPlugin: Nullable; private _type: ShapeType; + constructor(type: number, + options: PhysicsShapeParameters = { }, + scene: Scene) { + this._type = type; + if (!scene) { + return; + } + const physicsEngine = scene.getPhysicsEngine(); + this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); + this._physicsPlugin?.createShape(type, options); + + } + /** * */ @@ -107,3 +124,45 @@ export class PhysicsShape { this._physicsPlugin.disposeShape(this); } } + +export class PhysicsShapeSphere extends PhysicsShape { + constructor(center: Vector3, radius: number) { + super(ShapeType.BOX, {center:center, radius:radius}); + } +} + +export class PhysicsShapeCapsule extends PhysicsShape { + constructor(pointA: Vector3, pointB: Vector3, radius: number) { + super(ShapeType.CAPSULE, {pointA:pointA, pointB:pointB, radius:radius}); + } +} + +export class PhysicsShapeCylinder extends PhysicsShape { + constructor(pointA: Vector3, pointB: Vector3, radius: number) { + super(ShapeType.CYLINDER, {pointA:pointA, pointB:pointB, radius:radius}); + } +} + +export class PhysicsShapeShapeBox extends PhysicsShape { + constructor(center: Vector3, rotation: Quaternion, extents: Vector3) { + super(ShapeType.BOX, {center:center, rotation:rotation, extents:extents}); + } +} + +export class PhysicsShapeShapeConvexHull extends PhysicsShape { + constructor(mesh: AbstractMesh) { + super(ShapeType.CONVEX_HULL, {mesh:mesh}); + } +} + +export class PhysicsShapeShapeMesh extends PhysicsShape { + constructor(mesh: AbstractMesh) { + super(ShapeType.MESH, {mesh:mesh}); + } +} + +export class PhysicsShapeShapeContainer extends PhysicsShape { + constructor(mesh: AbstractMesh) { + super(ShapeType.CONTAINER, {}); + } +} From 95f14c5a24ee31b5fbc12f61bb6422fd8c746bd4 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 24 Nov 2022 19:03:57 +0100 Subject: [PATCH 03/28] more work on the interfaces --- .../dev/core/src/Physics2/IPhysicsEngine.ts | 75 +++------ packages/dev/core/src/Physics2/index.ts | 1 + .../dev/core/src/Physics2/physicsAggregate.ts | 121 ++++++++++++++ packages/dev/core/src/Physics2/physicsBody.ts | 58 +++---- .../dev/core/src/Physics2/physicsEngine.ts | 126 ++------------- .../dev/core/src/Physics2/physicsImpostor.ts | 48 ------ .../dev/core/src/Physics2/physicsJoint.ts | 127 +++++++++++---- .../dev/core/src/Physics2/physicsMaterial.ts | 17 +- .../dev/core/src/Physics2/physicsShape.ts | 153 +++++++++++++----- 9 files changed, 416 insertions(+), 310 deletions(-) create mode 100644 packages/dev/core/src/Physics2/physicsAggregate.ts delete mode 100644 packages/dev/core/src/Physics2/physicsImpostor.ts diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics2/IPhysicsEngine.ts index 8c7e5ffa4fc..2aea4f67262 100644 --- a/packages/dev/core/src/Physics2/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics2/IPhysicsEngine.ts @@ -1,6 +1,5 @@ import type { Vector3, Quaternion } from "../Maths/math.vector"; import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { PhysicsImpostor } from "./physicsImpostor"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; import type { PhysicsBody } from "./physicsBody"; import type { PhysicsShape } from "./physicsShape"; @@ -8,22 +7,13 @@ import type { PhysicsJoint } from "./physicsJoint"; import type { BoundingBox } from "../culling/boundingBox"; import type { TransformNode } from "../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; -/** - * Interface used to describe a physics joint - */ -export interface PhysicsImpostorJoint { - /** Defines the main impostor to which the joint is linked */ - mainImpostor: PhysicsImpostor; - /** Defines the impostor that is connected to the main impostor using this joint */ - connectedImpostor: PhysicsImpostor; - /** Defines the joint itself */ - joint: PhysicsJoint; -} +import type { PhysicsAggregate } from "./physicsAggregate"; export enum JointAxisLimitMode { FREE, LIMITED, LOCKED, + NONE } export enum JointAxis { @@ -69,6 +59,13 @@ export interface PhysicsShapeParameters { mesh?: AbstractMesh; } +export interface PhysicsJointParameters { + pivotA?: Vector3; + pivotB?: Vector3; + axisA?: Vector3; + axisB?: Vector3; +} + /** * */ @@ -92,7 +89,7 @@ export interface MassProperties { } /** @internal */ -export interface IPhysicsEnginePlugin { +export interface IPhysicsEnginePlugin2 { /** * */ @@ -104,10 +101,10 @@ export interface IPhysicsEnginePlugin { setGravity(gravity: Vector3): void; setTimeStep(timeStep: number): void; getTimeStep(): number; - executeStep(delta: number, impostors: Array): void; //not forgetting pre and post events + executeStep(delta: number): void; //not forgetting pre and post events // body - createBody(): PhysicsBody; + initBody(body: PhysicsBody): void; setShape(body: PhysicsBody, shape: PhysicsShape): void; getShape(body: PhysicsBody): PhysicsShape; setFilterGroup(body: PhysicsBody, group: number): void; @@ -128,8 +125,7 @@ export interface IPhysicsEnginePlugin { disposeBody(body: PhysicsBody): void; // shape - createShape(type: ShapeType, options: PhysicsShapeParameters): PhysicsShape; - + initShape(shape: PhysicsShape, type: ShapeType, options: PhysicsShapeParameters): void; setFilterLayer(shape: PhysicsShape, layer: number): void; getFilterLayer(shape: PhysicsShape): number; setMaterial(shape: PhysicsShape, material: PhysicsMaterial): void; @@ -143,6 +139,7 @@ export interface IPhysicsEnginePlugin { disposeShape(shape: PhysicsShape): void; // material + initMaterial(material: PhysicsMaterial): void; setFriction(material: PhysicsMaterial, friction: number): void; getFriction(material: PhysicsMaterial): number; setRestitution(material: PhysicsMaterial, restitution: number): void; @@ -150,11 +147,7 @@ export interface IPhysicsEnginePlugin { disposeMaterial(material: PhysicsMaterial): void; // joint - createJointBallAndSocket(): PhysicsJoint; - createJointDistance(): PhysicsJoint; - createJointHinge(): PhysicsJoint; - createJointSlider(): PhysicsJoint; - createJointLock(): PhysicsJoint; + initJoint(joint: PhysicsJoint, type: JointType, options: PhysicsJointParameters): void; setParentBody(joint: PhysicsJoint, body: PhysicsBody): void; getParentBody(joint: PhysicsJoint): PhysicsBody; setChildBody(joint: PhysicsJoint, body: PhysicsBody): void; @@ -191,7 +184,7 @@ export interface IPhysicsEnginePlugin { * Interface used to define a physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export interface IPhysicsEngine { +export interface IPhysicsEngine2 { /** * Gets the gravity vector used by the simulation */ @@ -243,42 +236,16 @@ export interface IPhysicsEngine { */ getPhysicsPluginName(): string; - //*********************************************************** - createShapeSphere(center: Vector3, radius: number): PhysicsShape; - createShapeCapsule(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; - createShapeCylinder(pointA: Vector3, pointB: Vector3, radius: number): PhysicsShape; - createShapeBox(center: Vector3, rotation: Quaternion, extents: Vector3): PhysicsShape; - createShapeConvexHull(mesh: AbstractMesh): PhysicsShape; - createShapeMesh(mesh: AbstractMesh): PhysicsShape; - createShapeContainer(): PhysicsShape; - - createJointBallAndSocket(): PhysicsJoint; - createJointDistance(): PhysicsJoint; - createJointHinge(): PhysicsJoint; - createJointSlider(): PhysicsJoint; - createJointLock(): PhysicsJoint; - - createBody(): PhysicsBody; - - addBody(body: PhysicsBody): void; - removeBody(body: PhysicsBody): void; - // Helpers - getBodies(): Array; - addImpostor(impostor: PhysicsImpostor): void; - removeImpostor(impostor: PhysicsImpostor): void; - + /*getBodies(): Array; + addAggregate(impostor: PhysicsAggregate): void; + removeAggregate(impostor: PhysicsAggregate): void; +*/ /** * Gets the current plugin used to run the simulation * @returns current plugin */ - getPhysicsPlugin(): IPhysicsEnginePlugin; - - /** - * Gets the list of physic impostors - * @returns an array of PhysicsImpostor - */ - getImpostors(): Array; + getPhysicsPlugin(): IPhysicsEnginePlugin2; //**************************************************************************** diff --git a/packages/dev/core/src/Physics2/index.ts b/packages/dev/core/src/Physics2/index.ts index 0fa1396214a..36005027f22 100644 --- a/packages/dev/core/src/Physics2/index.ts +++ b/packages/dev/core/src/Physics2/index.ts @@ -6,3 +6,4 @@ export * from "./physicsRaycastResult"; export * from "./physicsShape"; export * from "./physicsJoint"; export * from "./physicsMaterial"; +export * from "./physicsAggregate"; diff --git a/packages/dev/core/src/Physics2/physicsAggregate.ts b/packages/dev/core/src/Physics2/physicsAggregate.ts new file mode 100644 index 00000000000..a984572292a --- /dev/null +++ b/packages/dev/core/src/Physics2/physicsAggregate.ts @@ -0,0 +1,121 @@ +import type { PhysicsBody } from "./physicsBody"; +import type { PhysicsMaterial } from "./physicsMaterial"; +import type { PhysicsShape } from "./PhysicsShape"; +import { Logger } from "../Misc/logger"; +import type { Scene } from "../scene"; +import type { TransformNode } from "../Meshes/transformNode"; + +/** + * The interface for the physics aggregate parameters + */ + export interface PhysicsAggregateParameters { + /** + * The mass of the physics imposter + */ + mass: number; + /** + * The friction of the physics imposter + */ + friction?: number; + /** + * The coefficient of restitution of the physics imposter + */ + restitution?: number; + /** + * The native options of the physics imposter + */ + nativeOptions?: any; + /** + * Specifies if the parent should be ignored + */ + ignoreParent?: boolean; + /** + * Specifies if bi-directional transformations should be disabled + */ + disableBidirectionalTransformation?: boolean; + /** + * The pressure inside the physics imposter, soft object only + */ + pressure?: number; + /** + * The stiffness the physics imposter, soft object only + */ + stiffness?: number; + /** + * The number of iterations used in maintaining consistent vertex velocities, soft object only + */ + velocityIterations?: number; + /** + * The number of iterations used in maintaining consistent vertex positions, soft object only + */ + positionIterations?: number; + /** + * The number used to fix points on a cloth (0, 1, 2, 4, 8) or rope (0, 1, 2) only + * 0 None, 1, back left or top, 2, back right or bottom, 4, front left, 8, front right + * Add to fix multiple points + */ + fixedPoints?: number; + /** + * The collision margin around a soft object + */ + margin?: number; + /** + * The collision margin around a soft object + */ + damping?: number; + /** + * The path for a rope based on an extrusion + */ + path?: any; + /** + * The shape of an extrusion used for a rope based on an extrusion + */ + shape?: any; +} +/** + * + */ +export class PhysicsAggregate { + /** + * + */ + public body: PhysicsBody; + + /** + * + */ + public shape: PhysicsShape; + + /** + * + */ + public material: PhysicsMaterial; + + constructor( + /** + * The physics-enabled object used as the physics imposter + */ + public transformNode: TransformNode, + /** + * The type of the physics imposter + */ + public type: number, + private _options: PhysicsAggregateParameters = { mass: 0 }, + private _scene?: Scene + ) { + //sanity check! + if (!this.transformNode) { + Logger.Error("No object was provided. A physics object is obligatory"); + return; + } + if (this.transformNode.parent && _options.mass !== 0) { + Logger.Warn("A physics impostor has been created for an object which has a parent. Babylon physics currently works in local space so unexpected issues may occur."); + } + + // Legacy support for old syntax. + if (!this._scene && transformNode.getScene) { + this._scene = transformNode.getScene(); + } + // TODO + } +} diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics2/physicsBody.ts index a0bcc96c1e8..d9fdb020f40 100644 --- a/packages/dev/core/src/Physics2/physicsBody.ts +++ b/packages/dev/core/src/Physics2/physicsBody.ts @@ -1,39 +1,43 @@ -import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEngine"; +import type { IPhysicsEnginePlugin2, MassProperties } from "./IPhysicsEngine"; import type { PhysicsShape } from "./PhysicsShape"; -import type { Vector3 } from "../Maths/math.vector"; +import { Vector3 } from "../Maths/math.vector"; import type { Scene } from "../scene"; -import { Nullable } from ".."; /** * */ export class PhysicsBody { /** @internal */ - public _pluginData: any = {}; + public _pluginData: any = undefined; - private _physicsPlugin: Nullable; + private _physicsPlugin: IPhysicsEnginePlugin2 | undefined; + /** + * + * @param scene + * @returns + */ constructor(scene: Scene) { if (!scene) { return; } - const physicsEngine = scene.getPhysicsEngine(); - this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); + this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; + this._physicsPlugin?.initBody(this); } /** * * @param shape */ public setShape(shape: PhysicsShape): void { - this._physicsPlugin.setShape(this, shape); + this._physicsPlugin?.setShape(this, shape); } /** * * @returns */ - public getShape(): PhysicsShape { - return this._physicsPlugin.getShape(this); + public getShape(): PhysicsShape | undefined{ + return this._physicsPlugin ? this._physicsPlugin.getShape(this) : undefined; } /** @@ -41,7 +45,7 @@ export class PhysicsBody { * @param group */ public setFilterGroup(group: number): void { - this._physicsPlugin.setFilterGroup(this, group); + this._physicsPlugin?.setFilterGroup(this, group); } /** @@ -49,7 +53,7 @@ export class PhysicsBody { * @returns */ public getFilterGroup(): number { - return this._physicsPlugin.getFilterGroup(this); + return this._physicsPlugin ? this._physicsPlugin.getFilterGroup(this) : 0; } /** @@ -57,7 +61,7 @@ export class PhysicsBody { * @param eventMask */ public setEventMask(eventMask: number): void { - this._physicsPlugin.setEventMask(this, eventMask); + this._physicsPlugin?.setEventMask(this, eventMask); } /** @@ -65,7 +69,7 @@ export class PhysicsBody { * @returns */ public getEventMask(): number { - return this._physicsPlugin.getEventMask(this); + return this._physicsPlugin ? this._physicsPlugin.getEventMask(this) : 0; } /** @@ -73,15 +77,15 @@ export class PhysicsBody { * @param massProps */ public setMassProperties(massProps: MassProperties): void { - this._physicsPlugin.setMassProperties(this, massProps); + this._physicsPlugin?.setMassProperties(this, massProps); } /** * * @returns */ - public getMassProperties(): MassProperties { - return this._physicsPlugin.getMassProperties(this); + public getMassProperties(): MassProperties | undefined{ + return this._physicsPlugin ? this._physicsPlugin.getMassProperties(this) : undefined; } /** @@ -89,7 +93,7 @@ export class PhysicsBody { * @param damping */ public setLinearDamping(damping: number): void { - this._physicsPlugin.setLinearDamping(this, damping); + this._physicsPlugin?.setLinearDamping(this, damping); } /** @@ -97,7 +101,7 @@ export class PhysicsBody { * @returns */ public getLinearDamping(): number { - return this._physicsPlugin.getLinearDamping(this); + return this._physicsPlugin ? this._physicsPlugin.getLinearDamping(this) : 0; } /** @@ -105,7 +109,7 @@ export class PhysicsBody { * @param damping */ public setAngularDamping(damping: number): void { - this._physicsPlugin.setAngularDamping(this, damping); + this._physicsPlugin?.setAngularDamping(this, damping); } /** @@ -113,7 +117,7 @@ export class PhysicsBody { * @returns */ public getAngularDamping(): number { - return this._physicsPlugin.getAngularDamping(this); + return this._physicsPlugin ? this._physicsPlugin.getAngularDamping(this) : 0; } /** @@ -121,7 +125,7 @@ export class PhysicsBody { * @param linVel */ public setLinearVelocity(linVel: Vector3): void { - this._physicsPlugin.setLinearVelocity(this, linVel); + this._physicsPlugin?.setLinearVelocity(this, linVel); } /** @@ -129,7 +133,7 @@ export class PhysicsBody { * @returns */ public getLinearVelocity(): Vector3 { - return this._physicsPlugin.getLinearVelocity(this); + return this._physicsPlugin ? this._physicsPlugin.getLinearVelocity(this) : Vector3.Zero(); } /** @@ -137,7 +141,7 @@ export class PhysicsBody { * @param angVel */ public setAngularVelocity(angVel: Vector3): void { - this._physicsPlugin.setAngularVelocity(this, angVel); + this._physicsPlugin?.setAngularVelocity(this, angVel); } /** @@ -145,7 +149,7 @@ export class PhysicsBody { * @returns */ public getAngularVelocity(): Vector3 { - return this._physicsPlugin.getAngularVelocity(this); + return this._physicsPlugin ? this._physicsPlugin.getAngularVelocity(this) : Vector3.Zero(); } /** @@ -154,13 +158,13 @@ export class PhysicsBody { * @param impulse */ public applyImpulse(location: Vector3, impulse: Vector3): void { - this._physicsPlugin.applyImpulse(this, location, impulse); + this._physicsPlugin?.applyImpulse(this, location, impulse); } /** * */ public dispose() { - this._physicsPlugin.disposeBody(this); + this._physicsPlugin?.disposeBody(this); } } diff --git a/packages/dev/core/src/Physics2/physicsEngine.ts b/packages/dev/core/src/Physics2/physicsEngine.ts index d966cf87989..76251782c65 100644 --- a/packages/dev/core/src/Physics2/physicsEngine.ts +++ b/packages/dev/core/src/Physics2/physicsEngine.ts @@ -1,7 +1,7 @@ import type { Nullable } from "../types"; import { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "./IPhysicsEngine"; -import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +import type { IPhysicsEngine2, IPhysicsEnginePlugin2 } from "./IPhysicsEngine"; +//import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; import type { PhysicsJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; import { _WarnImport } from "../Misc/devTools"; @@ -10,14 +10,14 @@ import { _WarnImport } from "../Misc/devTools"; * Class used to control physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export class PhysicsEngine implements IPhysicsEngine { +export class PhysicsEngine implements IPhysicsEngine2 { /** * Global value used to control the smallest number supported by the simulation */ public static Epsilon = 0.001; - private _impostors: Array = []; - private _joints: Array = []; + //private _impostors: Array = []; + //private _joints: Array = []; private _subTimeStep: number = 0; private _uniqueIdCounter = 0; @@ -30,7 +30,7 @@ export class PhysicsEngine implements IPhysicsEngine { * Factory used to create the default physics plugin. * @returns The default physics plugin */ - public static DefaultPluginFactory(): IPhysicsEnginePlugin { + public static DefaultPluginFactory(): IPhysicsEnginePlugin2 { throw _WarnImport("CannonJSPlugin"); } @@ -39,10 +39,7 @@ export class PhysicsEngine implements IPhysicsEngine { * @param gravity defines the gravity vector used by the simulation * @param _physicsPlugin defines the plugin to use (CannonJS by default) */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { - if (!this._physicsPlugin.isSupported()) { - throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); - } + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin2 = PhysicsEngine.DefaultPluginFactory()) { gravity = gravity || new Vector3(0, -9.807, 0); this.setGravity(gravity); this.setTimeStep(); @@ -98,9 +95,7 @@ export class PhysicsEngine implements IPhysicsEngine { * Release all resources */ public dispose(): void { - this._impostors.forEach(function (impostor) { - impostor.dispose(); - }); + this._physicsPlugin.dispose(); } @@ -117,130 +112,37 @@ export class PhysicsEngine implements IPhysicsEngine { * This will be done by the impostor itself. * @param impostor the impostor to add */ - public addImpostor(impostor: PhysicsImpostor) { - this._impostors.push(impostor); - impostor.uniqueId = this._uniqueIdCounter++; - //if no parent, generate the body - if (!impostor.parent) { - this._physicsPlugin.generatePhysicsBody(impostor); - } - } - - /** - * Remove an impostor from the engine. - * This impostor and its mesh will not longer be updated by the physics engine. - * @param impostor the impostor to remove - */ - public removeImpostor(impostor: PhysicsImpostor) { - const index = this._impostors.indexOf(impostor); - if (index > -1) { - const removed = this._impostors.splice(index, 1); - //Is it needed? - if (removed.length) { - this.getPhysicsPlugin().removePhysicsBody(impostor); - } - } - } - - /** - * Add a joint to the physics engine - * @param mainImpostor defines the main impostor to which the joint is added. - * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint - * @param joint defines the joint that will connect both impostors. - */ - public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { - const impostorJoint = { - mainImpostor: mainImpostor, - connectedImpostor: connectedImpostor, - joint: joint, - }; - joint.physicsPlugin = this._physicsPlugin; - this._joints.push(impostorJoint); - this._physicsPlugin.generateJoint(impostorJoint); - } - - /** - * Removes a joint from the simulation - * @param mainImpostor defines the impostor used with the joint - * @param connectedImpostor defines the other impostor connected to the main one by the joint - * @param joint defines the joint to remove - */ - public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { - const matchingJoints = this._joints.filter(function (impostorJoint) { - return impostorJoint.connectedImpostor === connectedImpostor && impostorJoint.joint === joint && impostorJoint.mainImpostor === mainImpostor; - }); - if (matchingJoints.length) { - this._physicsPlugin.removeJoint(matchingJoints[0]); - //TODO remove it from the list as well - } - } - + /** * Called by the scene. No need to call it. * @param delta defines the timespan between frames */ public _step(delta: number) { //check if any mesh has no body / requires an update - this._impostors.forEach((impostor) => { + /*this._impostors.forEach((impostor) => { if (impostor.isBodyInitRequired()) { this._physicsPlugin.generatePhysicsBody(impostor); } }); - +*/ if (delta > 0.1) { delta = 0.1; } else if (delta <= 0) { delta = 1.0 / 60.0; } - this._physicsPlugin.executeStep(delta, this._impostors); + this._physicsPlugin.executeStep(delta); } /** * Gets the current plugin used to run the simulation * @returns current plugin */ - public getPhysicsPlugin(): IPhysicsEnginePlugin { + public getPhysicsPlugin(): IPhysicsEnginePlugin2 { return this._physicsPlugin; } - /** - * Gets the list of physic impostors - * @returns an array of PhysicsImpostor - */ - public getImpostors(): Array { - return this._impostors; - } - - /** - * Gets the impostor for a physics enabled object - * @param object defines the object impersonated by the impostor - * @returns the PhysicsImpostor or null if not found - */ - public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable { - for (let i = 0; i < this._impostors.length; ++i) { - if (this._impostors[i].object === object) { - return this._impostors[i]; - } - } - - return null; - } - - /** - * Gets the impostor for a physics body object - * @param body defines physics body used by the impostor - * @returns the PhysicsImpostor or null if not found - */ - public getImpostorWithPhysicsBody(body: any): Nullable { - for (let i = 0; i < this._impostors.length; ++i) { - if (this._impostors[i].physicsBody === body) { - return this._impostors[i]; - } - } - - return null; - } + /** * Does a raycast in the physics world diff --git a/packages/dev/core/src/Physics2/physicsImpostor.ts b/packages/dev/core/src/Physics2/physicsImpostor.ts deleted file mode 100644 index 42a08961ed1..00000000000 --- a/packages/dev/core/src/Physics2/physicsImpostor.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { PhysicsBody } from "./physicsBody"; -import type { PhysicsMaterial } from "./physicsMaterial"; -import type { PhysicsShape } from "./PhysicsShape"; -import { Logger } from "../Misc/logger"; -/** - * - */ -export class PhysicsImpostor { - /** - * - */ - public body: PhysicsBody; - - /** - * - */ - public shape: PhysicsShape; - - public material: PhysicsMaterial; - - constructor( - /** - * The physics-enabled object used as the physics imposter - */ - public object: IPhysicsEnabledObject, - /** - * The type of the physics imposter - */ - public type: number, - private _options: PhysicsImpostorParameters = { mass: 0 }, - private _scene?: Scene - ) { - //sanity check! - if (!this.object) { - Logger.Error("No object was provided. A physics object is obligatory"); - return; - } - if (this.object.parent && _options.mass !== 0) { - Logger.Warn("A physics impostor has been created for an object which has a parent. Babylon physics currently works in local space so unexpected issues may occur."); - } - - // Legacy support for old syntax. - if (!this._scene && object.getScene) { - this._scene = object.getScene(); - } - // TODO - } -} diff --git a/packages/dev/core/src/Physics2/physicsJoint.ts b/packages/dev/core/src/Physics2/physicsJoint.ts index 6d4b6b8a43f..0164c17f1f9 100644 --- a/packages/dev/core/src/Physics2/physicsJoint.ts +++ b/packages/dev/core/src/Physics2/physicsJoint.ts @@ -1,5 +1,8 @@ +import type { Scene } from "../scene"; import type { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEnginePlugin, JointAxis, JointAxisLimitMode, JointMotorType } from "./IPhysicsEngine"; +import type { IPhysicsEnginePlugin2, JointAxis, PhysicsJointParameters } from "./IPhysicsEngine"; +import { JointAxisLimitMode, JointMotorType } from "./IPhysicsEngine"; +import { JointType } from "./IPhysicsEngine"; import type { PhysicsBody } from "./physicsBody"; /** @@ -8,27 +11,40 @@ import type { PhysicsBody } from "./physicsBody"; * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ export class PhysicsJoint { - protected _physicsPlugin: IPhysicsEnginePlugin; + /** + * + */ + public _pluginData: any = undefined; + protected _physicsPlugin: IPhysicsEnginePlugin2 | undefined; - //Joint Types /** * */ + constructor(type: JointType, options: PhysicsJointParameters, + scene: Scene) + { + if (!scene) { + return; + } + const physicsEngine = scene.getPhysicsEngine() as any; + this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); + this._physicsPlugin?.initJoint(this, type, options); + } /** * * @param body */ public setParentBody(body: PhysicsBody): void { - this._physicsPlugin.setParentBody(this, body); + this._physicsPlugin?.setParentBody(this, body); } /** * * @returns */ - public getParentBody(): PhysicsBody { - return this._physicsPlugin.getParentBody(this); + public getParentBody(): PhysicsBody | undefined { + return this._physicsPlugin ? this._physicsPlugin.getParentBody(this) : undefined; } /** @@ -36,15 +52,15 @@ export class PhysicsJoint { * @param body */ public setChildBody(body: PhysicsBody): void { - this._physicsPlugin.setChildBody(this, body); + this._physicsPlugin?.setChildBody(this, body); } /** * * @returns */ - public getChildBody(): PhysicsBody { - return this._physicsPlugin.getChildBody(this); + public getChildBody(): PhysicsBody | undefined { + return this._physicsPlugin ? this._physicsPlugin.getChildBody(this) : undefined; } /** @@ -54,7 +70,7 @@ export class PhysicsJoint { * @param axisY */ public setAnchorInParent(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { - this._physicsPlugin.setAnchorInParent(this, pivot, axisX, axisY); + this._physicsPlugin?.setAnchorInParent(this, pivot, axisX, axisY); } /** @@ -64,7 +80,7 @@ export class PhysicsJoint { * @param axisY */ public setAnchorInChild(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { - this._physicsPlugin.setAnchorInChild(this, pivot, axisX, axisY); + this._physicsPlugin?.setAnchorInChild(this, pivot, axisX, axisY); } /** @@ -72,7 +88,7 @@ export class PhysicsJoint { * @param isEnabled */ public setEnabled(isEnabled: boolean): void { - this._physicsPlugin.setEnabled(this, isEnabled); + this._physicsPlugin?.setEnabled(this, isEnabled); } /** @@ -80,7 +96,7 @@ export class PhysicsJoint { * @returns */ public getEnabled(): boolean { - return this._physicsPlugin.getEnabled(this); + return this._physicsPlugin ? this._physicsPlugin.getEnabled(this) : false; } /** @@ -88,7 +104,7 @@ export class PhysicsJoint { * @param isEnabled */ public setCollisionsEnabled(isEnabled: boolean): void { - this._physicsPlugin.setCollisionsEnabled(this, isEnabled); + this._physicsPlugin?.setCollisionsEnabled(this, isEnabled); } /** @@ -96,7 +112,7 @@ export class PhysicsJoint { * @returns */ public getCollisionsEnabled(): boolean { - return this._physicsPlugin.getCollisionsEnabled(this); + return this._physicsPlugin ? this._physicsPlugin.getCollisionsEnabled(this) : false; } /** @@ -105,7 +121,7 @@ export class PhysicsJoint { * @param friction */ public setAxisFriction(axis: JointAxis, friction: number): void { - this._physicsPlugin.setAxisFriction(this, axis, friction); + this._physicsPlugin?.setAxisFriction(this, axis, friction); } /** @@ -114,7 +130,7 @@ export class PhysicsJoint { * @returns */ public getAxisFriction(axis: JointAxis): number { - return this._physicsPlugin.getAxisFriction(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisFriction(this, axis) : 0; } /** @@ -123,90 +139,141 @@ export class PhysicsJoint { * @param limitMode */ public setAxisMode(axis: JointAxis, limitMode: JointAxisLimitMode): void { - this._physicsPlugin.setAxisMode(this, axis, limitMode); + this._physicsPlugin?.setAxisMode(this, axis, limitMode); } /** * * @param axis */ public getAxisMode(axis: JointAxis): JointAxisLimitMode { - return this._physicsPlugin.getAxisMode(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMode(this, axis) : JointAxisLimitMode.NONE; } /** * */ public setAxisMinLimit(axis: JointAxis, minLimit: number): void { - this._physicsPlugin.setAxisMinLimit(this, axis, minLimit); + this._physicsPlugin?.setAxisMinLimit(this, axis, minLimit); } /** * */ public getAxisMinLimit(axis: JointAxis): number { - return this._physicsPlugin.getAxisMinLimit(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMinLimit(this, axis) : 0; } /** * */ public setAxisMaxLimit(axis: JointAxis, limit: number): void { - this._physicsPlugin.setAxisMaxLimit(this, axis, limit); + this._physicsPlugin?.setAxisMaxLimit(this, axis, limit); } /** * */ public getAxisMaxLimit(axis: JointAxis): number { - return this._physicsPlugin.getAxisMaxLimit(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMaxLimit(this, axis) : 0; } /** * */ public setAxisMotorType(axis: JointAxis, motorType: JointMotorType): void { - this._physicsPlugin.setAxisMotorType(this, axis, motorType); + this._physicsPlugin?.setAxisMotorType(this, axis, motorType); } /** * */ public getAxisMotorType(axis: JointAxis): JointMotorType { - return this._physicsPlugin.getAxisMotorType(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMotorType(this, axis) : JointMotorType.NONE; } /** * */ public setAxisMotorTarget(axis: JointAxis, target: number): void { - this._physicsPlugin.setAxisMotorTarget(this, axis, target); + this._physicsPlugin?.setAxisMotorTarget(this, axis, target); } /** * */ public getAxisMotorTarget(axis: JointAxis): number { - return this._physicsPlugin.getAxisMotorTarget(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMotorTarget(this, axis) : 0; } /** * */ public setAxisMotorMaxForce(axis: JointAxis, maxForce: number): void { - this._physicsPlugin.setAxisMotorMaxForce(this, axis, maxForce); + this._physicsPlugin?.setAxisMotorMaxForce(this, axis, maxForce); } /** * */ public getAxisMotorMaxForce(axis: JointAxis): number { - return this._physicsPlugin.getAxisMotorMaxForce(this, axis); + return this._physicsPlugin ? this._physicsPlugin.getAxisMotorMaxForce(this, axis) : 0; } /** * */ public dispose(): void { - this._physicsPlugin.disposeJoint(this); + this._physicsPlugin?.disposeJoint(this); + } +} + +/** + * + */ +export class PhysicsJointBallAndSocket extends PhysicsJoint { + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) + { + super(JointType.BALL_AND_SOCKET, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); } } + +/** + * + */ +export class PhysicsJointDistance extends PhysicsJoint { + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) + { + super(JointType.DISTANCE, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + } +} + +/** + * + */ +export class PhysicsJointHinge extends PhysicsJoint { + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) + { + super(JointType.HINGE, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + } +} + +/** + * + */ +export class PhysicsJointSlider extends PhysicsJoint { + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) + { + super(JointType.SLIDER, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + } +} + +/** + * + */ +export class PhysicsJointLock extends PhysicsJoint { + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) + { + super(JointType.LOCK, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + } +} + diff --git a/packages/dev/core/src/Physics2/physicsMaterial.ts b/packages/dev/core/src/Physics2/physicsMaterial.ts index 698ac8899f7..17df5a1e1bb 100644 --- a/packages/dev/core/src/Physics2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics2/physicsMaterial.ts @@ -1,4 +1,5 @@ -import type { IPhysicsEnginePlugin } from "./IPhysicsEngine"; +import type { Scene } from "../scene"; +import type { IPhysicsEnginePlugin2 } from "./IPhysicsEngine"; /** * @@ -7,10 +8,20 @@ export class PhysicsMaterial { /** * */ - public _pluginData: any = {}; + public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePlugin; + protected _physicsPlugin: IPhysicsEnginePlugin2; + /** + * + * @param friction + * @param restitution + * @param scene + */ + constructor(friction: number, restitution: number, scene: Scene) { + this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; + this._physicsPlugin?.initMaterial(this); + } /** * * @param friction diff --git a/packages/dev/core/src/Physics2/physicsShape.ts b/packages/dev/core/src/Physics2/physicsShape.ts index 53cff2890ac..725e8394536 100644 --- a/packages/dev/core/src/Physics2/physicsShape.ts +++ b/packages/dev/core/src/Physics2/physicsShape.ts @@ -1,10 +1,12 @@ import type { TransformNode } from "../Meshes/transformNode"; -import type { BoundingBox } from "../Culling/boundingBox"; -import { IPhysicsEnginePlugin, ShapeType, PhysicsShapeParameters } from "./IPhysicsEngine"; +import { BoundingBox } from "../Culling/boundingBox"; +import { ShapeType } from "./IPhysicsEngine"; +import type { IPhysicsEnginePlugin2, PhysicsShapeParameters } from "./IPhysicsEngine"; import type { PhysicsMaterial } from "./physicsMaterial"; -import { Quaternion, Vector3 } from "../Maths/math.vector"; -import { AbstractMesh } from "../Meshes/abstractMesh"; -import { Nullable, Scene } from ".."; +import { Vector3 } from "../Maths/math.vector"; +import type { Quaternion } from "../Maths/math.vector"; +import type { AbstractMesh } from "../Meshes/abstractMesh"; +import type { Scene } from "../scene"; /** @@ -12,12 +14,19 @@ import { Nullable, Scene } from ".."; */ export class PhysicsShape { /** @internal */ - public _pluginData: any = {}; + public _pluginData: any = undefined; - protected _physicsPlugin: Nullable; + private _physicsPlugin: IPhysicsEnginePlugin2; private _type: ShapeType; + /** + * + * @param type + * @param options + * @param scene + * @returns + */ constructor(type: number, options: PhysicsShapeParameters = { }, scene: Scene) { @@ -25,10 +34,12 @@ export class PhysicsShape { if (!scene) { return; } - const physicsEngine = scene.getPhysicsEngine(); - this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); - this._physicsPlugin?.createShape(type, options); + this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; + if (!this._physicsPlugin) { + return; + } + this._physicsPlugin?.initShape(this, type, options); } /** @@ -43,7 +54,7 @@ export class PhysicsShape { * @param layer */ public setFilterLayer(layer: number): void { - this._physicsPlugin.setFilterLayer(this, layer); + this._physicsPlugin?.setFilterLayer(this, layer); } /** @@ -51,7 +62,7 @@ export class PhysicsShape { * @returns */ public getFilterLayer(): number { - return this._physicsPlugin.getFilterLayer(this); + return this._physicsPlugin ? this._physicsPlugin.getFilterLayer(this) : 0; } /** @@ -59,15 +70,15 @@ export class PhysicsShape { * @param materialId */ public setMaterial(materialId: PhysicsMaterial): void { - this._physicsPlugin.setMaterial(this, materialId); + this._physicsPlugin?.setMaterial(this, materialId); } /** * * @returns */ - public getMaterial(): PhysicsMaterial { - return this._physicsPlugin.getMaterial(this); + public getMaterial(): PhysicsMaterial | undefined { + return this._physicsPlugin ? this._physicsPlugin.getMaterial(this) : undefined; } /** @@ -75,14 +86,14 @@ export class PhysicsShape { * @param density */ public setDensity(density: number): void { - this._physicsPlugin.setDensity(this, density); + this._physicsPlugin?.setDensity(this, density); } /** * */ public getDensity(): number { - return this._physicsPlugin.getDensity(this); + return this._physicsPlugin ? this._physicsPlugin.getDensity(this) : 0; } /** @@ -91,7 +102,7 @@ export class PhysicsShape { * @param childTransform */ public addChild(newChild: PhysicsShape, childTransform: TransformNode): void { - this._physicsPlugin.addChild(this, newChild, childTransform); + this._physicsPlugin?.addChild(this, newChild, childTransform); } /** @@ -99,7 +110,7 @@ export class PhysicsShape { * @param childIndex */ public removeChild(childIndex: number): void { - this._physicsPlugin.removeChild(this, childIndex); + this._physicsPlugin?.removeChild(this, childIndex); } /** @@ -107,62 +118,132 @@ export class PhysicsShape { * @returns */ public getNumChildren(): number { - return this._physicsPlugin.getNumChildren(this); + return this._physicsPlugin ? this._physicsPlugin.getNumChildren(this) : 0; } /** * */ public getBoundingBox(): BoundingBox { - return this._physicsPlugin.getBoundingBox(this); + return this._physicsPlugin ? this._physicsPlugin.getBoundingBox(this) : new BoundingBox(Vector3.Zero(), Vector3.Zero()); } /** * */ public dispose() { - this._physicsPlugin.disposeShape(this); + this._physicsPlugin?.disposeShape(this); } } +/** + * + */ export class PhysicsShapeSphere extends PhysicsShape { - constructor(center: Vector3, radius: number) { - super(ShapeType.BOX, {center:center, radius:radius}); + /** + * + * @param center + * @param radius + * @param scene + */ + constructor(center: Vector3, radius: number, + scene: Scene) { + super(ShapeType.BOX, {center:center, radius:radius}, scene); } } +/*** + * + */ export class PhysicsShapeCapsule extends PhysicsShape { - constructor(pointA: Vector3, pointB: Vector3, radius: number) { - super(ShapeType.CAPSULE, {pointA:pointA, pointB:pointB, radius:radius}); + /** + * + * @param pointA + * @param pointB + * @param radius + * @param scene + */ + constructor(pointA: Vector3, pointB: Vector3, radius: number, + scene: Scene) { + super(ShapeType.CAPSULE, {pointA:pointA, pointB:pointB, radius:radius}, scene); } } +/** + * + */ export class PhysicsShapeCylinder extends PhysicsShape { - constructor(pointA: Vector3, pointB: Vector3, radius: number) { - super(ShapeType.CYLINDER, {pointA:pointA, pointB:pointB, radius:radius}); + /** + * + * @param pointA + * @param pointB + * @param radius + * @param scene + */ + constructor(pointA: Vector3, pointB: Vector3, radius: number, + scene: Scene) { + super(ShapeType.CYLINDER, {pointA:pointA, pointB:pointB, radius:radius}, scene); } } +/** + * + */ export class PhysicsShapeShapeBox extends PhysicsShape { - constructor(center: Vector3, rotation: Quaternion, extents: Vector3) { - super(ShapeType.BOX, {center:center, rotation:rotation, extents:extents}); + /** + * + * @param center + * @param rotation + * @param extents + * @param scene + */ + constructor(center: Vector3, rotation: Quaternion, extents: Vector3, + scene: Scene) { + super(ShapeType.BOX, {center:center, rotation:rotation, extents:extents}, scene); } } +/** + * + */ export class PhysicsShapeShapeConvexHull extends PhysicsShape { - constructor(mesh: AbstractMesh) { - super(ShapeType.CONVEX_HULL, {mesh:mesh}); + /** + * + * @param mesh + * @param scene + */ + constructor(mesh: AbstractMesh, + scene: Scene) { + super(ShapeType.CONVEX_HULL, {mesh:mesh}, scene); } } +/** + * + */ export class PhysicsShapeShapeMesh extends PhysicsShape { - constructor(mesh: AbstractMesh) { - super(ShapeType.MESH, {mesh:mesh}); + /** + * + * @param mesh + * @param scene + */ + constructor(mesh: AbstractMesh, + scene: Scene) { + super(ShapeType.MESH, {mesh:mesh}, scene); } } +/** + * + */ export class PhysicsShapeShapeContainer extends PhysicsShape { - constructor(mesh: AbstractMesh) { - super(ShapeType.CONTAINER, {}); + /** + * + * @param mesh + * @param scene + */ + constructor(mesh: AbstractMesh, + scene: Scene) { + super(ShapeType.CONTAINER, {}, scene); } } From 89353361cdc94d165069ee8925826a55977a0854 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 24 Nov 2022 19:16:02 +0100 Subject: [PATCH 04/28] format --- .../dev/core/src/Physics2/IPhysicsEngine.ts | 2 +- .../dev/core/src/Physics2/physicsAggregate.ts | 4 +- packages/dev/core/src/Physics2/physicsBody.ts | 10 +- .../dev/core/src/Physics2/physicsEngine.ts | 5 +- .../dev/core/src/Physics2/physicsJoint.ts | 42 +++--- .../dev/core/src/Physics2/physicsMaterial.ts | 8 +- .../dev/core/src/Physics2/physicsShape.ts | 138 ++++++++---------- 7 files changed, 94 insertions(+), 115 deletions(-) diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics2/IPhysicsEngine.ts index 2aea4f67262..2133dede9c1 100644 --- a/packages/dev/core/src/Physics2/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics2/IPhysicsEngine.ts @@ -13,7 +13,7 @@ export enum JointAxisLimitMode { FREE, LIMITED, LOCKED, - NONE + NONE, } export enum JointAxis { diff --git a/packages/dev/core/src/Physics2/physicsAggregate.ts b/packages/dev/core/src/Physics2/physicsAggregate.ts index a984572292a..724aa83877a 100644 --- a/packages/dev/core/src/Physics2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics2/physicsAggregate.ts @@ -8,7 +8,7 @@ import type { TransformNode } from "../Meshes/transformNode"; /** * The interface for the physics aggregate parameters */ - export interface PhysicsAggregateParameters { +export interface PhysicsAggregateParameters { /** * The mass of the physics imposter */ @@ -87,7 +87,7 @@ export class PhysicsAggregate { public shape: PhysicsShape; /** - * + * */ public material: PhysicsMaterial; diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics2/physicsBody.ts index d9fdb020f40..95e27f1d0df 100644 --- a/packages/dev/core/src/Physics2/physicsBody.ts +++ b/packages/dev/core/src/Physics2/physicsBody.ts @@ -13,9 +13,9 @@ export class PhysicsBody { private _physicsPlugin: IPhysicsEnginePlugin2 | undefined; /** - * - * @param scene - * @returns + * + * @param scene + * @returns */ constructor(scene: Scene) { if (!scene) { @@ -36,7 +36,7 @@ export class PhysicsBody { * * @returns */ - public getShape(): PhysicsShape | undefined{ + public getShape(): PhysicsShape | undefined { return this._physicsPlugin ? this._physicsPlugin.getShape(this) : undefined; } @@ -84,7 +84,7 @@ export class PhysicsBody { * * @returns */ - public getMassProperties(): MassProperties | undefined{ + public getMassProperties(): MassProperties | undefined { return this._physicsPlugin ? this._physicsPlugin.getMassProperties(this) : undefined; } diff --git a/packages/dev/core/src/Physics2/physicsEngine.ts b/packages/dev/core/src/Physics2/physicsEngine.ts index 76251782c65..336bbe90dbd 100644 --- a/packages/dev/core/src/Physics2/physicsEngine.ts +++ b/packages/dev/core/src/Physics2/physicsEngine.ts @@ -95,7 +95,6 @@ export class PhysicsEngine implements IPhysicsEngine2 { * Release all resources */ public dispose(): void { - this._physicsPlugin.dispose(); } @@ -112,7 +111,7 @@ export class PhysicsEngine implements IPhysicsEngine2 { * This will be done by the impostor itself. * @param impostor the impostor to add */ - + /** * Called by the scene. No need to call it. * @param delta defines the timespan between frames @@ -142,8 +141,6 @@ export class PhysicsEngine implements IPhysicsEngine2 { return this._physicsPlugin; } - - /** * Does a raycast in the physics world * @param from when should the ray start? diff --git a/packages/dev/core/src/Physics2/physicsJoint.ts b/packages/dev/core/src/Physics2/physicsJoint.ts index 0164c17f1f9..bb813f76dda 100644 --- a/packages/dev/core/src/Physics2/physicsJoint.ts +++ b/packages/dev/core/src/Physics2/physicsJoint.ts @@ -12,7 +12,7 @@ import type { PhysicsBody } from "./physicsBody"; */ export class PhysicsJoint { /** - * + * */ public _pluginData: any = undefined; protected _physicsPlugin: IPhysicsEnginePlugin2 | undefined; @@ -20,9 +20,7 @@ export class PhysicsJoint { /** * */ - constructor(type: JointType, options: PhysicsJointParameters, - scene: Scene) - { + constructor(type: JointType, options: PhysicsJointParameters, scene: Scene) { if (!scene) { return; } @@ -228,52 +226,46 @@ export class PhysicsJoint { } /** - * + * */ export class PhysicsJointBallAndSocket extends PhysicsJoint { - constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) - { - super(JointType.BALL_AND_SOCKET, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { + super(JointType.BALL_AND_SOCKET, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** - * + * */ export class PhysicsJointDistance extends PhysicsJoint { - constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) - { - super(JointType.DISTANCE, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { + super(JointType.DISTANCE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** - * + * */ export class PhysicsJointHinge extends PhysicsJoint { - constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) - { - super(JointType.HINGE, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { + super(JointType.HINGE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** - * + * */ export class PhysicsJointSlider extends PhysicsJoint { - constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) - { - super(JointType.SLIDER, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { + super(JointType.SLIDER, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** - * + * */ export class PhysicsJointLock extends PhysicsJoint { - constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) - { - super(JointType.LOCK, {pivotA:pivotA, pivotB:pivotB, axisA:axisA, axisB:axisB}, scene); + constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { + super(JointType.LOCK, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } - diff --git a/packages/dev/core/src/Physics2/physicsMaterial.ts b/packages/dev/core/src/Physics2/physicsMaterial.ts index 17df5a1e1bb..b2602961287 100644 --- a/packages/dev/core/src/Physics2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics2/physicsMaterial.ts @@ -13,10 +13,10 @@ export class PhysicsMaterial { protected _physicsPlugin: IPhysicsEnginePlugin2; /** - * - * @param friction - * @param restitution - * @param scene + * + * @param friction + * @param restitution + * @param scene */ constructor(friction: number, restitution: number, scene: Scene) { this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; diff --git a/packages/dev/core/src/Physics2/physicsShape.ts b/packages/dev/core/src/Physics2/physicsShape.ts index 725e8394536..8f754c081c8 100644 --- a/packages/dev/core/src/Physics2/physicsShape.ts +++ b/packages/dev/core/src/Physics2/physicsShape.ts @@ -6,8 +6,7 @@ import type { PhysicsMaterial } from "./physicsMaterial"; import { Vector3 } from "../Maths/math.vector"; import type { Quaternion } from "../Maths/math.vector"; import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { Scene } from "../scene"; - +import type { Scene } from "../scene"; /** * @@ -21,25 +20,23 @@ export class PhysicsShape { private _type: ShapeType; /** - * - * @param type - * @param options - * @param scene - * @returns + * + * @param type + * @param options + * @param scene + * @returns */ - constructor(type: number, - options: PhysicsShapeParameters = { }, - scene: Scene) { - this._type = type; - if (!scene) { - return; - } + constructor(type: number, options: PhysicsShapeParameters = {}, scene: Scene) { + this._type = type; + if (!scene) { + return; + } - this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; - if (!this._physicsPlugin) { - return; - } - this._physicsPlugin?.initShape(this, type, options); + this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; + if (!this._physicsPlugin) { + return; + } + this._physicsPlugin?.initShape(this, type, options); } /** @@ -137,113 +134,106 @@ export class PhysicsShape { } /** - * + * */ export class PhysicsShapeSphere extends PhysicsShape { /** - * - * @param center - * @param radius - * @param scene + * + * @param center + * @param radius + * @param scene */ - constructor(center: Vector3, radius: number, - scene: Scene) { - super(ShapeType.BOX, {center:center, radius:radius}, scene); + constructor(center: Vector3, radius: number, scene: Scene) { + super(ShapeType.BOX, { center: center, radius: radius }, scene); } } /*** - * + * */ export class PhysicsShapeCapsule extends PhysicsShape { /** - * - * @param pointA - * @param pointB - * @param radius - * @param scene + * + * @param pointA + * @param pointB + * @param radius + * @param scene */ - constructor(pointA: Vector3, pointB: Vector3, radius: number, - scene: Scene) { - super(ShapeType.CAPSULE, {pointA:pointA, pointB:pointB, radius:radius}, scene); + constructor(pointA: Vector3, pointB: Vector3, radius: number, scene: Scene) { + super(ShapeType.CAPSULE, { pointA: pointA, pointB: pointB, radius: radius }, scene); } } /** - * + * */ export class PhysicsShapeCylinder extends PhysicsShape { /** - * - * @param pointA - * @param pointB - * @param radius - * @param scene + * + * @param pointA + * @param pointB + * @param radius + * @param scene */ - constructor(pointA: Vector3, pointB: Vector3, radius: number, - scene: Scene) { - super(ShapeType.CYLINDER, {pointA:pointA, pointB:pointB, radius:radius}, scene); + constructor(pointA: Vector3, pointB: Vector3, radius: number, scene: Scene) { + super(ShapeType.CYLINDER, { pointA: pointA, pointB: pointB, radius: radius }, scene); } } /** - * + * */ export class PhysicsShapeShapeBox extends PhysicsShape { /** - * - * @param center - * @param rotation - * @param extents - * @param scene + * + * @param center + * @param rotation + * @param extents + * @param scene */ - constructor(center: Vector3, rotation: Quaternion, extents: Vector3, - scene: Scene) { - super(ShapeType.BOX, {center:center, rotation:rotation, extents:extents}, scene); + constructor(center: Vector3, rotation: Quaternion, extents: Vector3, scene: Scene) { + super(ShapeType.BOX, { center: center, rotation: rotation, extents: extents }, scene); } } /** - * + * */ export class PhysicsShapeShapeConvexHull extends PhysicsShape { /** - * - * @param mesh - * @param scene + * + * @param mesh + * @param scene */ - constructor(mesh: AbstractMesh, - scene: Scene) { - super(ShapeType.CONVEX_HULL, {mesh:mesh}, scene); + constructor(mesh: AbstractMesh, scene: Scene) { + super(ShapeType.CONVEX_HULL, { mesh: mesh }, scene); } } /** - * + * */ export class PhysicsShapeShapeMesh extends PhysicsShape { /** - * - * @param mesh - * @param scene + * + * @param mesh + * @param scene */ - constructor(mesh: AbstractMesh, - scene: Scene) { - super(ShapeType.MESH, {mesh:mesh}, scene); + constructor(mesh: AbstractMesh, scene: Scene) { + super(ShapeType.MESH, { mesh: mesh }, scene); } } /** - * + * */ export class PhysicsShapeShapeContainer extends PhysicsShape { /** - * - * @param mesh - * @param scene + * + * @param mesh + * @param scene */ - constructor(mesh: AbstractMesh, - scene: Scene) { + constructor(mesh: AbstractMesh, scene: Scene) { super(ShapeType.CONTAINER, {}, scene); } } From 986f3768e16fb205f5c6b6b9940ae3405cbea1ba Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 10:22:14 +0100 Subject: [PATCH 05/28] fix build errors --- packages/dev/core/src/Physics2/IPhysicsEngine.ts | 4 ++-- packages/dev/core/src/Physics2/physicsAggregate.ts | 2 +- packages/dev/core/src/Physics2/physicsBody.ts | 2 +- packages/dev/core/src/Physics2/physicsEngine.ts | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics2/IPhysicsEngine.ts index 2133dede9c1..1821b83b4f7 100644 --- a/packages/dev/core/src/Physics2/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics2/IPhysicsEngine.ts @@ -4,10 +4,10 @@ import type { PhysicsRaycastResult } from "./physicsRaycastResult"; import type { PhysicsBody } from "./physicsBody"; import type { PhysicsShape } from "./physicsShape"; import type { PhysicsJoint } from "./physicsJoint"; -import type { BoundingBox } from "../culling/boundingBox"; +import type { BoundingBox } from "../Culling/boundingBox"; import type { TransformNode } from "../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; -import type { PhysicsAggregate } from "./physicsAggregate"; +//import type { PhysicsAggregate } from "./physicsAggregate"; export enum JointAxisLimitMode { FREE, diff --git a/packages/dev/core/src/Physics2/physicsAggregate.ts b/packages/dev/core/src/Physics2/physicsAggregate.ts index 724aa83877a..688e69e0a64 100644 --- a/packages/dev/core/src/Physics2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics2/physicsAggregate.ts @@ -1,6 +1,6 @@ import type { PhysicsBody } from "./physicsBody"; import type { PhysicsMaterial } from "./physicsMaterial"; -import type { PhysicsShape } from "./PhysicsShape"; +import type { PhysicsShape } from "./physicsShape"; import { Logger } from "../Misc/logger"; import type { Scene } from "../scene"; import type { TransformNode } from "../Meshes/transformNode"; diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics2/physicsBody.ts index 95e27f1d0df..0b302a5da72 100644 --- a/packages/dev/core/src/Physics2/physicsBody.ts +++ b/packages/dev/core/src/Physics2/physicsBody.ts @@ -1,5 +1,5 @@ import type { IPhysicsEnginePlugin2, MassProperties } from "./IPhysicsEngine"; -import type { PhysicsShape } from "./PhysicsShape"; +import type { PhysicsShape } from "./physicsShape"; import { Vector3 } from "../Maths/math.vector"; import type { Scene } from "../scene"; diff --git a/packages/dev/core/src/Physics2/physicsEngine.ts b/packages/dev/core/src/Physics2/physicsEngine.ts index 336bbe90dbd..179cab84ac6 100644 --- a/packages/dev/core/src/Physics2/physicsEngine.ts +++ b/packages/dev/core/src/Physics2/physicsEngine.ts @@ -2,7 +2,7 @@ import type { Nullable } from "../types"; import { Vector3 } from "../Maths/math.vector"; import type { IPhysicsEngine2, IPhysicsEnginePlugin2 } from "./IPhysicsEngine"; //import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; -import type { PhysicsJoint } from "./physicsJoint"; +//import type { PhysicsJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; import { _WarnImport } from "../Misc/devTools"; @@ -19,7 +19,7 @@ export class PhysicsEngine implements IPhysicsEngine2 { //private _impostors: Array = []; //private _joints: Array = []; private _subTimeStep: number = 0; - private _uniqueIdCounter = 0; + //private _uniqueIdCounter = 0; /** * Gets the gravity vector used by the simulation From 94a3f3100abc5fcb273818fe9fc34cf6440e71bf Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 10:45:39 +0100 Subject: [PATCH 06/28] more fixes --- .../dev/core/src/Physics2/Plugins/index.ts | 0 packages/dev/core/src/Physics2/index.ts | 1 + .../dev/core/src/Physics2/physicsAggregate.ts | 20 ++++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 packages/dev/core/src/Physics2/Plugins/index.ts diff --git a/packages/dev/core/src/Physics2/Plugins/index.ts b/packages/dev/core/src/Physics2/Plugins/index.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/dev/core/src/Physics2/index.ts b/packages/dev/core/src/Physics2/index.ts index 36005027f22..6175a4b61c2 100644 --- a/packages/dev/core/src/Physics2/index.ts +++ b/packages/dev/core/src/Physics2/index.ts @@ -7,3 +7,4 @@ export * from "./physicsShape"; export * from "./physicsJoint"; export * from "./physicsMaterial"; export * from "./physicsAggregate"; +//export * from "./Plugins/index"; \ No newline at end of file diff --git a/packages/dev/core/src/Physics2/physicsAggregate.ts b/packages/dev/core/src/Physics2/physicsAggregate.ts index 688e69e0a64..cbdad3ab275 100644 --- a/packages/dev/core/src/Physics2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics2/physicsAggregate.ts @@ -1,5 +1,5 @@ import type { PhysicsBody } from "./physicsBody"; -import type { PhysicsMaterial } from "./physicsMaterial"; +import { PhysicsMaterial } from "./physicsMaterial"; import type { PhysicsShape } from "./physicsShape"; import { Logger } from "../Misc/logger"; import type { Scene } from "../scene"; @@ -108,7 +108,7 @@ export class PhysicsAggregate { Logger.Error("No object was provided. A physics object is obligatory"); return; } - if (this.transformNode.parent && _options.mass !== 0) { + if (this.transformNode.parent && this._options.mass !== 0) { Logger.Warn("A physics impostor has been created for an object which has a parent. Babylon physics currently works in local space so unexpected issues may occur."); } @@ -116,6 +116,20 @@ export class PhysicsAggregate { if (!this._scene && transformNode.getScene) { this._scene = transformNode.getScene(); } - // TODO + + if (!this._scene) { + return; + } + + this.material = new PhysicsMaterial(this._options.friction ? this._options.friction : 0, this._options.restitution ? this._options.restitution : 0, this._scene); + } + + /** + * + */ + public dispose(): void { + this.body.dispose(); + this.material.dispose(); + this.shape.dispose(); } } From dd23470b9fc106dace76a706b640952c9fd83f1b Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 12:08:28 +0100 Subject: [PATCH 07/28] yet another refactor --- packages/dev/core/src/Physics/index.ts | 8 +- .../src/Physics/v1/IPhysicsEnginePluginV1.ts | 68 +++++ .../Physics/{ => v1}/Plugins/ammoJSPlugin.ts | 0 .../{ => v1}/Plugins/cannonJSPlugin.ts | 0 .../src/Physics/{ => v1}/Plugins/index.ts | 0 .../Physics/{ => v1}/Plugins/oimoJSPlugin.ts | 0 packages/dev/core/src/Physics/v1/index.ts | 8 + .../core/src/Physics/v1/physicsEngineV1.ts | 254 ++++++++++++++++++ .../src/Physics/{ => v1}/physicsImpostor.ts | 0 .../core/src/Physics/{ => v1}/physicsJoint.ts | 0 .../v2/IPhysicsEngineV2.ts} | 80 +++--- .../{Physics2 => Physics/v2}/Plugins/index.ts | 0 .../src/{Physics2 => Physics/v2}/index.ts | 6 +- .../v2}/physicsAggregate.ts | 6 +- .../{Physics2 => Physics/v2}/physicsBody.ts | 8 +- .../v2/physicsConstraint.ts} | 76 +++--- .../v2/physicsEngineV2.ts} | 12 +- .../v2}/physicsMaterial.ts | 6 +- .../{Physics2 => Physics/v2}/physicsShape.ts | 18 +- .../core/src/Physics2/physicsRaycastResult.ts | 119 -------- 20 files changed, 438 insertions(+), 231 deletions(-) create mode 100644 packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts rename packages/dev/core/src/Physics/{ => v1}/Plugins/ammoJSPlugin.ts (100%) rename packages/dev/core/src/Physics/{ => v1}/Plugins/cannonJSPlugin.ts (100%) rename packages/dev/core/src/Physics/{ => v1}/Plugins/index.ts (100%) rename packages/dev/core/src/Physics/{ => v1}/Plugins/oimoJSPlugin.ts (100%) create mode 100644 packages/dev/core/src/Physics/v1/index.ts create mode 100644 packages/dev/core/src/Physics/v1/physicsEngineV1.ts rename packages/dev/core/src/Physics/{ => v1}/physicsImpostor.ts (100%) rename packages/dev/core/src/Physics/{ => v1}/physicsJoint.ts (100%) rename packages/dev/core/src/{Physics2/IPhysicsEngine.ts => Physics/v2/IPhysicsEngineV2.ts} (65%) rename packages/dev/core/src/{Physics2 => Physics/v2}/Plugins/index.ts (100%) rename packages/dev/core/src/{Physics2 => Physics/v2}/index.ts (59%) rename packages/dev/core/src/{Physics2 => Physics/v2}/physicsAggregate.ts (92%) rename packages/dev/core/src/{Physics2 => Physics/v2}/physicsBody.ts (89%) rename packages/dev/core/src/{Physics2/physicsJoint.ts => Physics/v2/physicsConstraint.ts} (59%) rename packages/dev/core/src/{Physics2/physicsEngine.ts => Physics/v2/physicsEngineV2.ts} (89%) rename packages/dev/core/src/{Physics2 => Physics/v2}/physicsMaterial.ts (83%) rename packages/dev/core/src/{Physics2 => Physics/v2}/physicsShape.ts (86%) delete mode 100644 packages/dev/core/src/Physics2/physicsRaycastResult.ts diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index c2d98afbb2b..6a14922ba41 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -1,8 +1,6 @@ /* eslint-disable import/no-internal-modules */ -export * from "./IPhysicsEngine"; -export * from "./physicsEngine"; +export * from "./v1/index"; +export * from "./v2/index"; export * from "./physicsEngineComponent"; export * from "./physicsHelper"; -export * from "./physicsImpostor"; -export * from "./physicsJoint"; -export * from "./Plugins/index"; + diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts new file mode 100644 index 00000000000..cdce9aacf3f --- /dev/null +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -0,0 +1,68 @@ +import type { Nullable } from "../../types"; +import type { Vector3, Quaternion } from "../../Maths/math.vector"; +import type { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; +import type { PhysicsRaycastResult } from "../physicsRaycastResult"; + +/** + * Interface used to describe a physics joint + */ +export interface PhysicsImpostorJoint { + /** Defines the main impostor to which the joint is linked */ + mainImpostor: PhysicsImpostor; + /** Defines the impostor that is connected to the main impostor using this joint */ + connectedImpostor: PhysicsImpostor; + /** Defines the joint itself */ + joint: PhysicsJoint; +} + +/** @internal */ +export interface IPhysicsEnginePluginV1 { + world: any; + name: string; + setGravity(gravity: Vector3): void; + setTimeStep(timeStep: number): void; + getTimeStep(): number; + executeStep(delta: number, impostors: Array): void; //not forgetting pre and post events + applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; + applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; + generatePhysicsBody(impostor: PhysicsImpostor): void; + removePhysicsBody(impostor: PhysicsImpostor): void; + generateJoint(joint: PhysicsImpostorJoint): void; + removeJoint(joint: PhysicsImpostorJoint): void; + isSupported(): boolean; + setTransformationFromPhysicsBody(impostor: PhysicsImpostor): void; + setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion): void; + setLinearVelocity(impostor: PhysicsImpostor, velocity: Nullable): void; + setAngularVelocity(impostor: PhysicsImpostor, velocity: Nullable): void; + getLinearVelocity(impostor: PhysicsImpostor): Nullable; + getAngularVelocity(impostor: PhysicsImpostor): Nullable; + setBodyMass(impostor: PhysicsImpostor, mass: number): void; + getBodyMass(impostor: PhysicsImpostor): number; + getBodyFriction(impostor: PhysicsImpostor): number; + setBodyFriction(impostor: PhysicsImpostor, friction: number): void; + getBodyRestitution(impostor: PhysicsImpostor): number; + setBodyRestitution(impostor: PhysicsImpostor, restitution: number): void; + getBodyPressure?(impostor: PhysicsImpostor): number; + setBodyPressure?(impostor: PhysicsImpostor, pressure: number): void; + getBodyStiffness?(impostor: PhysicsImpostor): number; + setBodyStiffness?(impostor: PhysicsImpostor, stiffness: number): void; + getBodyVelocityIterations?(impostor: PhysicsImpostor): number; + setBodyVelocityIterations?(impostor: PhysicsImpostor, velocityIterations: number): void; + getBodyPositionIterations?(impostor: PhysicsImpostor): number; + setBodyPositionIterations?(impostor: PhysicsImpostor, positionIterations: number): void; + appendAnchor?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, width: number, height: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void; + appendHook?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, length: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void; + sleepBody(impostor: PhysicsImpostor): void; + wakeUpBody(impostor: PhysicsImpostor): void; + raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; + //Joint Update + updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number): void; + setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number): void; + setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number): void; + getRadius(impostor: PhysicsImpostor): number; + getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void; + syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor): void; + dispose(): void; +} diff --git a/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts similarity index 100% rename from packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts rename to packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts diff --git a/packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts similarity index 100% rename from packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts rename to packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts diff --git a/packages/dev/core/src/Physics/Plugins/index.ts b/packages/dev/core/src/Physics/v1/Plugins/index.ts similarity index 100% rename from packages/dev/core/src/Physics/Plugins/index.ts rename to packages/dev/core/src/Physics/v1/Plugins/index.ts diff --git a/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts similarity index 100% rename from packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts rename to packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts diff --git a/packages/dev/core/src/Physics/v1/index.ts b/packages/dev/core/src/Physics/v1/index.ts new file mode 100644 index 00000000000..c2d98afbb2b --- /dev/null +++ b/packages/dev/core/src/Physics/v1/index.ts @@ -0,0 +1,8 @@ +/* eslint-disable import/no-internal-modules */ +export * from "./IPhysicsEngine"; +export * from "./physicsEngine"; +export * from "./physicsEngineComponent"; +export * from "./physicsHelper"; +export * from "./physicsImpostor"; +export * from "./physicsJoint"; +export * from "./Plugins/index"; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts new file mode 100644 index 00000000000..c5d3595036f --- /dev/null +++ b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts @@ -0,0 +1,254 @@ +import type { Nullable } from "../../types"; +import { Vector3 } from "../../Maths/math.vector"; +import type { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "../IPhysicsEngine"; +import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +import type { PhysicsJoint } from "./physicsJoint"; +import type { PhysicsRaycastResult } from "../physicsRaycastResult"; +import { _WarnImport } from "../../Misc/devTools"; + +/** + * Class used to control physics engine + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +export class PhysicsEngineV1 implements IPhysicsEngine { + /** + * Global value used to control the smallest number supported by the simulation + */ + public static Epsilon = 0.001; + + private _impostors: Array = []; + private _joints: Array = []; + private _subTimeStep: number = 0; + private _uniqueIdCounter = 0; + + /** + * Gets the gravity vector used by the simulation + */ + public gravity: Vector3; + + /** + * Factory used to create the default physics plugin. + * @returns The default physics plugin + */ + public static DefaultPluginFactory(): IPhysicsEnginePlugin { + throw _WarnImport("CannonJSPlugin"); + } + + /** + * Creates a new Physics Engine + * @param gravity defines the gravity vector used by the simulation + * @param _physicsPlugin defines the plugin to use (CannonJS by default) + */ + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { + if (!this._physicsPlugin.isSupported()) { + throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); + } + gravity = gravity || new Vector3(0, -9.807, 0); + this.setGravity(gravity); + this.setTimeStep(); + } + + /** + * Sets the gravity vector used by the simulation + * @param gravity defines the gravity vector to use + */ + public setGravity(gravity: Vector3): void { + this.gravity = gravity; + this._physicsPlugin.setGravity(this.gravity); + } + + /** + * Set the time step of the physics engine. + * Default is 1/60. + * To slow it down, enter 1/600 for example. + * To speed it up, 1/30 + * @param newTimeStep defines the new timestep to apply to this world. + */ + public setTimeStep(newTimeStep: number = 1 / 60) { + this._physicsPlugin.setTimeStep(newTimeStep); + } + + /** + * Get the time step of the physics engine. + * @returns the current time step + */ + public getTimeStep(): number { + return this._physicsPlugin.getTimeStep(); + } + + /** + * Set the sub time step of the physics engine. + * Default is 0 meaning there is no sub steps + * To increase physics resolution precision, set a small value (like 1 ms) + * @param subTimeStep defines the new sub timestep used for physics resolution. + */ + public setSubTimeStep(subTimeStep: number = 0) { + this._subTimeStep = subTimeStep; + } + + /** + * Get the sub time step of the physics engine. + * @returns the current sub time step + */ + public getSubTimeStep() { + return this._subTimeStep; + } + + /** + * Release all resources + */ + public dispose(): void { + this._impostors.forEach(function (impostor) { + impostor.dispose(); + }); + this._physicsPlugin.dispose(); + } + + /** + * Gets the name of the current physics plugin + * @returns the name of the plugin + */ + public getPhysicsPluginName(): string { + return this._physicsPlugin.name; + } + + /** + * Adding a new impostor for the impostor tracking. + * This will be done by the impostor itself. + * @param impostor the impostor to add + */ + public addImpostor(impostor: PhysicsImpostor) { + this._impostors.push(impostor); + impostor.uniqueId = this._uniqueIdCounter++; + //if no parent, generate the body + if (!impostor.parent) { + this._physicsPlugin.generatePhysicsBody(impostor); + } + } + + /** + * Remove an impostor from the engine. + * This impostor and its mesh will not longer be updated by the physics engine. + * @param impostor the impostor to remove + */ + public removeImpostor(impostor: PhysicsImpostor) { + const index = this._impostors.indexOf(impostor); + if (index > -1) { + const removed = this._impostors.splice(index, 1); + //Is it needed? + if (removed.length) { + this.getPhysicsPlugin().removePhysicsBody(impostor); + } + } + } + + /** + * Add a joint to the physics engine + * @param mainImpostor defines the main impostor to which the joint is added. + * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint + * @param joint defines the joint that will connect both impostors. + */ + public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { + const impostorJoint = { + mainImpostor: mainImpostor, + connectedImpostor: connectedImpostor, + joint: joint, + }; + joint.physicsPlugin = this._physicsPlugin; + this._joints.push(impostorJoint); + this._physicsPlugin.generateJoint(impostorJoint); + } + + /** + * Removes a joint from the simulation + * @param mainImpostor defines the impostor used with the joint + * @param connectedImpostor defines the other impostor connected to the main one by the joint + * @param joint defines the joint to remove + */ + public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { + const matchingJoints = this._joints.filter(function (impostorJoint) { + return impostorJoint.connectedImpostor === connectedImpostor && impostorJoint.joint === joint && impostorJoint.mainImpostor === mainImpostor; + }); + if (matchingJoints.length) { + this._physicsPlugin.removeJoint(matchingJoints[0]); + //TODO remove it from the list as well + } + } + + /** + * Called by the scene. No need to call it. + * @param delta defines the timespan between frames + */ + public _step(delta: number) { + //check if any mesh has no body / requires an update + this._impostors.forEach((impostor) => { + if (impostor.isBodyInitRequired()) { + this._physicsPlugin.generatePhysicsBody(impostor); + } + }); + + if (delta > 0.1) { + delta = 0.1; + } else if (delta <= 0) { + delta = 1.0 / 60.0; + } + + this._physicsPlugin.executeStep(delta, this._impostors); + } + + /** + * Gets the current plugin used to run the simulation + * @returns current plugin + */ + public getPhysicsPlugin(): IPhysicsEnginePlugin { + return this._physicsPlugin; + } + + /** + * Gets the list of physic impostors + * @returns an array of PhysicsImpostor + */ + public getImpostors(): Array { + return this._impostors; + } + + /** + * Gets the impostor for a physics enabled object + * @param object defines the object impersonated by the impostor + * @returns the PhysicsImpostor or null if not found + */ + public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable { + for (let i = 0; i < this._impostors.length; ++i) { + if (this._impostors[i].object === object) { + return this._impostors[i]; + } + } + + return null; + } + + /** + * Gets the impostor for a physics body object + * @param body defines physics body used by the impostor + * @returns the PhysicsImpostor or null if not found + */ + public getImpostorWithPhysicsBody(body: any): Nullable { + for (let i = 0; i < this._impostors.length; ++i) { + if (this._impostors[i].physicsBody === body) { + return this._impostors[i]; + } + } + + return null; + } + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @returns PhysicsRaycastResult + */ + public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { + return this._physicsPlugin.raycast(from, to); + } +} diff --git a/packages/dev/core/src/Physics/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts similarity index 100% rename from packages/dev/core/src/Physics/physicsImpostor.ts rename to packages/dev/core/src/Physics/v1/physicsImpostor.ts diff --git a/packages/dev/core/src/Physics/physicsJoint.ts b/packages/dev/core/src/Physics/v1/physicsJoint.ts similarity index 100% rename from packages/dev/core/src/Physics/physicsJoint.ts rename to packages/dev/core/src/Physics/v1/physicsJoint.ts diff --git a/packages/dev/core/src/Physics2/IPhysicsEngine.ts b/packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts similarity index 65% rename from packages/dev/core/src/Physics2/IPhysicsEngine.ts rename to packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts index 1821b83b4f7..7067a1a28d5 100644 --- a/packages/dev/core/src/Physics2/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts @@ -1,22 +1,22 @@ -import type { Vector3, Quaternion } from "../Maths/math.vector"; -import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { PhysicsRaycastResult } from "./physicsRaycastResult"; +import type { Vector3, Quaternion } from "../../Maths/math.vector"; +import type { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { PhysicsRaycastResult } from "../physicsRaycastResult"; import type { PhysicsBody } from "./physicsBody"; import type { PhysicsShape } from "./physicsShape"; -import type { PhysicsJoint } from "./physicsJoint"; -import type { BoundingBox } from "../Culling/boundingBox"; -import type { TransformNode } from "../Meshes/transformNode"; +import type { PhysicsConstraint } from "./physicsConstraint"; +import type { BoundingBox } from "../../Culling/boundingBox"; +import type { TransformNode } from "../../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; //import type { PhysicsAggregate } from "./physicsAggregate"; -export enum JointAxisLimitMode { +export enum ConstraintAxisLimitMode { FREE, LIMITED, LOCKED, NONE, } -export enum JointAxis { +export enum ConstraintAxis { LINEAR_X, LINEAR_Y, LINEAR_Z, @@ -25,7 +25,7 @@ export enum JointAxis { ANGULAR_Z, LINEAR_DISTANCE, } -export enum JointType { +export enum ConstraintType { BALL_AND_SOCKET, DISTANCE, HINGE, @@ -43,7 +43,7 @@ export enum ShapeType { MESH, } -export enum JointMotorType { +export enum ConstraintMotorType { NONE, VELOCITY, POSITION, @@ -59,7 +59,7 @@ export interface PhysicsShapeParameters { mesh?: AbstractMesh; } -export interface PhysicsJointParameters { +export interface PhysicsConstraintParameters { pivotA?: Vector3; pivotB?: Vector3; axisA?: Vector3; @@ -89,7 +89,7 @@ export interface MassProperties { } /** @internal */ -export interface IPhysicsEnginePlugin2 { +export interface IPhysicsEnginePluginV2 { /** * */ @@ -146,33 +146,33 @@ export interface IPhysicsEnginePlugin2 { getRestitution(material: PhysicsMaterial): number; disposeMaterial(material: PhysicsMaterial): void; - // joint - initJoint(joint: PhysicsJoint, type: JointType, options: PhysicsJointParameters): void; - setParentBody(joint: PhysicsJoint, body: PhysicsBody): void; - getParentBody(joint: PhysicsJoint): PhysicsBody; - setChildBody(joint: PhysicsJoint, body: PhysicsBody): void; - getChildBody(joint: PhysicsJoint): PhysicsBody; - setAnchorInParent(joint: PhysicsJoint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; - setAnchorInChild(joint: PhysicsJoint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; - setEnabled(joint: PhysicsJoint, isEnabled: boolean): void; - getEnabled(joint: PhysicsJoint): boolean; - setCollisionsEnabled(joint: PhysicsJoint, isEnabled: boolean): void; - getCollisionsEnabled(joint: PhysicsJoint): boolean; - setAxisFriction(joint: PhysicsJoint, axis: JointAxis, friction: number): void; - getAxisFriction(joint: PhysicsJoint, axis: JointAxis): number; - setAxisMode(joint: PhysicsJoint, axis: JointAxis, limitMode: JointAxisLimitMode): void; - getAxisMode(joint: PhysicsJoint, axis: JointAxis): JointAxisLimitMode; - setAxisMinLimit(joint: PhysicsJoint, axis: JointAxis, minLimit: number): void; - getAxisMinLimit(joint: PhysicsJoint, axis: JointAxis): number; - setAxisMaxLimit(joint: PhysicsJoint, axis: JointAxis, limit: number): void; - getAxisMaxLimit(joint: PhysicsJoint, axis: JointAxis): number; - setAxisMotorType(joint: PhysicsJoint, axis: JointAxis, motorType: JointMotorType): void; - getAxisMotorType(joint: PhysicsJoint, axis: JointAxis): JointMotorType; - setAxisMotorTarget(joint: PhysicsJoint, axis: JointAxis, target: number): void; - getAxisMotorTarget(joint: PhysicsJoint, axis: JointAxis): number; - setAxisMotorMaxForce(joint: PhysicsJoint, axis: JointAxis, maxForce: number): void; - getAxisMotorMaxForce(joint: PhysicsJoint, axis: JointAxis): number; - disposeJoint(joint: PhysicsJoint): void; + // constraint + initConstraint(constraint: PhysicsConstraint, type: ConstraintType, options: PhysicsConstraintParameters): void; + setParentBody(constraint: PhysicsConstraint, body: PhysicsBody): void; + getParentBody(constraint: PhysicsConstraint): PhysicsBody; + setChildBody(constraint: PhysicsConstraint, body: PhysicsBody): void; + getChildBody(constraint: PhysicsConstraint): PhysicsBody; + setAnchorInParent(constraint: PhysicsConstraint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; + setAnchorInChild(constraint: PhysicsConstraint, pivot: Vector3, axisX: Vector3, axisY: Vector3): void; + setEnabled(constraint: PhysicsConstraint, isEnabled: boolean): void; + getEnabled(constraint: PhysicsConstraint): boolean; + setCollisionsEnabled(constraint: PhysicsConstraint, isEnabled: boolean): void; + getCollisionsEnabled(constraint: PhysicsConstraint): boolean; + setAxisFriction(constraint: PhysicsConstraint, axis: ConstraintAxis, friction: number): void; + getAxisFriction(constraint: PhysicsConstraint, axis: ConstraintAxis): number; + setAxisMode(constraint: PhysicsConstraint, axis: ConstraintAxis, limitMode: ConstraintAxisLimitMode): void; + getAxisMode(constraint: PhysicsConstraint, axis: ConstraintAxis): ConstraintAxisLimitMode; + setAxisMinLimit(constraint: PhysicsConstraint, axis: ConstraintAxis, minLimit: number): void; + getAxisMinLimit(constraint: PhysicsConstraint, axis: ConstraintAxis): number; + setAxisMaxLimit(constraint: PhysicsConstraint, axis: ConstraintAxis, limit: number): void; + getAxisMaxLimit(constraint: PhysicsConstraint, axis: ConstraintAxis): number; + setAxisMotorType(constraint: PhysicsConstraint, axis: ConstraintAxis, motorType: ConstraintMotorType): void; + getAxisMotorType(constraint: PhysicsConstraint, axis: ConstraintAxis): ConstraintMotorType; + setAxisMotorTarget(constraint: PhysicsConstraint, axis: ConstraintAxis, target: number): void; + getAxisMotorTarget(constraint: PhysicsConstraint, axis: ConstraintAxis): number; + setAxisMotorMaxForce(constraint: PhysicsConstraint, axis: ConstraintAxis, maxForce: number): void; + getAxisMotorMaxForce(constraint: PhysicsConstraint, axis: ConstraintAxis): number; + disposeConstraint(constraint: PhysicsConstraint): void; // raycast raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; @@ -262,4 +262,4 @@ export interface IPhysicsEngine2 { * @param delta defines the timespan between frames */ _step(delta: number): void; -} +} \ No newline at end of file diff --git a/packages/dev/core/src/Physics2/Plugins/index.ts b/packages/dev/core/src/Physics/v2/Plugins/index.ts similarity index 100% rename from packages/dev/core/src/Physics2/Plugins/index.ts rename to packages/dev/core/src/Physics/v2/Plugins/index.ts diff --git a/packages/dev/core/src/Physics2/index.ts b/packages/dev/core/src/Physics/v2/index.ts similarity index 59% rename from packages/dev/core/src/Physics2/index.ts rename to packages/dev/core/src/Physics/v2/index.ts index 6175a4b61c2..66290024bf9 100644 --- a/packages/dev/core/src/Physics2/index.ts +++ b/packages/dev/core/src/Physics/v2/index.ts @@ -1,10 +1,8 @@ /* eslint-disable import/no-internal-modules */ -export * from "./IPhysicsEngine"; -export * from "./physicsEngine"; +export * from "./physicsEngineV2"; export * from "./physicsBody"; -export * from "./physicsRaycastResult"; export * from "./physicsShape"; -export * from "./physicsJoint"; +export * from "./physicsConstraint"; export * from "./physicsMaterial"; export * from "./physicsAggregate"; //export * from "./Plugins/index"; \ No newline at end of file diff --git a/packages/dev/core/src/Physics2/physicsAggregate.ts b/packages/dev/core/src/Physics/v2/physicsAggregate.ts similarity index 92% rename from packages/dev/core/src/Physics2/physicsAggregate.ts rename to packages/dev/core/src/Physics/v2/physicsAggregate.ts index cbdad3ab275..1e65e7ddab4 100644 --- a/packages/dev/core/src/Physics2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics/v2/physicsAggregate.ts @@ -1,9 +1,9 @@ import type { PhysicsBody } from "./physicsBody"; import { PhysicsMaterial } from "./physicsMaterial"; import type { PhysicsShape } from "./physicsShape"; -import { Logger } from "../Misc/logger"; -import type { Scene } from "../scene"; -import type { TransformNode } from "../Meshes/transformNode"; +import { Logger } from "../../Misc/logger"; +import type { Scene } from "../../scene"; +import type { TransformNode } from "../../Meshes/transformNode"; /** * The interface for the physics aggregate parameters diff --git a/packages/dev/core/src/Physics2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts similarity index 89% rename from packages/dev/core/src/Physics2/physicsBody.ts rename to packages/dev/core/src/Physics/v2/physicsBody.ts index 0b302a5da72..a4851efe74f 100644 --- a/packages/dev/core/src/Physics2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -1,7 +1,7 @@ -import type { IPhysicsEnginePlugin2, MassProperties } from "./IPhysicsEngine"; +import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEngineV2"; import type { PhysicsShape } from "./physicsShape"; -import { Vector3 } from "../Maths/math.vector"; -import type { Scene } from "../scene"; +import { Vector3 } from "../../Maths/math.vector"; +import type { Scene } from "../../scene"; /** * @@ -10,7 +10,7 @@ export class PhysicsBody { /** @internal */ public _pluginData: any = undefined; - private _physicsPlugin: IPhysicsEnginePlugin2 | undefined; + private _physicsPlugin: IPhysicsEnginePluginV2 | undefined; /** * diff --git a/packages/dev/core/src/Physics2/physicsJoint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts similarity index 59% rename from packages/dev/core/src/Physics2/physicsJoint.ts rename to packages/dev/core/src/Physics/v2/physicsConstraint.ts index bb813f76dda..40f15f2048d 100644 --- a/packages/dev/core/src/Physics2/physicsJoint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -1,32 +1,32 @@ -import type { Scene } from "../scene"; -import type { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEnginePlugin2, JointAxis, PhysicsJointParameters } from "./IPhysicsEngine"; -import { JointAxisLimitMode, JointMotorType } from "./IPhysicsEngine"; -import { JointType } from "./IPhysicsEngine"; +import type { Scene } from "../../scene"; +import type { Vector3 } from "../../Maths/math.vector"; +import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters } from "./IPhysicsEngineV2"; +import { ConstraintType, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEngineV2"; +//import { ConstraintType } from "./IPhysicsEngine2"; import type { PhysicsBody } from "./physicsBody"; /** - * This is a holder class for the physics joint created by the physics plugin - * It holds a set of functions to control the underlying joint + * This is a holder class for the physics constraint created by the physics plugin + * It holds a set of functions to control the underlying constraint * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export class PhysicsJoint { +export class PhysicsConstraint { /** * */ public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePlugin2 | undefined; + protected _physicsPlugin: IPhysicsEnginePluginV2 | undefined; /** * */ - constructor(type: JointType, options: PhysicsJointParameters, scene: Scene) { + constructor(type: ConstraintType, options: PhysicsConstraintParameters, scene: Scene) { if (!scene) { return; } const physicsEngine = scene.getPhysicsEngine() as any; this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); - this._physicsPlugin?.initJoint(this, type, options); + this._physicsPlugin?.initConstraint(this, type, options); } /** @@ -118,7 +118,7 @@ export class PhysicsJoint { * @param axis * @param friction */ - public setAxisFriction(axis: JointAxis, friction: number): void { + public setAxisFriction(axis: ConstraintAxis, friction: number): void { this._physicsPlugin?.setAxisFriction(this, axis, friction); } @@ -127,7 +127,7 @@ export class PhysicsJoint { * @param axis * @returns */ - public getAxisFriction(axis: JointAxis): number { + public getAxisFriction(axis: ConstraintAxis): number { return this._physicsPlugin ? this._physicsPlugin.getAxisFriction(this, axis) : 0; } @@ -136,84 +136,84 @@ export class PhysicsJoint { * @param axis * @param limitMode */ - public setAxisMode(axis: JointAxis, limitMode: JointAxisLimitMode): void { + public setAxisMode(axis: ConstraintAxis, limitMode: ConstraintAxisLimitMode): void { this._physicsPlugin?.setAxisMode(this, axis, limitMode); } /** * * @param axis */ - public getAxisMode(axis: JointAxis): JointAxisLimitMode { - return this._physicsPlugin ? this._physicsPlugin.getAxisMode(this, axis) : JointAxisLimitMode.NONE; + public getAxisMode(axis: ConstraintAxis): ConstraintAxisLimitMode { + return this._physicsPlugin ? this._physicsPlugin.getAxisMode(this, axis) : ConstraintAxisLimitMode.NONE; } /** * */ - public setAxisMinLimit(axis: JointAxis, minLimit: number): void { + public setAxisMinLimit(axis: ConstraintAxis, minLimit: number): void { this._physicsPlugin?.setAxisMinLimit(this, axis, minLimit); } /** * */ - public getAxisMinLimit(axis: JointAxis): number { + public getAxisMinLimit(axis: ConstraintAxis): number { return this._physicsPlugin ? this._physicsPlugin.getAxisMinLimit(this, axis) : 0; } /** * */ - public setAxisMaxLimit(axis: JointAxis, limit: number): void { + public setAxisMaxLimit(axis: ConstraintAxis, limit: number): void { this._physicsPlugin?.setAxisMaxLimit(this, axis, limit); } /** * */ - public getAxisMaxLimit(axis: JointAxis): number { + public getAxisMaxLimit(axis: ConstraintAxis): number { return this._physicsPlugin ? this._physicsPlugin.getAxisMaxLimit(this, axis) : 0; } /** * */ - public setAxisMotorType(axis: JointAxis, motorType: JointMotorType): void { + public setAxisMotorType(axis: ConstraintAxis, motorType: ConstraintMotorType): void { this._physicsPlugin?.setAxisMotorType(this, axis, motorType); } /** * */ - public getAxisMotorType(axis: JointAxis): JointMotorType { - return this._physicsPlugin ? this._physicsPlugin.getAxisMotorType(this, axis) : JointMotorType.NONE; + public getAxisMotorType(axis: ConstraintAxis): ConstraintMotorType { + return this._physicsPlugin ? this._physicsPlugin.getAxisMotorType(this, axis) : ConstraintMotorType.NONE; } /** * */ - public setAxisMotorTarget(axis: JointAxis, target: number): void { + public setAxisMotorTarget(axis: ConstraintAxis, target: number): void { this._physicsPlugin?.setAxisMotorTarget(this, axis, target); } /** * */ - public getAxisMotorTarget(axis: JointAxis): number { + public getAxisMotorTarget(axis: ConstraintAxis): number { return this._physicsPlugin ? this._physicsPlugin.getAxisMotorTarget(this, axis) : 0; } /** * */ - public setAxisMotorMaxForce(axis: JointAxis, maxForce: number): void { + public setAxisMotorMaxForce(axis: ConstraintAxis, maxForce: number): void { this._physicsPlugin?.setAxisMotorMaxForce(this, axis, maxForce); } /** * */ - public getAxisMotorMaxForce(axis: JointAxis): number { + public getAxisMotorMaxForce(axis: ConstraintAxis): number { return this._physicsPlugin ? this._physicsPlugin.getAxisMotorMaxForce(this, axis) : 0; } @@ -221,51 +221,51 @@ export class PhysicsJoint { * */ public dispose(): void { - this._physicsPlugin?.disposeJoint(this); + this._physicsPlugin?.disposeConstraint(this); } } /** * */ -export class PhysicsJointBallAndSocket extends PhysicsJoint { +export class PhysicsConstraintBallAndSocket extends PhysicsConstraint { constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { - super(JointType.BALL_AND_SOCKET, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); + super(ConstraintType.BALL_AND_SOCKET, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** * */ -export class PhysicsJointDistance extends PhysicsJoint { +export class PhysicsConstraintDistance extends PhysicsConstraint { constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { - super(JointType.DISTANCE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); + super(ConstraintType.DISTANCE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** * */ -export class PhysicsJointHinge extends PhysicsJoint { +export class PhysicsConstraintHinge extends PhysicsConstraint { constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { - super(JointType.HINGE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); + super(ConstraintType.HINGE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** * */ -export class PhysicsJointSlider extends PhysicsJoint { +export class PhysicsConstraintSlider extends PhysicsConstraint { constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { - super(JointType.SLIDER, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); + super(ConstraintType.SLIDER, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } /** * */ -export class PhysicsJointLock extends PhysicsJoint { +export class PhysicsConstraintLock extends PhysicsConstraint { constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { - super(JointType.LOCK, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); + super(ConstraintType.LOCK, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } } diff --git a/packages/dev/core/src/Physics2/physicsEngine.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts similarity index 89% rename from packages/dev/core/src/Physics2/physicsEngine.ts rename to packages/dev/core/src/Physics/v2/physicsEngineV2.ts index 179cab84ac6..13b292f142d 100644 --- a/packages/dev/core/src/Physics2/physicsEngine.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -1,16 +1,16 @@ -import type { Nullable } from "../types"; -import { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEngine2, IPhysicsEnginePlugin2 } from "./IPhysicsEngine"; +import type { Nullable } from "../../types"; +import { Vector3 } from "../../Maths/math.vector"; +import type { IPhysicsEngine, IPhysicsEnginePlugin } from "../IPhysicsEngine"; //import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; //import type { PhysicsJoint } from "./physicsJoint"; -import type { PhysicsRaycastResult } from "./physicsRaycastResult"; -import { _WarnImport } from "../Misc/devTools"; +import type { PhysicsRaycastResult } from "../physicsRaycastResult"; +import { _WarnImport } from "../../Misc/devTools"; /** * Class used to control physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export class PhysicsEngine implements IPhysicsEngine2 { +export class PhysicsEngineV2 implements IPhysicsEngine { /** * Global value used to control the smallest number supported by the simulation */ diff --git a/packages/dev/core/src/Physics2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts similarity index 83% rename from packages/dev/core/src/Physics2/physicsMaterial.ts rename to packages/dev/core/src/Physics/v2/physicsMaterial.ts index b2602961287..7ad53f4aea5 100644 --- a/packages/dev/core/src/Physics2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -1,5 +1,5 @@ -import type { Scene } from "../scene"; -import type { IPhysicsEnginePlugin2 } from "./IPhysicsEngine"; +import type { Scene } from "../../scene"; +import type { IPhysicsEnginePluginV2 } from "./IPhysicsEngineV2"; /** * @@ -10,7 +10,7 @@ export class PhysicsMaterial { */ public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePlugin2; + protected _physicsPlugin: IPhysicsEnginePluginV2; /** * diff --git a/packages/dev/core/src/Physics2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts similarity index 86% rename from packages/dev/core/src/Physics2/physicsShape.ts rename to packages/dev/core/src/Physics/v2/physicsShape.ts index 8f754c081c8..b0e30ee0893 100644 --- a/packages/dev/core/src/Physics2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -1,12 +1,12 @@ -import type { TransformNode } from "../Meshes/transformNode"; -import { BoundingBox } from "../Culling/boundingBox"; -import { ShapeType } from "./IPhysicsEngine"; -import type { IPhysicsEnginePlugin2, PhysicsShapeParameters } from "./IPhysicsEngine"; +import type { TransformNode } from "../../Meshes/transformNode"; +import { BoundingBox } from "../../Culling/boundingBox"; +import { ShapeType } from "./IPhysicsEngineV2"; +import type { IPhysicsEnginePluginV2, PhysicsShapeParameters } from "./IPhysicsEngineV2"; import type { PhysicsMaterial } from "./physicsMaterial"; -import { Vector3 } from "../Maths/math.vector"; -import type { Quaternion } from "../Maths/math.vector"; -import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { Scene } from "../scene"; +import { Vector3 } from "../../Maths/math.vector"; +import type { Quaternion } from "../../Maths/math.vector"; +import type { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { Scene } from "../../scene"; /** * @@ -15,7 +15,7 @@ export class PhysicsShape { /** @internal */ public _pluginData: any = undefined; - private _physicsPlugin: IPhysicsEnginePlugin2; + private _physicsPlugin: IPhysicsEnginePluginV2; private _type: ShapeType; diff --git a/packages/dev/core/src/Physics2/physicsRaycastResult.ts b/packages/dev/core/src/Physics2/physicsRaycastResult.ts deleted file mode 100644 index 8443538c0b2..00000000000 --- a/packages/dev/core/src/Physics2/physicsRaycastResult.ts +++ /dev/null @@ -1,119 +0,0 @@ -import { Vector3 } from "../Maths/math.vector"; - -/** - * Holds the data for the raycast result - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine - */ -export class PhysicsRaycastResult { - private _hasHit: boolean = false; - - private _hitDistance: number = 0; - private _hitNormalWorld: Vector3 = Vector3.Zero(); - private _hitPointWorld: Vector3 = Vector3.Zero(); - private _rayFromWorld: Vector3 = Vector3.Zero(); - private _rayToWorld: Vector3 = Vector3.Zero(); - - /** - * Gets if there was a hit - */ - get hasHit(): boolean { - return this._hasHit; - } - - /** - * Gets the distance from the hit - */ - get hitDistance(): number { - return this._hitDistance; - } - - /** - * Gets the hit normal/direction in the world - */ - get hitNormalWorld(): Vector3 { - return this._hitNormalWorld; - } - - /** - * Gets the hit point in the world - */ - get hitPointWorld(): Vector3 { - return this._hitPointWorld; - } - - /** - * Gets the ray "start point" of the ray in the world - */ - get rayFromWorld(): Vector3 { - return this._rayFromWorld; - } - - /** - * Gets the ray "end point" of the ray in the world - */ - get rayToWorld(): Vector3 { - return this._rayToWorld; - } - - /** - * Sets the hit data (normal & point in world space) - * @param hitNormalWorld defines the normal in world space - * @param hitPointWorld defines the point in world space - */ - public setHitData(hitNormalWorld: IXYZ, hitPointWorld: IXYZ) { - this._hasHit = true; - this._hitNormalWorld = new Vector3(hitNormalWorld.x, hitNormalWorld.y, hitNormalWorld.z); - this._hitPointWorld = new Vector3(hitPointWorld.x, hitPointWorld.y, hitPointWorld.z); - } - - /** - * Sets the distance from the start point to the hit point - * @param distance - */ - public setHitDistance(distance: number) { - this._hitDistance = distance; - } - - /** - * Calculates the distance manually - */ - public calculateHitDistance() { - this._hitDistance = Vector3.Distance(this._rayFromWorld, this._hitPointWorld); - } - - /** - * Resets all the values to default - * @param from The from point on world space - * @param to The to point on world space - */ - public reset(from: Vector3 = Vector3.Zero(), to: Vector3 = Vector3.Zero()) { - this._rayFromWorld = from; - this._rayToWorld = to; - - this._hasHit = false; - this._hitDistance = 0; - - this._hitNormalWorld = Vector3.Zero(); - this._hitPointWorld = Vector3.Zero(); - } -} - -/** - * Interface for the size containing width and height - */ -interface IXYZ { - /** - * X - */ - x: number; - - /** - * Y - */ - y: number; - - /** - * Z - */ - z: number; -} From 4018dc0f9e6951b43ba2713e3535d2567fdde5ec Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 14:17:06 +0100 Subject: [PATCH 08/28] v1 / v2 folders --- .../dev/core/src/Physics/IPhysicsEngine.ts | 85 +----- .../dev/core/src/Physics/physicsEngine.ts | 254 ------------------ .../src/Physics/physicsEngineComponent.ts | 146 ++-------- packages/dev/core/src/Physics/v1/index.ts | 7 +- .../Physics/v1/physicsEngineComponentV1.ts | 193 +++++++++++++ .../core/src/Physics/v1/physicsEngineV1.ts | 9 +- .../core/src/Physics/v1/physicsImpostor.ts | 32 +-- .../dev/core/src/Physics/v1/physicsJoint.ts | 8 +- ...sEngineV2.ts => IPhysicsEnginePluginV2.ts} | 84 ------ .../dev/core/src/Physics/v2/physicsBody.ts | 2 +- .../core/src/Physics/v2/physicsConstraint.ts | 4 +- .../Physics/v2/physicsEngineComponentV2.ts | 19 ++ .../core/src/Physics/v2/physicsEngineV2.ts | 10 +- .../core/src/Physics/v2/physicsMaterial.ts | 2 +- .../dev/core/src/Physics/v2/physicsShape.ts | 4 +- 15 files changed, 281 insertions(+), 578 deletions(-) delete mode 100644 packages/dev/core/src/Physics/physicsEngine.ts create mode 100644 packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts rename packages/dev/core/src/Physics/v2/{IPhysicsEngineV2.ts => IPhysicsEnginePluginV2.ts} (71%) create mode 100644 packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index 7f0f12047ae..704ed59136e 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -1,71 +1,10 @@ -import type { Nullable } from "../types"; +//import type { Nullable } from "../types"; import type { Vector3, Quaternion } from "../Maths/math.vector"; -import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; -import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; +//import type { AbstractMesh } from "../Meshes/abstractMesh"; +//import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +//import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; -/** - * Interface used to describe a physics joint - */ -export interface PhysicsImpostorJoint { - /** Defines the main impostor to which the joint is linked */ - mainImpostor: PhysicsImpostor; - /** Defines the impostor that is connected to the main impostor using this joint */ - connectedImpostor: PhysicsImpostor; - /** Defines the joint itself */ - joint: PhysicsJoint; -} - -/** @internal */ -export interface IPhysicsEnginePlugin { - world: any; - name: string; - setGravity(gravity: Vector3): void; - setTimeStep(timeStep: number): void; - getTimeStep(): number; - executeStep(delta: number, impostors: Array): void; //not forgetting pre and post events - applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; - applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; - generatePhysicsBody(impostor: PhysicsImpostor): void; - removePhysicsBody(impostor: PhysicsImpostor): void; - generateJoint(joint: PhysicsImpostorJoint): void; - removeJoint(joint: PhysicsImpostorJoint): void; - isSupported(): boolean; - setTransformationFromPhysicsBody(impostor: PhysicsImpostor): void; - setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion): void; - setLinearVelocity(impostor: PhysicsImpostor, velocity: Nullable): void; - setAngularVelocity(impostor: PhysicsImpostor, velocity: Nullable): void; - getLinearVelocity(impostor: PhysicsImpostor): Nullable; - getAngularVelocity(impostor: PhysicsImpostor): Nullable; - setBodyMass(impostor: PhysicsImpostor, mass: number): void; - getBodyMass(impostor: PhysicsImpostor): number; - getBodyFriction(impostor: PhysicsImpostor): number; - setBodyFriction(impostor: PhysicsImpostor, friction: number): void; - getBodyRestitution(impostor: PhysicsImpostor): number; - setBodyRestitution(impostor: PhysicsImpostor, restitution: number): void; - getBodyPressure?(impostor: PhysicsImpostor): number; - setBodyPressure?(impostor: PhysicsImpostor, pressure: number): void; - getBodyStiffness?(impostor: PhysicsImpostor): number; - setBodyStiffness?(impostor: PhysicsImpostor, stiffness: number): void; - getBodyVelocityIterations?(impostor: PhysicsImpostor): number; - setBodyVelocityIterations?(impostor: PhysicsImpostor, velocityIterations: number): void; - getBodyPositionIterations?(impostor: PhysicsImpostor): number; - setBodyPositionIterations?(impostor: PhysicsImpostor, positionIterations: number): void; - appendAnchor?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, width: number, height: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void; - appendHook?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, length: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void; - sleepBody(impostor: PhysicsImpostor): void; - wakeUpBody(impostor: PhysicsImpostor): void; - raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; - //Joint Update - updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number): void; - setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number): void; - setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number): void; - getRadius(impostor: PhysicsImpostor): number; - getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void; - syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor): void; - dispose(): void; -} /** * Interface used to define a physics engine @@ -128,14 +67,14 @@ export interface IPhysicsEngine { * This will be done by the impostor itself. * @param impostor the impostor to add */ - addImpostor(impostor: PhysicsImpostor): void; + //addImpostor(impostor: PhysicsImpostor): void; /** * Remove an impostor from the engine. * This impostor and its mesh will not longer be updated by the physics engine. * @param impostor the impostor to remove */ - removeImpostor(impostor: PhysicsImpostor): void; + //removeImpostor(impostor: PhysicsImpostor): void; /** * Add a joint to the physics engine @@ -143,7 +82,7 @@ export interface IPhysicsEngine { * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint * @param joint defines the joint that will connect both impostors. */ - addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; + //addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; /** * Removes a joint from the simulation @@ -151,33 +90,33 @@ export interface IPhysicsEngine { * @param connectedImpostor defines the other impostor connected to the main one by the joint * @param joint defines the joint to remove */ - removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; + //removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; /** * Gets the current plugin used to run the simulation * @returns current plugin */ - getPhysicsPlugin(): IPhysicsEnginePlugin; + //getPhysicsPlugin(): IPhysicsEnginePlugin; /** * Gets the list of physic impostors * @returns an array of PhysicsImpostor */ - getImpostors(): Array; + //getImpostors(): Array; /** * Gets the impostor for a physics enabled object * @param object defines the object impersonated by the impostor * @returns the PhysicsImpostor or null if not found */ - getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable; + //getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable; /** * Gets the impostor for a physics body object * @param body defines physics body used by the impostor * @returns the PhysicsImpostor or null if not found */ - getImpostorWithPhysicsBody(body: any): Nullable; + //getImpostorWithPhysicsBody(body: any): Nullable; /** * Does a raycast in the physics world diff --git a/packages/dev/core/src/Physics/physicsEngine.ts b/packages/dev/core/src/Physics/physicsEngine.ts deleted file mode 100644 index d966cf87989..00000000000 --- a/packages/dev/core/src/Physics/physicsEngine.ts +++ /dev/null @@ -1,254 +0,0 @@ -import type { Nullable } from "../types"; -import { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "./IPhysicsEngine"; -import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; -import type { PhysicsJoint } from "./physicsJoint"; -import type { PhysicsRaycastResult } from "./physicsRaycastResult"; -import { _WarnImport } from "../Misc/devTools"; - -/** - * Class used to control physics engine - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine - */ -export class PhysicsEngine implements IPhysicsEngine { - /** - * Global value used to control the smallest number supported by the simulation - */ - public static Epsilon = 0.001; - - private _impostors: Array = []; - private _joints: Array = []; - private _subTimeStep: number = 0; - private _uniqueIdCounter = 0; - - /** - * Gets the gravity vector used by the simulation - */ - public gravity: Vector3; - - /** - * Factory used to create the default physics plugin. - * @returns The default physics plugin - */ - public static DefaultPluginFactory(): IPhysicsEnginePlugin { - throw _WarnImport("CannonJSPlugin"); - } - - /** - * Creates a new Physics Engine - * @param gravity defines the gravity vector used by the simulation - * @param _physicsPlugin defines the plugin to use (CannonJS by default) - */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { - if (!this._physicsPlugin.isSupported()) { - throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); - } - gravity = gravity || new Vector3(0, -9.807, 0); - this.setGravity(gravity); - this.setTimeStep(); - } - - /** - * Sets the gravity vector used by the simulation - * @param gravity defines the gravity vector to use - */ - public setGravity(gravity: Vector3): void { - this.gravity = gravity; - this._physicsPlugin.setGravity(this.gravity); - } - - /** - * Set the time step of the physics engine. - * Default is 1/60. - * To slow it down, enter 1/600 for example. - * To speed it up, 1/30 - * @param newTimeStep defines the new timestep to apply to this world. - */ - public setTimeStep(newTimeStep: number = 1 / 60) { - this._physicsPlugin.setTimeStep(newTimeStep); - } - - /** - * Get the time step of the physics engine. - * @returns the current time step - */ - public getTimeStep(): number { - return this._physicsPlugin.getTimeStep(); - } - - /** - * Set the sub time step of the physics engine. - * Default is 0 meaning there is no sub steps - * To increase physics resolution precision, set a small value (like 1 ms) - * @param subTimeStep defines the new sub timestep used for physics resolution. - */ - public setSubTimeStep(subTimeStep: number = 0) { - this._subTimeStep = subTimeStep; - } - - /** - * Get the sub time step of the physics engine. - * @returns the current sub time step - */ - public getSubTimeStep() { - return this._subTimeStep; - } - - /** - * Release all resources - */ - public dispose(): void { - this._impostors.forEach(function (impostor) { - impostor.dispose(); - }); - this._physicsPlugin.dispose(); - } - - /** - * Gets the name of the current physics plugin - * @returns the name of the plugin - */ - public getPhysicsPluginName(): string { - return this._physicsPlugin.name; - } - - /** - * Adding a new impostor for the impostor tracking. - * This will be done by the impostor itself. - * @param impostor the impostor to add - */ - public addImpostor(impostor: PhysicsImpostor) { - this._impostors.push(impostor); - impostor.uniqueId = this._uniqueIdCounter++; - //if no parent, generate the body - if (!impostor.parent) { - this._physicsPlugin.generatePhysicsBody(impostor); - } - } - - /** - * Remove an impostor from the engine. - * This impostor and its mesh will not longer be updated by the physics engine. - * @param impostor the impostor to remove - */ - public removeImpostor(impostor: PhysicsImpostor) { - const index = this._impostors.indexOf(impostor); - if (index > -1) { - const removed = this._impostors.splice(index, 1); - //Is it needed? - if (removed.length) { - this.getPhysicsPlugin().removePhysicsBody(impostor); - } - } - } - - /** - * Add a joint to the physics engine - * @param mainImpostor defines the main impostor to which the joint is added. - * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint - * @param joint defines the joint that will connect both impostors. - */ - public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { - const impostorJoint = { - mainImpostor: mainImpostor, - connectedImpostor: connectedImpostor, - joint: joint, - }; - joint.physicsPlugin = this._physicsPlugin; - this._joints.push(impostorJoint); - this._physicsPlugin.generateJoint(impostorJoint); - } - - /** - * Removes a joint from the simulation - * @param mainImpostor defines the impostor used with the joint - * @param connectedImpostor defines the other impostor connected to the main one by the joint - * @param joint defines the joint to remove - */ - public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) { - const matchingJoints = this._joints.filter(function (impostorJoint) { - return impostorJoint.connectedImpostor === connectedImpostor && impostorJoint.joint === joint && impostorJoint.mainImpostor === mainImpostor; - }); - if (matchingJoints.length) { - this._physicsPlugin.removeJoint(matchingJoints[0]); - //TODO remove it from the list as well - } - } - - /** - * Called by the scene. No need to call it. - * @param delta defines the timespan between frames - */ - public _step(delta: number) { - //check if any mesh has no body / requires an update - this._impostors.forEach((impostor) => { - if (impostor.isBodyInitRequired()) { - this._physicsPlugin.generatePhysicsBody(impostor); - } - }); - - if (delta > 0.1) { - delta = 0.1; - } else if (delta <= 0) { - delta = 1.0 / 60.0; - } - - this._physicsPlugin.executeStep(delta, this._impostors); - } - - /** - * Gets the current plugin used to run the simulation - * @returns current plugin - */ - public getPhysicsPlugin(): IPhysicsEnginePlugin { - return this._physicsPlugin; - } - - /** - * Gets the list of physic impostors - * @returns an array of PhysicsImpostor - */ - public getImpostors(): Array { - return this._impostors; - } - - /** - * Gets the impostor for a physics enabled object - * @param object defines the object impersonated by the impostor - * @returns the PhysicsImpostor or null if not found - */ - public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable { - for (let i = 0; i < this._impostors.length; ++i) { - if (this._impostors[i].object === object) { - return this._impostors[i]; - } - } - - return null; - } - - /** - * Gets the impostor for a physics body object - * @param body defines physics body used by the impostor - * @returns the PhysicsImpostor or null if not found - */ - public getImpostorWithPhysicsBody(body: any): Nullable { - for (let i = 0; i < this._impostors.length; ++i) { - if (this._impostors[i].physicsBody === body) { - return this._impostors[i]; - } - } - - return null; - } - - /** - * Does a raycast in the physics world - * @param from when should the ray start? - * @param to when should the ray end? - * @returns PhysicsRaycastResult - */ - public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { - return this._physicsPlugin.raycast(from, to); - } -} diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index cf760609583..aecf2c69f66 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -1,21 +1,21 @@ import type { Nullable } from "../types"; import { Logger } from "../Misc/logger"; -import type { Observer } from "../Misc/observable"; +//import type { Observer } from "../Misc/observable"; import { Observable } from "../Misc/observable"; import type { Vector3 } from "../Maths/math.vector"; -import type { Mesh } from "../Meshes/mesh"; -import { AbstractMesh } from "../Meshes/abstractMesh"; +//import type { Mesh } from "../Meshes/mesh"; +//import { AbstractMesh } from "../Meshes/abstractMesh"; import type { ISceneComponent } from "../sceneComponent"; import { SceneComponentConstants } from "../sceneComponent"; import { Scene } from "../scene"; -import type { Node } from "../node"; +//import type { Node } from "../node"; -import type { IPhysicsEngine, IPhysicsEnginePlugin } from "./IPhysicsEngine"; -import { PhysicsEngine } from "./physicsEngine"; -import type { PhysicsImpostor } from "./physicsImpostor"; -import { PhysicsJoint } from "./physicsJoint"; +import type { IPhysicsEngine } from "./IPhysicsEngine"; declare module "../scene" { + /** + * + */ export interface Scene { /** @internal (Backing field) */ _physicsEngine: Nullable; @@ -34,7 +34,7 @@ declare module "../scene" { * @param plugin defines the physics engine to be used. defaults to CannonJS. * @returns a boolean indicating if the physics engine was initialized */ - enablePhysics(gravity?: Nullable, plugin?: IPhysicsEnginePlugin): boolean; + enablePhysics(gravity?: Nullable, plugin?: any/*IPhysicsEnginePlugin*/): boolean; /** * Disables and disposes the physics engine associated with the scene @@ -92,7 +92,7 @@ Scene.prototype.enablePhysics = function (gravity: Nullable = null, plu } try { - this._physicsEngine = new PhysicsEngine(gravity, plugin); + this._physicsEngine = null; // new PhysicsEngine(gravity, plugin); this._physicsTimeAccumulator = 0; return true; } catch (e) { @@ -126,12 +126,13 @@ Scene.prototype.isPhysicsEnabled = function (): boolean { * @param compound defines the compound to delete */ Scene.prototype.deleteCompoundImpostor = function (compound: any): void { - const mesh: AbstractMesh = compound.parts[0].mesh; + //const mesh: AbstractMesh = compound.parts[0].mesh; - if (mesh.physicsImpostor) { - mesh.physicsImpostor.dispose(/*true*/); - mesh.physicsImpostor = null; - } + // TODO + //if (mesh.physicsImpostor) { + // mesh.physicsImpostor.dispose(/*true*/); + // mesh.physicsImpostor = null; + //} }; /** @@ -156,121 +157,6 @@ Scene.prototype._advancePhysicsEngineStep = function (step: number) { } }; -declare module "../Meshes/abstractMesh" { - export interface AbstractMesh { - /** @internal */ - _physicsImpostor: Nullable; - - /** - * Gets or sets impostor used for physic simulation - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics - */ - physicsImpostor: Nullable; - - /** - * Gets the current physics impostor - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics - * @returns a physics impostor or null - */ - getPhysicsImpostor(): Nullable; - - /** Apply a physic impulse to the mesh - * @param force defines the force to apply - * @param contactPoint defines where to apply the force - * @returns the current mesh - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine - */ - applyImpulse(force: Vector3, contactPoint: Vector3): AbstractMesh; - - /** - * Creates a physic joint between two meshes - * @param otherMesh defines the other mesh to use - * @param pivot1 defines the pivot to use on this mesh - * @param pivot2 defines the pivot to use on the other mesh - * @param options defines additional options (can be plugin dependent) - * @returns the current mesh - * @see https://www.babylonjs-playground.com/#0BS5U0#0 - */ - setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): AbstractMesh; - - /** @internal */ - _disposePhysicsObserver: Nullable>; - } -} - -Object.defineProperty(AbstractMesh.prototype, "physicsImpostor", { - get: function (this: AbstractMesh) { - return this._physicsImpostor; - }, - set: function (this: AbstractMesh, value: Nullable) { - if (this._physicsImpostor === value) { - return; - } - if (this._disposePhysicsObserver) { - this.onDisposeObservable.remove(this._disposePhysicsObserver); - } - - this._physicsImpostor = value; - - if (value) { - this._disposePhysicsObserver = this.onDisposeObservable.add(() => { - // Physics - if (this.physicsImpostor) { - this.physicsImpostor.dispose(/*!doNotRecurse*/); - this.physicsImpostor = null; - } - }); - } - }, - enumerable: true, - configurable: true, -}); - -/** - * Gets the current physics impostor - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics - * @returns a physics impostor or null - */ -AbstractMesh.prototype.getPhysicsImpostor = function (): Nullable { - return this.physicsImpostor; -}; - -/** - * Apply a physic impulse to the mesh - * @param force defines the force to apply - * @param contactPoint defines where to apply the force - * @returns the current mesh - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine - */ -AbstractMesh.prototype.applyImpulse = function (force: Vector3, contactPoint: Vector3): AbstractMesh { - if (!this.physicsImpostor) { - return this; - } - this.physicsImpostor.applyImpulse(force, contactPoint); - return this; -}; - -/** - * Creates a physic joint between two meshes - * @param otherMesh defines the other mesh to use - * @param pivot1 defines the pivot to use on this mesh - * @param pivot2 defines the pivot to use on the other mesh - * @param options defines additional options (can be plugin dependent) - * @returns the current mesh - * @see https://www.babylonjs-playground.com/#0BS5U0#0 - */ -AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): AbstractMesh { - if (!this.physicsImpostor || !otherMesh.physicsImpostor) { - return this; - } - this.physicsImpostor.createJoint(otherMesh.physicsImpostor, PhysicsJoint.HingeJoint, { - mainPivot: pivot1, - connectedPivot: pivot2, - nativeParams: options, - }); - return this; -}; - /** * Defines the physics engine scene component responsible to manage a physics engine */ diff --git a/packages/dev/core/src/Physics/v1/index.ts b/packages/dev/core/src/Physics/v1/index.ts index c2d98afbb2b..58ab15ee0b5 100644 --- a/packages/dev/core/src/Physics/v1/index.ts +++ b/packages/dev/core/src/Physics/v1/index.ts @@ -1,8 +1,7 @@ /* eslint-disable import/no-internal-modules */ -export * from "./IPhysicsEngine"; -export * from "./physicsEngine"; -export * from "./physicsEngineComponent"; -export * from "./physicsHelper"; +export * from "./IPhysicsEnginePluginV1"; +export * from "./physicsEngineV1"; +export * from "./physicsEngineComponentV1"; export * from "./physicsImpostor"; export * from "./physicsJoint"; export * from "./Plugins/index"; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts new file mode 100644 index 00000000000..2a57fbf3526 --- /dev/null +++ b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts @@ -0,0 +1,193 @@ +import type { Nullable } from "../../types"; +//import { Logger } from "../../Misc/logger"; +import type { Observer } from "../../Misc/observable"; +import { Observable } from "../../Misc/observable"; +import type { Vector3 } from "../../Maths/math.vector"; +import type { Mesh } from "../../Meshes/mesh"; +import { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { ISceneComponent } from "../../sceneComponent"; +import { SceneComponentConstants } from "../../sceneComponent"; +import type { Scene } from "../../scene"; +import type { Node } from "../../node"; + +//import type { IPhysicsEngine, IPhysicsEnginePlugin } from "../IPhysicsEngine"; +//import { PhysicsEngine } from "../physicsEngine"; +import type { PhysicsImpostor } from "./physicsImpostor"; +import { PhysicsJoint } from "./physicsJoint"; + +declare module "../../Meshes/abstractMesh" { + /** + * + */ + export interface AbstractMesh { + /** @internal */ + _physicsImpostor: Nullable; + + /** + * Gets or sets impostor used for physic simulation + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics + */ + physicsImpostor: Nullable; + + /** + * Gets the current physics impostor + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics + * @returns a physics impostor or null + */ + getPhysicsImpostor(): Nullable; + + /** Apply a physic impulse to the mesh + * @param force defines the force to apply + * @param contactPoint defines where to apply the force + * @returns the current mesh + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ + applyImpulse(force: Vector3, contactPoint: Vector3): AbstractMesh; + + /** + * Creates a physic joint between two meshes + * @param otherMesh defines the other mesh to use + * @param pivot1 defines the pivot to use on this mesh + * @param pivot2 defines the pivot to use on the other mesh + * @param options defines additional options (can be plugin dependent) + * @returns the current mesh + * @see https://www.babylonjs-playground.com/#0BS5U0#0 + */ + setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): AbstractMesh; + + /** @internal */ + _disposePhysicsObserver: Nullable>; + } +} + +Object.defineProperty(AbstractMesh.prototype, "physicsImpostor", { + get: function (this: AbstractMesh) { + return this._physicsImpostor; + }, + set: function (this: AbstractMesh, value: Nullable) { + if (this._physicsImpostor === value) { + return; + } + if (this._disposePhysicsObserver) { + this.onDisposeObservable.remove(this._disposePhysicsObserver); + } + + this._physicsImpostor = value; + + if (value) { + this._disposePhysicsObserver = this.onDisposeObservable.add(() => { + // Physics + if (this.physicsImpostor) { + this.physicsImpostor.dispose(/*!doNotRecurse*/); + this.physicsImpostor = null; + } + }); + } + }, + enumerable: true, + configurable: true, +}); + +/** + * Gets the current physics impostor + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics + * @returns a physics impostor or null + */ +AbstractMesh.prototype.getPhysicsImpostor = function (): Nullable { + return this.physicsImpostor; +}; + +/** + * Apply a physic impulse to the mesh + * @param force defines the force to apply + * @param contactPoint defines where to apply the force + * @returns the current mesh + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +AbstractMesh.prototype.applyImpulse = function (force: Vector3, contactPoint: Vector3): AbstractMesh { + if (!this.physicsImpostor) { + return this; + } + this.physicsImpostor.applyImpulse(force, contactPoint); + return this; +}; + +/** + * Creates a physic joint between two meshes + * @param otherMesh defines the other mesh to use + * @param pivot1 defines the pivot to use on this mesh + * @param pivot2 defines the pivot to use on the other mesh + * @param options defines additional options (can be plugin dependent) + * @returns the current mesh + * @see https://www.babylonjs-playground.com/#0BS5U0#0 + */ +AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): AbstractMesh { + if (!this.physicsImpostor || !otherMesh.physicsImpostor) { + return this; + } + this.physicsImpostor.createJoint(otherMesh.physicsImpostor, PhysicsJoint.HingeJoint, { + mainPivot: pivot1, + connectedPivot: pivot2, + nativeParams: options, + }); + return this; +}; + +/** + * Defines the physics engine scene component responsible to manage a physics engine + */ +export class PhysicsEngineSceneComponent implements ISceneComponent { + /** + * The component name helpful to identify the component in the list of scene components. + */ + public readonly name = SceneComponentConstants.NAME_PHYSICSENGINE; + + /** + * The scene the component belongs to. + */ + public scene: Scene; + + /** + * Creates a new instance of the component for the given scene + * @param scene Defines the scene to register the component in + */ + constructor(scene: Scene) { + this.scene = scene; + this.scene.onBeforePhysicsObservable = new Observable(); + this.scene.onAfterPhysicsObservable = new Observable(); + + // Replace the function used to get the deterministic frame time + this.scene.getDeterministicFrameTime = () => { + if (this.scene._physicsEngine) { + return this.scene._physicsEngine.getTimeStep() * 1000; + } + + return 1000.0 / 60.0; + }; + } + + /** + * Registers the component in a given scene + */ + public register(): void {} + + /** + * Rebuilds the elements related to this component in case of + * context lost for instance. + */ + public rebuild(): void { + // Nothing to do for this component + } + + /** + * Disposes the component and the associated resources + */ + public dispose(): void { + this.scene.onBeforePhysicsObservable.clear(); + this.scene.onAfterPhysicsObservable.clear(); + + if (this.scene._physicsEngine) { + this.scene.disablePhysicsEngine(); + } + } +} diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts index c5d3595036f..684408ff8af 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts @@ -1,6 +1,7 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "../IPhysicsEngine"; +import type { PhysicsImpostorJoint,IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; +import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; import type { PhysicsJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "../physicsRaycastResult"; @@ -30,7 +31,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * Factory used to create the default physics plugin. * @returns The default physics plugin */ - public static DefaultPluginFactory(): IPhysicsEnginePlugin { + public static DefaultPluginFactory(): IPhysicsEnginePluginV1 { throw _WarnImport("CannonJSPlugin"); } @@ -39,7 +40,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * @param gravity defines the gravity vector used by the simulation * @param _physicsPlugin defines the plugin to use (CannonJS by default) */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePluginV1 = PhysicsEngineV1.DefaultPluginFactory()) { if (!this._physicsPlugin.isSupported()) { throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); } @@ -200,7 +201,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - public getPhysicsPlugin(): IPhysicsEnginePlugin { + public getPhysicsPlugin(): IPhysicsEnginePluginV1 { return this._physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v1/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts index 65b3fedc2bb..c1b774b6390 100644 --- a/packages/dev/core/src/Physics/v1/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/v1/physicsImpostor.ts @@ -1,18 +1,20 @@ -import type { Nullable, IndicesArray } from "../types"; -import { Logger } from "../Misc/logger"; -import { ArrayTools } from "../Misc/arrayTools"; -import type { Matrix } from "../Maths/math.vector"; -import { Vector3, Quaternion } from "../Maths/math.vector"; -import type { TransformNode } from "../Meshes/transformNode"; -import { AbstractMesh } from "../Meshes/abstractMesh"; -import { Mesh } from "../Meshes/mesh"; -import type { Scene } from "../scene"; -import type { Bone } from "../Bones/bone"; -import type { BoundingInfo } from "../Culling/boundingInfo"; -import type { IPhysicsEngine } from "./IPhysicsEngine"; +import type { Nullable, IndicesArray } from "../../types"; +import { Logger } from "../../Misc/logger"; +import { ArrayTools } from "../../Misc/arrayTools"; +import type { Matrix } from "../../Maths/math.vector"; +import { Vector3, Quaternion } from "../../Maths/math.vector"; +import type { TransformNode } from "../../Meshes/transformNode"; +import { AbstractMesh } from "../../Meshes/abstractMesh"; +import { Mesh } from "../../Meshes/mesh"; +import type { Scene } from "../../scene"; +import type { Bone } from "../../Bones/bone"; +import type { BoundingInfo } from "../../Culling/boundingInfo"; +//import type { IPhysicsEngine } from "../IPhysicsEngine"; +import type { PhysicsEngineV1 } from "./physicsEngineV1"; + import type { PhysicsJointData } from "./physicsJoint"; import { PhysicsJoint } from "./physicsJoint"; -import { Space } from "../Maths/math.axis"; +import { Space } from "../../Maths/math.axis"; /** * The interface for the physics imposter parameters @@ -216,7 +218,7 @@ export class PhysicsImpostor { /** @internal */ public _pluginData: any = {}; - private _physicsEngine: Nullable; + private _physicsEngine: Nullable; //The native cannon/oimo/energy physics body object. private _physicsBody: any; private _bodyUpdateRequired: boolean = false; @@ -470,7 +472,7 @@ export class PhysicsImpostor { this.soft = true; } - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine() as any; if (!this._physicsEngine) { Logger.Error("Physics not enabled. Please use scene.enablePhysics(...) before creating impostors."); } else { diff --git a/packages/dev/core/src/Physics/v1/physicsJoint.ts b/packages/dev/core/src/Physics/v1/physicsJoint.ts index cc43201f0fe..9944b34ec2b 100644 --- a/packages/dev/core/src/Physics/v1/physicsJoint.ts +++ b/packages/dev/core/src/Physics/v1/physicsJoint.ts @@ -1,5 +1,5 @@ -import type { Vector3 } from "../Maths/math.vector"; -import type { IPhysicsEnginePlugin } from "./IPhysicsEngine"; +import type { Vector3 } from "../../Maths/math.vector"; +import type { IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; /** * Interface for Physics-Joint data * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine @@ -39,7 +39,7 @@ export interface PhysicsJointData { */ export class PhysicsJoint { private _physicsJoint: any; - protected _physicsPlugin: IPhysicsEnginePlugin; + protected _physicsPlugin: IPhysicsEnginePluginV1; /** * Initializes the physics joint @@ -80,7 +80,7 @@ export class PhysicsJoint { /** * Sets the physics plugin */ - public set physicsPlugin(physicsPlugin: IPhysicsEnginePlugin) { + public set physicsPlugin(physicsPlugin: IPhysicsEnginePluginV1) { this._physicsPlugin = physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts similarity index 71% rename from packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts rename to packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts index 7067a1a28d5..1ffca48e999 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts @@ -179,87 +179,3 @@ export interface IPhysicsEnginePluginV2 { dispose(): void; } - -/** - * Interface used to define a physics engine - * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine - */ -export interface IPhysicsEngine2 { - /** - * Gets the gravity vector used by the simulation - */ - gravity: Vector3; - - /** - * Sets the gravity vector used by the simulation - * @param gravity defines the gravity vector to use - */ - setGravity(gravity: Vector3): void; - - /** - * Set the time step of the physics engine. - * Default is 1/60. - * To slow it down, enter 1/600 for example. - * To speed it up, 1/30 - * @param newTimeStep the new timestep to apply to this world. - */ - setTimeStep(newTimeStep: number): void; - - /** - * Get the time step of the physics engine. - * @returns the current time step - */ - getTimeStep(): number; - - /** - * Set the sub time step of the physics engine. - * Default is 0 meaning there is no sub steps - * To increase physics resolution precision, set a small value (like 1 ms) - * @param subTimeStep defines the new sub timestep used for physics resolution. - */ - setSubTimeStep(subTimeStep: number): void; - - /** - * Get the sub time step of the physics engine. - * @returns the current sub time step - */ - getSubTimeStep(): number; - - /** - * Release all resources - */ - dispose(): void; - - /** - * Gets the name of the current physics plugin - * @returns the name of the plugin - */ - getPhysicsPluginName(): string; - - // Helpers - /*getBodies(): Array; - addAggregate(impostor: PhysicsAggregate): void; - removeAggregate(impostor: PhysicsAggregate): void; -*/ - /** - * Gets the current plugin used to run the simulation - * @returns current plugin - */ - getPhysicsPlugin(): IPhysicsEnginePlugin2; - - //**************************************************************************** - - /** - * Does a raycast in the physics world - * @param from when should the ray start? - * @param to when should the ray end? - * @returns PhysicsRaycastResult - */ - raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; - - /** - * Called by the scene. No need to call it. - * @param delta defines the timespan between frames - */ - _step(delta: number): void; -} \ No newline at end of file diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index a4851efe74f..2fde83ff1f5 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -1,4 +1,4 @@ -import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEngineV2"; +import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEnginePluginV2"; import type { PhysicsShape } from "./physicsShape"; import { Vector3 } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index 40f15f2048d..9be7fb0e6dc 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -1,7 +1,7 @@ import type { Scene } from "../../scene"; import type { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters } from "./IPhysicsEngineV2"; -import { ConstraintType, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEngineV2"; +import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters } from "./IPhysicsEnginePluginV2"; +import { ConstraintType, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePluginV2"; //import { ConstraintType } from "./IPhysicsEngine2"; import type { PhysicsBody } from "./physicsBody"; diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts new file mode 100644 index 00000000000..3ec28229358 --- /dev/null +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts @@ -0,0 +1,19 @@ +/* +import type { Nullable } from "../types"; +import { Logger } from "../Misc/logger"; +import type { Observer } from "../Misc/observable"; +import { Observable } from "../Misc/observable"; +import type { Vector3 } from "../Maths/math.vector"; +import type { Mesh } from "../Meshes/mesh"; +import { AbstractMesh } from "../Meshes/abstractMesh"; +import type { ISceneComponent } from "../sceneComponent"; +import { SceneComponentConstants } from "../sceneComponent"; +import { Scene } from "../scene"; +import type { Node } from "../node"; + +import type { IPhysicsEngine, IPhysicsEnginePlugin } from "./IPhysicsEngine"; +import { PhysicsEngine } from "./physicsEngine"; +import type { PhysicsImpostor } from "./physicsImpostor"; +import { PhysicsJoint } from "./physicsJoint"; +*/ +// TODO \ No newline at end of file diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index 13b292f142d..fbc4b6a36fc 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -1,6 +1,8 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEngine, IPhysicsEnginePlugin } from "../IPhysicsEngine"; +import type { IPhysicsEngine } from "../IPhysicsEngine"; +import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2" + //import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; //import type { PhysicsJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "../physicsRaycastResult"; @@ -30,7 +32,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * Factory used to create the default physics plugin. * @returns The default physics plugin */ - public static DefaultPluginFactory(): IPhysicsEnginePlugin2 { + public static DefaultPluginFactory(): IPhysicsEnginePluginV2 { throw _WarnImport("CannonJSPlugin"); } @@ -39,7 +41,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * @param gravity defines the gravity vector used by the simulation * @param _physicsPlugin defines the plugin to use (CannonJS by default) */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin2 = PhysicsEngine.DefaultPluginFactory()) { + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePluginV2 = PhysicsEngineV2.DefaultPluginFactory()) { gravity = gravity || new Vector3(0, -9.807, 0); this.setGravity(gravity); this.setTimeStep(); @@ -137,7 +139,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - public getPhysicsPlugin(): IPhysicsEnginePlugin2 { + public getPhysicsPlugin(): IPhysicsEnginePluginV2 { return this._physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts index 7ad53f4aea5..ba0541d1be9 100644 --- a/packages/dev/core/src/Physics/v2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -1,5 +1,5 @@ import type { Scene } from "../../scene"; -import type { IPhysicsEnginePluginV2 } from "./IPhysicsEngineV2"; +import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; /** * diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index b0e30ee0893..764dc8f072c 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -1,7 +1,7 @@ import type { TransformNode } from "../../Meshes/transformNode"; import { BoundingBox } from "../../Culling/boundingBox"; -import { ShapeType } from "./IPhysicsEngineV2"; -import type { IPhysicsEnginePluginV2, PhysicsShapeParameters } from "./IPhysicsEngineV2"; +import { ShapeType } from "./IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePluginV2, PhysicsShapeParameters } from "./IPhysicsEnginePluginV2"; import type { PhysicsMaterial } from "./physicsMaterial"; import { Vector3 } from "../../Maths/math.vector"; import type { Quaternion } from "../../Maths/math.vector"; From a65489099bc7ccce31477e36fc8500d0930b2a09 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 14:49:24 +0100 Subject: [PATCH 09/28] finishing rough edges of refactor --- packages/dev/core/src/Physics/IPhysicsEngine.ts | 4 ++-- packages/dev/core/src/Physics/index.ts | 2 +- .../dev/core/src/Physics/physicsEngineComponent.ts | 2 +- .../dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts | 6 ++++++ packages/dev/core/src/Physics/v1/physicsImpostor.ts | 10 +++------- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index 704ed59136e..b0830c296ac 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -1,5 +1,5 @@ //import type { Nullable } from "../types"; -import type { Vector3, Quaternion } from "../Maths/math.vector"; +import type { Vector3 } from "../Maths/math.vector"; //import type { AbstractMesh } from "../Meshes/abstractMesh"; //import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; //import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; @@ -96,7 +96,7 @@ export interface IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - //getPhysicsPlugin(): IPhysicsEnginePlugin; + getPhysicsPlugin(): any; /** * Gets the list of physic impostors diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index 6a14922ba41..dba83948b75 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -1,6 +1,6 @@ /* eslint-disable import/no-internal-modules */ export * from "./v1/index"; export * from "./v2/index"; -export * from "./physicsEngineComponent"; +//export * from "./physicsEngineComponent"; export * from "./physicsHelper"; diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index aecf2c69f66..8610cbf4898 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -79,7 +79,7 @@ Scene.prototype.getPhysicsEngine = function (): Nullable { * @param plugin defines the physics engine to be used. defaults to CannonJS. * @returns a boolean indicating if the physics engine was initialized */ -Scene.prototype.enablePhysics = function (gravity: Nullable = null, plugin?: IPhysicsEnginePlugin): boolean { +Scene.prototype.enablePhysics = function (gravity: Nullable = null, plugin?: any): boolean { if (this._physicsEngine) { return true; } diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts index cdce9aacf3f..6614aaebddf 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -19,7 +19,13 @@ export interface PhysicsImpostorJoint { /** @internal */ export interface IPhysicsEnginePluginV1 { + /** + * + */ world: any; + /** + * + */ name: string; setGravity(gravity: Vector3): void; setTimeStep(timeStep: number): void; diff --git a/packages/dev/core/src/Physics/v1/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts index c1b774b6390..161801a3112 100644 --- a/packages/dev/core/src/Physics/v1/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/v1/physicsImpostor.ts @@ -911,13 +911,9 @@ export class PhysicsImpostor { public onCollideEvent: Nullable<(collider: PhysicsImpostor, collidedWith: PhysicsImpostor) => void> = null; /** - * event and body object due to cannon's event-based architecture. - * @param e - * @param e.body - * @param e.point - * @param e.distance - * @param e.impulse - * @param e.normal + * + * @param e + * @returns */ public onCollide = (e: { body: any; point: Nullable; distance: number; impulse: number; normal: Nullable }) => { if (!this._onPhysicsCollideCallbacks.length && !this.onCollideEvent) { From e1cb850968120fafd772705258149cfe78108c07 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 14:53:47 +0100 Subject: [PATCH 10/28] code format --- packages/dev/core/src/Physics/IPhysicsEngine.ts | 1 - packages/dev/core/src/Physics/physicsEngineComponent.ts | 5 ++--- packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts | 4 ++-- .../dev/core/src/Physics/v1/physicsEngineComponentV1.ts | 2 +- packages/dev/core/src/Physics/v1/physicsEngineV1.ts | 2 +- packages/dev/core/src/Physics/v1/physicsImpostor.ts | 6 +++--- packages/dev/core/src/Physics/v2/physicsAggregate.ts | 4 ++-- .../dev/core/src/Physics/v2/physicsEngineComponentV2.ts | 2 +- packages/dev/core/src/Physics/v2/physicsEngineV2.ts | 2 +- 9 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index b0830c296ac..2255e99d24f 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -5,7 +5,6 @@ import type { Vector3 } from "../Maths/math.vector"; //import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; - /** * Interface used to define a physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index 8610cbf4898..95e903761b3 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -14,7 +14,7 @@ import type { IPhysicsEngine } from "./IPhysicsEngine"; declare module "../scene" { /** - * + * */ export interface Scene { /** @internal (Backing field) */ @@ -34,7 +34,7 @@ declare module "../scene" { * @param plugin defines the physics engine to be used. defaults to CannonJS. * @returns a boolean indicating if the physics engine was initialized */ - enablePhysics(gravity?: Nullable, plugin?: any/*IPhysicsEnginePlugin*/): boolean; + enablePhysics(gravity?: Nullable, plugin?: any /*IPhysicsEnginePlugin*/): boolean; /** * Disables and disposes the physics engine associated with the scene @@ -127,7 +127,6 @@ Scene.prototype.isPhysicsEnabled = function (): boolean { */ Scene.prototype.deleteCompoundImpostor = function (compound: any): void { //const mesh: AbstractMesh = compound.parts[0].mesh; - // TODO //if (mesh.physicsImpostor) { // mesh.physicsImpostor.dispose(/*true*/); diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts index 6614aaebddf..5b4aa65c059 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -20,11 +20,11 @@ export interface PhysicsImpostorJoint { /** @internal */ export interface IPhysicsEnginePluginV1 { /** - * + * */ world: any; /** - * + * */ name: string; setGravity(gravity: Vector3): void; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts index 2a57fbf3526..6d26fe71585 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts @@ -17,7 +17,7 @@ import { PhysicsJoint } from "./physicsJoint"; declare module "../../Meshes/abstractMesh" { /** - * + * */ export interface AbstractMesh { /** @internal */ diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts index 684408ff8af..b7b6876f52b 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts @@ -1,6 +1,6 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; -import type { PhysicsImpostorJoint,IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; +import type { PhysicsImpostorJoint, IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; import type { PhysicsJoint } from "./physicsJoint"; diff --git a/packages/dev/core/src/Physics/v1/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts index 161801a3112..c7a00bb9125 100644 --- a/packages/dev/core/src/Physics/v1/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/v1/physicsImpostor.ts @@ -911,9 +911,9 @@ export class PhysicsImpostor { public onCollideEvent: Nullable<(collider: PhysicsImpostor, collidedWith: PhysicsImpostor) => void> = null; /** - * - * @param e - * @returns + * + * @param e + * @returns */ public onCollide = (e: { body: any; point: Nullable; distance: number; impulse: number; normal: Nullable }) => { if (!this._onPhysicsCollideCallbacks.length && !this.onCollideEvent) { diff --git a/packages/dev/core/src/Physics/v2/physicsAggregate.ts b/packages/dev/core/src/Physics/v2/physicsAggregate.ts index 1e65e7ddab4..c27756f8eb3 100644 --- a/packages/dev/core/src/Physics/v2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics/v2/physicsAggregate.ts @@ -116,7 +116,7 @@ export class PhysicsAggregate { if (!this._scene && transformNode.getScene) { this._scene = transformNode.getScene(); } - + if (!this._scene) { return; } @@ -125,7 +125,7 @@ export class PhysicsAggregate { } /** - * + * */ public dispose(): void { this.body.dispose(); diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts index 3ec28229358..20e29270ae8 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts @@ -16,4 +16,4 @@ import { PhysicsEngine } from "./physicsEngine"; import type { PhysicsImpostor } from "./physicsImpostor"; import { PhysicsJoint } from "./physicsJoint"; */ -// TODO \ No newline at end of file +// TODO diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index fbc4b6a36fc..7d619a5f8f3 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -1,7 +1,7 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; import type { IPhysicsEngine } from "../IPhysicsEngine"; -import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2" +import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; //import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; //import type { PhysicsJoint } from "./physicsJoint"; From d0078780ad837184b58c89ce881705be7f500e72 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 15:08:03 +0100 Subject: [PATCH 11/28] moving helpers to v1 --- packages/dev/core/src/Physics/index.ts | 1 - packages/dev/core/src/Physics/v1/index.ts | 1 + .../src/Physics/{ => v1}/physicsHelper.ts | 34 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) rename packages/dev/core/src/Physics/{ => v1}/physicsHelper.ts (94%) diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index dba83948b75..0b264edb370 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -2,5 +2,4 @@ export * from "./v1/index"; export * from "./v2/index"; //export * from "./physicsEngineComponent"; -export * from "./physicsHelper"; diff --git a/packages/dev/core/src/Physics/v1/index.ts b/packages/dev/core/src/Physics/v1/index.ts index 58ab15ee0b5..83d32005b86 100644 --- a/packages/dev/core/src/Physics/v1/index.ts +++ b/packages/dev/core/src/Physics/v1/index.ts @@ -5,3 +5,4 @@ export * from "./physicsEngineComponentV1"; export * from "./physicsImpostor"; export * from "./physicsJoint"; export * from "./Plugins/index"; +export * from "./physicsHelper"; \ No newline at end of file diff --git a/packages/dev/core/src/Physics/physicsHelper.ts b/packages/dev/core/src/Physics/v1/physicsHelper.ts similarity index 94% rename from packages/dev/core/src/Physics/physicsHelper.ts rename to packages/dev/core/src/Physics/v1/physicsHelper.ts index 93f8b252f9f..06634c36597 100644 --- a/packages/dev/core/src/Physics/physicsHelper.ts +++ b/packages/dev/core/src/Physics/v1/physicsHelper.ts @@ -1,14 +1,14 @@ -import type { Nullable } from "../types"; -import { Logger } from "../Misc/logger"; -import { Vector3 } from "../Maths/math.vector"; -import type { AbstractMesh } from "../Meshes/abstractMesh"; -import type { Mesh } from "../Meshes/mesh"; -import { CreateSphere } from "../Meshes/Builders/sphereBuilder"; -import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder"; -import { Ray } from "../Culling/ray"; -import type { Scene } from "../scene"; -import type { IPhysicsEngine } from "./IPhysicsEngine"; -import type { PhysicsEngine } from "./physicsEngine"; +import type { Nullable } from "../../types"; +import { Logger } from "../../Misc/logger"; +import { Vector3 } from "../../Maths/math.vector"; +import type { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { Mesh } from "../../Meshes/mesh"; +import { CreateSphere } from "../../Meshes/Builders/sphereBuilder"; +import { CreateCylinder } from "../../Meshes/Builders/cylinderBuilder"; +import { Ray } from "../../Culling/ray"; +import type { Scene } from "../../scene"; +//import type { IPhysicsEngine } from "../IPhysicsEngine"; +import type { PhysicsEngineV1 } from "./physicsEngineV1"; import type { PhysicsImpostor } from "./physicsImpostor"; /** @@ -17,7 +17,7 @@ import type { PhysicsImpostor } from "./physicsImpostor"; */ export class PhysicsHelper { private _scene: Scene; - private _physicsEngine: Nullable; + private _physicsEngine: Nullable; /** * Initializes the Physics helper @@ -25,7 +25,7 @@ export class PhysicsHelper { */ constructor(scene: Scene) { this._scene = scene; - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine() as any; if (!this._physicsEngine) { Logger.Warn("Physics engine not enabled. Please enable the physics before you can use the methods."); @@ -459,7 +459,7 @@ class PhysicsGravitationalFieldEvent { * Represents a physics updraft event */ class PhysicsUpdraftEvent { - private _physicsEngine: PhysicsEngine; + private _physicsEngine: PhysicsEngineV1; private _originTop: Vector3 = Vector3.Zero(); // the most upper part of the cylinder private _originDirection: Vector3 = Vector3.Zero(); // used if the updraftMode is perpendicular private _tickCallback: any; @@ -474,7 +474,7 @@ class PhysicsUpdraftEvent { * @param _options The options for the updraft event */ constructor(private _scene: Scene, private _origin: Vector3, private _options: PhysicsUpdraftEventOptions) { - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine(); this._options = { ...new PhysicsUpdraftEventOptions(), ...this._options }; this._origin.addToRef(new Vector3(0, this._options.height / 2, 0), this._cylinderPosition); @@ -602,7 +602,7 @@ class PhysicsUpdraftEvent { * Represents a physics vortex event */ class PhysicsVortexEvent { - private _physicsEngine: PhysicsEngine; + private _physicsEngine: PhysicsEngineV1; private _originTop: Vector3 = Vector3.Zero(); // the most upper part of the cylinder private _tickCallback: any; private _cylinder: Mesh; @@ -616,7 +616,7 @@ class PhysicsVortexEvent { * @param _options The options for the vortex event */ constructor(private _scene: Scene, private _origin: Vector3, private _options: PhysicsVortexEventOptions) { - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine(); this._options = { ...new PhysicsVortexEventOptions(), ...this._options }; this._origin.addToRef(new Vector3(0, this._options.height / 2, 0), this._cylinderPosition); From 1c66795339a4a4e7cb2b3503f81cec0ca12f86db Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 15:45:20 +0100 Subject: [PATCH 12/28] dependencies --- packages/dev/core/src/Debug/physicsViewer.ts | 6 +-- .../src/Loading/Plugins/babylonFileLoader.ts | 6 +-- packages/dev/core/src/Meshes/mesh.ts | 7 ++-- .../src/Physics/v1/IPhysicsEnginePluginV1.ts | 2 +- .../src/Physics/v1/Plugins/ammoJSPlugin.ts | 38 +++++++++---------- .../src/Physics/v1/Plugins/cannonJSPlugin.ts | 33 ++++++++-------- .../src/Physics/v1/Plugins/oimoJSPlugin.ts | 26 ++++++------- .../src/XR/features/WebXRControllerPhysics.ts | 2 +- .../core/src/XR/features/WebXRHandTracking.ts | 2 +- 9 files changed, 62 insertions(+), 60 deletions(-) diff --git a/packages/dev/core/src/Debug/physicsViewer.ts b/packages/dev/core/src/Debug/physicsViewer.ts index 8dc2e35bc1f..5fbc4de4b7b 100644 --- a/packages/dev/core/src/Debug/physicsViewer.ts +++ b/packages/dev/core/src/Debug/physicsViewer.ts @@ -9,8 +9,8 @@ import { Color3 } from "../Maths/math.color"; import type { Material } from "../Materials/material"; import { EngineStore } from "../Engines/engineStore"; import { StandardMaterial } from "../Materials/standardMaterial"; -import type { IPhysicsEnginePlugin } from "../Physics/IPhysicsEngine"; -import { PhysicsImpostor } from "../Physics/physicsImpostor"; +import type { IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePluginV1"; +import { PhysicsImpostor } from "../Physics/v1/physicsImpostor"; import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer"; import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder"; import type { ICreateCapsuleOptions } from "../Meshes/Builders/capsuleBuilder"; @@ -30,7 +30,7 @@ export class PhysicsViewer { /** @internal */ protected _numMeshes = 0; /** @internal */ - protected _physicsEnginePlugin: Nullable; + protected _physicsEnginePlugin: Nullable; private _renderFunction: () => void; private _utilityLayer: Nullable; diff --git a/packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts b/packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts index afb12317083..5ceda385cf1 100644 --- a/packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts +++ b/packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts @@ -23,9 +23,9 @@ import { ActionManager } from "../../Actions/actionManager"; import type { IParticleSystem } from "../../Particles/IParticleSystem"; import { Skeleton } from "../../Bones/skeleton"; import { MorphTargetManager } from "../../Morph/morphTargetManager"; -import { CannonJSPlugin } from "../../Physics/Plugins/cannonJSPlugin"; -import { OimoJSPlugin } from "../../Physics/Plugins/oimoJSPlugin"; -import { AmmoJSPlugin } from "../../Physics/Plugins/ammoJSPlugin"; +import { CannonJSPlugin } from "../../Physics/v1/Plugins/cannonJSPlugin"; +import { OimoJSPlugin } from "../../Physics/v1/Plugins/oimoJSPlugin"; +import { AmmoJSPlugin } from "../../Physics/v1/Plugins/ammoJSPlugin"; import { ReflectionProbe } from "../../Probes/reflectionProbe"; import { GetClass } from "../../Misc/typeStore"; import { Tools } from "../../Misc/tools"; diff --git a/packages/dev/core/src/Meshes/mesh.ts b/packages/dev/core/src/Meshes/mesh.ts index d65e40d6363..70d5830bd6e 100644 --- a/packages/dev/core/src/Meshes/mesh.ts +++ b/packages/dev/core/src/Meshes/mesh.ts @@ -38,11 +38,12 @@ import type { Path3D } from "../Maths/math.path"; import type { Plane } from "../Maths/math.plane"; import type { TransformNode } from "./transformNode"; import type { DrawWrapper } from "../Materials/drawWrapper"; +import type { PhysicsEngineV1 } from "../Physics/v1/physicsEngineV1"; declare type GoldbergMesh = import("./goldbergMesh").GoldbergMesh; declare type InstancedMesh = import("./instancedMesh").InstancedMesh; -declare type IPhysicsEnabledObject = import("../Physics/physicsImpostor").IPhysicsEnabledObject; -declare type PhysicsImpostor = import("../Physics/physicsImpostor").PhysicsImpostor; +declare type IPhysicsEnabledObject = import("../Physics/v1/physicsImpostor").IPhysicsEnabledObject; +declare type PhysicsImpostor = import("../Physics/v1/physicsImpostor").PhysicsImpostor; /** * @internal @@ -661,7 +662,7 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData { // Physics clone if (scene.getPhysicsEngine) { - const physicsEngine = scene.getPhysicsEngine(); + const physicsEngine = scene.getPhysicsEngine() as PhysicsEngineV1; if (clonePhysicsImpostor && physicsEngine) { const impostor = physicsEngine.getImpostorForPhysicsObject(source); if (impostor) { diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts index 5b4aa65c059..538b224307f 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -1,7 +1,7 @@ import type { Nullable } from "../../types"; import type { Vector3, Quaternion } from "../../Maths/math.vector"; import type { AbstractMesh } from "../../Meshes/abstractMesh"; -import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; +import type { PhysicsImpostor } from "./physicsImpostor"; import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "../physicsRaycastResult"; diff --git a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts index 0b29bee76ec..07352adb633 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts @@ -1,21 +1,21 @@ -import { Quaternion, Vector3, Matrix } from "../../Maths/math.vector"; -import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine"; -import { Logger } from "../../Misc/logger"; -import type { IPhysicsEnabledObject } from "../../Physics/physicsImpostor"; -import { PhysicsImpostor } from "../../Physics/physicsImpostor"; -import type { IMotorEnabledJoint, DistanceJointData } from "../../Physics/physicsJoint"; -import { PhysicsJoint } from "../../Physics/physicsJoint"; -import { VertexBuffer } from "../../Buffers/buffer"; -import { VertexData } from "../../Meshes/mesh.vertexData"; -import type { Nullable } from "../../types"; -import type { AbstractMesh } from "../../Meshes/abstractMesh"; -import type { Mesh } from "../../Meshes/mesh"; -import { ExtrudeShape } from "../../Meshes/Builders/shapeBuilder"; -import { CreateLines } from "../../Meshes/Builders/linesBuilder"; -import type { LinesMesh } from "../../Meshes/linesMesh"; -import { PhysicsRaycastResult } from "../physicsRaycastResult"; -import { Scalar } from "../../Maths/math.scalar"; -import { Epsilon } from "../../Maths/math.constants"; +import { Quaternion, Vector3, Matrix } from "../../../Maths/math.vector"; +import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import { Logger } from "../../../Misc/logger"; +import type { IPhysicsEnabledObject } from "../physicsImpostor"; +import { PhysicsImpostor } from "../physicsImpostor"; +import type { IMotorEnabledJoint, DistanceJointData } from "..//physicsJoint"; +import { PhysicsJoint } from "../physicsJoint"; +import { VertexBuffer } from "../../../Buffers/buffer"; +import { VertexData } from "../../../Meshes/mesh.vertexData"; +import type { Nullable } from "../../../types"; +import type { AbstractMesh } from "../../../Meshes/abstractMesh"; +import type { Mesh } from "../../../Meshes/mesh"; +import { ExtrudeShape } from "../../../Meshes/Builders/shapeBuilder"; +import { CreateLines } from "../../../Meshes/Builders/linesBuilder"; +import type { LinesMesh } from "../../../Meshes/linesMesh"; +import { PhysicsRaycastResult } from "../../physicsRaycastResult"; +import { Scalar } from "../../../Maths/math.scalar"; +import { Epsilon } from "../../../Maths/math.constants"; // eslint-disable-next-line @typescript-eslint/naming-convention declare let Ammo: any; @@ -25,7 +25,7 @@ declare let Ammo: any; * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine * @see https://github.com/kripken/ammo.js/ */ -export class AmmoJSPlugin implements IPhysicsEnginePlugin { +export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { /** * Reference to the Ammo library */ diff --git a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts index 46a0f6186e9..f891489c3a7 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts @@ -1,22 +1,23 @@ -import type { Nullable, FloatArray } from "../../types"; -import { Logger } from "../../Misc/logger"; -import { Vector3, Matrix, Quaternion } from "../../Maths/math.vector"; -import { VertexBuffer } from "../../Buffers/buffer"; -import type { AbstractMesh } from "../../Meshes/abstractMesh"; -import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine"; -import type { IPhysicsEnabledObject } from "../../Physics/physicsImpostor"; -import { PhysicsImpostor } from "../../Physics/physicsImpostor"; -import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../../Physics/physicsJoint"; -import { PhysicsJoint } from "../../Physics/physicsJoint"; -import { PhysicsEngine } from "../../Physics/physicsEngine"; -import { PhysicsRaycastResult } from "../physicsRaycastResult"; -import type { TransformNode } from "../../Meshes/transformNode"; +import type { Nullable, FloatArray } from "../../../types"; +import { Logger } from "../../../Misc/logger"; +import { Vector3, Matrix, Quaternion } from "../../../Maths/math.vector"; +import { VertexBuffer } from "../../../Buffers/buffer"; +import type { AbstractMesh } from "../../../Meshes/abstractMesh"; +import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import type { IPhysicsEnabledObject } from "..//physicsImpostor"; +import { PhysicsImpostor } from "..//physicsImpostor"; +import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../physicsJoint"; +import { PhysicsJoint } from "../physicsJoint"; +//import { PhysicsEngineV1 } from "./physicsEngineV1"; +import { PhysicsRaycastResult } from "../../physicsRaycastResult"; +import type { TransformNode } from "../../../Meshes/transformNode"; +import { PhysicsEngineV1 } from "../physicsEngineV1"; //declare var require: any; declare let CANNON: any; /** @internal */ -export class CannonJSPlugin implements IPhysicsEnginePlugin { +export class CannonJSPlugin implements IPhysicsEnginePluginV1 { public world: any; public name: string = "CannonJSPlugin"; private _physicsMaterials = new Array(); @@ -313,7 +314,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin { } private _checkWithEpsilon(value: number): number { - return value < PhysicsEngine.Epsilon ? PhysicsEngine.Epsilon : value; + return value < PhysicsEngineV1.Epsilon ? PhysicsEngineV1.Epsilon : value; } private _createShape(impostor: PhysicsImpostor) { @@ -763,6 +764,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin { } } -PhysicsEngine.DefaultPluginFactory = () => { +PhysicsEngineV1.DefaultPluginFactory = () => { return new CannonJSPlugin(); }; diff --git a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts index 24c9f697419..d663a08eebf 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts @@ -1,19 +1,19 @@ -import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine"; -import type { IPhysicsEnabledObject } from "../../Physics/physicsImpostor"; -import { PhysicsImpostor } from "../../Physics/physicsImpostor"; -import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../../Physics/physicsJoint"; -import { PhysicsJoint } from "../../Physics/physicsJoint"; -import { PhysicsEngine } from "../../Physics/physicsEngine"; -import type { AbstractMesh } from "../../Meshes/abstractMesh"; -import { Vector3, Quaternion } from "../../Maths/math.vector"; -import type { Nullable } from "../../types"; -import { Logger } from "../../Misc/logger"; -import { PhysicsRaycastResult } from "../physicsRaycastResult"; +import type { IPhysicsEnabledObject } from "..//physicsImpostor"; +import { PhysicsImpostor } from "../physicsImpostor"; +import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../physicsJoint"; +import { PhysicsJoint } from "../physicsJoint"; +import { PhysicsEngineV1 } from "../physicsEngineV1"; +import type { AbstractMesh } from "../../../Meshes/abstractMesh"; +import { Vector3, Quaternion } from "../../../Maths/math.vector"; +import type { Nullable } from "../../../types"; +import { Logger } from "../../../Misc/logger"; +import { PhysicsRaycastResult } from "../../physicsRaycastResult"; +import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; declare let OIMO: any; /** @internal */ -export class OimoJSPlugin implements IPhysicsEnginePlugin { +export class OimoJSPlugin implements IPhysicsEnginePluginV1 { public world: any; public name: string = "OimoJSPlugin"; // eslint-disable-next-line @typescript-eslint/naming-convention @@ -134,7 +134,7 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin { addToArray(impostor.object); const checkWithEpsilon = (value: number): number => { - return Math.max(value, PhysicsEngine.Epsilon); + return Math.max(value, PhysicsEngineV1.Epsilon); }; const globalQuaternion: Quaternion = new Quaternion(); diff --git a/packages/dev/core/src/XR/features/WebXRControllerPhysics.ts b/packages/dev/core/src/XR/features/WebXRControllerPhysics.ts index ca425c01915..b553e94bc58 100644 --- a/packages/dev/core/src/XR/features/WebXRControllerPhysics.ts +++ b/packages/dev/core/src/XR/features/WebXRControllerPhysics.ts @@ -1,7 +1,7 @@ import { WebXRAbstractFeature } from "./WebXRAbstractFeature"; import { Vector3, Quaternion } from "../../Maths/math.vector"; import type { WebXRInputSource } from "../webXRInputSource"; -import { PhysicsImpostor } from "../../Physics/physicsImpostor"; +import { PhysicsImpostor } from "../../Physics/v1/physicsImpostor"; import type { WebXRInput } from "../webXRInput"; import type { WebXRSessionManager } from "../webXRSessionManager"; import type { AbstractMesh } from "../../Meshes/abstractMesh"; diff --git a/packages/dev/core/src/XR/features/WebXRHandTracking.ts b/packages/dev/core/src/XR/features/WebXRHandTracking.ts index 59d194fef12..522463d31e4 100644 --- a/packages/dev/core/src/XR/features/WebXRHandTracking.ts +++ b/packages/dev/core/src/XR/features/WebXRHandTracking.ts @@ -7,7 +7,7 @@ import type { WebXRInput } from "../webXRInput"; import type { WebXRInputSource } from "../webXRInputSource"; import { Matrix, Quaternion } from "../../Maths/math.vector"; import type { Nullable } from "../../types"; -import { PhysicsImpostor } from "../../Physics/physicsImpostor"; +import { PhysicsImpostor } from "../../Physics/v1/physicsImpostor"; import type { IDisposable, Scene } from "../../scene"; import { Observable } from "../../Misc/observable"; From b15e75270ed756f201cf105c35a1d9f9e0119cbe Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 16:25:01 +0100 Subject: [PATCH 13/28] linting --- packages/dev/core/src/Physics/index.ts | 1 - packages/dev/core/src/Physics/v1/index.ts | 2 +- packages/dev/core/src/Physics/v2/Plugins/index.ts | 1 + packages/dev/core/src/Physics/v2/index.ts | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index 0b264edb370..3923cad4220 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -2,4 +2,3 @@ export * from "./v1/index"; export * from "./v2/index"; //export * from "./physicsEngineComponent"; - diff --git a/packages/dev/core/src/Physics/v1/index.ts b/packages/dev/core/src/Physics/v1/index.ts index 83d32005b86..f0253c685e6 100644 --- a/packages/dev/core/src/Physics/v1/index.ts +++ b/packages/dev/core/src/Physics/v1/index.ts @@ -5,4 +5,4 @@ export * from "./physicsEngineComponentV1"; export * from "./physicsImpostor"; export * from "./physicsJoint"; export * from "./Plugins/index"; -export * from "./physicsHelper"; \ No newline at end of file +export * from "./physicsHelper"; diff --git a/packages/dev/core/src/Physics/v2/Plugins/index.ts b/packages/dev/core/src/Physics/v2/Plugins/index.ts index e69de29bb2d..ab0c01417a7 100644 --- a/packages/dev/core/src/Physics/v2/Plugins/index.ts +++ b/packages/dev/core/src/Physics/v2/Plugins/index.ts @@ -0,0 +1 @@ +// \ No newline at end of file diff --git a/packages/dev/core/src/Physics/v2/index.ts b/packages/dev/core/src/Physics/v2/index.ts index 66290024bf9..ef91bd4ff2b 100644 --- a/packages/dev/core/src/Physics/v2/index.ts +++ b/packages/dev/core/src/Physics/v2/index.ts @@ -5,4 +5,4 @@ export * from "./physicsShape"; export * from "./physicsConstraint"; export * from "./physicsMaterial"; export * from "./physicsAggregate"; -//export * from "./Plugins/index"; \ No newline at end of file +//export * from "./Plugins/index"; From ef125c0681678c9aa65f4c221a282d6078e8b23d Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 16:45:03 +0100 Subject: [PATCH 14/28] linting again --- packages/dev/core/src/Physics/v2/Plugins/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Physics/v2/Plugins/index.ts b/packages/dev/core/src/Physics/v2/Plugins/index.ts index ab0c01417a7..5f7c77da42a 100644 --- a/packages/dev/core/src/Physics/v2/Plugins/index.ts +++ b/packages/dev/core/src/Physics/v2/Plugins/index.ts @@ -1 +1 @@ -// \ No newline at end of file +// From 702cd968ccc5792cc7c0582dcc0ee9766f892739 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Nov 2022 17:48:03 +0100 Subject: [PATCH 15/28] import fixes --- packages/dev/core/src/Physics/index.ts | 2 +- .../Physics/v1/physicsEngineComponentV1.ts | 67 ++----------------- .../meshes/meshPropertyGridComponent.tsx | 2 +- 3 files changed, 6 insertions(+), 65 deletions(-) diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index 3923cad4220..212194e6b7f 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -1,4 +1,4 @@ /* eslint-disable import/no-internal-modules */ export * from "./v1/index"; export * from "./v2/index"; -//export * from "./physicsEngineComponent"; +export * from "./physicsEngineComponent"; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts index 6d26fe71585..b8e104dcd15 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts @@ -1,13 +1,13 @@ import type { Nullable } from "../../types"; //import { Logger } from "../../Misc/logger"; import type { Observer } from "../../Misc/observable"; -import { Observable } from "../../Misc/observable"; +//import { Observable } from "../../Misc/observable"; import type { Vector3 } from "../../Maths/math.vector"; import type { Mesh } from "../../Meshes/mesh"; import { AbstractMesh } from "../../Meshes/abstractMesh"; -import type { ISceneComponent } from "../../sceneComponent"; -import { SceneComponentConstants } from "../../sceneComponent"; -import type { Scene } from "../../scene"; +//import type { ISceneComponent } from "../../sceneComponent"; +//import { SceneComponentConstants } from "../../sceneComponent"; +//import type { Scene } from "../../scene"; import type { Node } from "../../node"; //import type { IPhysicsEngine, IPhysicsEnginePlugin } from "../IPhysicsEngine"; @@ -132,62 +132,3 @@ AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh: Mesh, pivot1: V }); return this; }; - -/** - * Defines the physics engine scene component responsible to manage a physics engine - */ -export class PhysicsEngineSceneComponent implements ISceneComponent { - /** - * The component name helpful to identify the component in the list of scene components. - */ - public readonly name = SceneComponentConstants.NAME_PHYSICSENGINE; - - /** - * The scene the component belongs to. - */ - public scene: Scene; - - /** - * Creates a new instance of the component for the given scene - * @param scene Defines the scene to register the component in - */ - constructor(scene: Scene) { - this.scene = scene; - this.scene.onBeforePhysicsObservable = new Observable(); - this.scene.onAfterPhysicsObservable = new Observable(); - - // Replace the function used to get the deterministic frame time - this.scene.getDeterministicFrameTime = () => { - if (this.scene._physicsEngine) { - return this.scene._physicsEngine.getTimeStep() * 1000; - } - - return 1000.0 / 60.0; - }; - } - - /** - * Registers the component in a given scene - */ - public register(): void {} - - /** - * Rebuilds the elements related to this component in case of - * context lost for instance. - */ - public rebuild(): void { - // Nothing to do for this component - } - - /** - * Disposes the component and the associated resources - */ - public dispose(): void { - this.scene.onBeforePhysicsObservable.clear(); - this.scene.onAfterPhysicsObservable.clear(); - - if (this.scene._physicsEngine) { - this.scene.disablePhysicsEngine(); - } - } -} diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx index 0222b8b38c0..104ec4d24bc 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx @@ -6,7 +6,7 @@ import { Color3 } from "core/Maths/math.color"; import type { Mesh } from "core/Meshes/mesh"; import { VertexBuffer } from "core/Buffers/buffer"; import { CreateLineSystem } from "core/Meshes/Builders/linesBuilder"; -import { PhysicsImpostor } from "core/Physics/physicsImpostor"; +import { PhysicsImpostor } from "core/Physics/v1/physicsImpostor"; import { Scene } from "core/scene"; import type { PropertyChangedEvent } from "../../../../propertyChangedEvent"; From 0ea0734393f0b61e15f3c84864bb3399daf43805 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Mon, 28 Nov 2022 15:36:50 +0100 Subject: [PATCH 16/28] imports --- packages/dev/core/src/Physics/index.ts | 1 + .../src/components/actionTabs/tabs/debugTabComponent.tsx | 2 ++ .../tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx | 3 +++ .../tabs/propertyGrids/scenePropertyGridComponent.tsx | 2 ++ 4 files changed, 8 insertions(+) diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index 212194e6b7f..5a53e2d707e 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -2,3 +2,4 @@ export * from "./v1/index"; export * from "./v2/index"; export * from "./physicsEngineComponent"; +export * from "./v1/physicsEngineComponentV1"; diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx index 364bd9aa6eb..9958b68dc9a 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx @@ -9,6 +9,8 @@ import { StandardMaterial } from "core/Materials/standardMaterial"; import type { Mesh } from "core/Meshes/mesh"; import "core/Physics/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponentV1"; +import "core/Physics/v1/physicsEngineComponentV2"; export class DebugTabComponent extends PaneComponent { private _physicsViewersEnabled = false; diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx index 104ec4d24bc..549b0280368 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx @@ -38,6 +38,9 @@ import type { IInspectableOptions } from "core/Misc/iInspectable"; import { NormalMaterial } from "materials/normal/normalMaterial"; import "core/Physics/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponentV1"; +import "core/Physics/v1/physicsEngineComponentV2"; + import { ParentPropertyGridComponent } from "../parentPropertyGridComponent"; import { Tools } from "core/Misc/tools"; diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx index 3ae5f8d47ff..dfd844a0023 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx @@ -27,6 +27,8 @@ import { ButtonLineComponent } from "shared-ui-components/lines/buttonLineCompon import { AnimationGridComponent } from "./animations/animationPropertyGridComponent"; import "core/Physics/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponentV1"; +import "core/Physics/v1/physicsEngineComponentV2"; interface IScenePropertyGridComponentProps { globalState: GlobalState; From 5c04ce7862a1ceca6bff20f24d96c0321dff2087 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Mon, 28 Nov 2022 16:20:46 +0100 Subject: [PATCH 17/28] es6 import --- packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts index f891489c3a7..03708af99c9 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts @@ -8,7 +8,6 @@ import type { IPhysicsEnabledObject } from "..//physicsImpostor"; import { PhysicsImpostor } from "..//physicsImpostor"; import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../physicsJoint"; import { PhysicsJoint } from "../physicsJoint"; -//import { PhysicsEngineV1 } from "./physicsEngineV1"; import { PhysicsRaycastResult } from "../../physicsRaycastResult"; import type { TransformNode } from "../../../Meshes/transformNode"; import { PhysicsEngineV1 } from "../physicsEngineV1"; From b59fab0bc7eb5b0f4649f2bbc7daf4a71fedab9a Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Mon, 28 Nov 2022 17:17:38 +0100 Subject: [PATCH 18/28] engine components --- .../src/Physics/physicsEngineComponent.ts | 21 ++-- .../Physics/v2/physicsEngineComponentV2.ts | 106 ++++++++++++++---- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index 95e903761b3..375356e65e1 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -1,16 +1,13 @@ import type { Nullable } from "../types"; import { Logger } from "../Misc/logger"; -//import type { Observer } from "../Misc/observable"; import { Observable } from "../Misc/observable"; import type { Vector3 } from "../Maths/math.vector"; -//import type { Mesh } from "../Meshes/mesh"; -//import { AbstractMesh } from "../Meshes/abstractMesh"; +import type { AbstractMesh } from "../Meshes/abstractMesh"; import type { ISceneComponent } from "../sceneComponent"; import { SceneComponentConstants } from "../sceneComponent"; import { Scene } from "../scene"; -//import type { Node } from "../node"; - import type { IPhysicsEngine } from "./IPhysicsEngine"; +import { PhysicsEngineV1 } from "./v1"; declare module "../scene" { /** @@ -92,7 +89,7 @@ Scene.prototype.enablePhysics = function (gravity: Nullable = null, plu } try { - this._physicsEngine = null; // new PhysicsEngine(gravity, plugin); + this._physicsEngine = new PhysicsEngineV1(gravity, plugin); this._physicsTimeAccumulator = 0; return true; } catch (e) { @@ -126,12 +123,12 @@ Scene.prototype.isPhysicsEnabled = function (): boolean { * @param compound defines the compound to delete */ Scene.prototype.deleteCompoundImpostor = function (compound: any): void { - //const mesh: AbstractMesh = compound.parts[0].mesh; - // TODO - //if (mesh.physicsImpostor) { - // mesh.physicsImpostor.dispose(/*true*/); - // mesh.physicsImpostor = null; - //} + const mesh: AbstractMesh = compound.parts[0].mesh; + + if (mesh.physicsImpostor) { + mesh.physicsImpostor.dispose(/*true*/); + mesh.physicsImpostor = null; + } }; /** diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts index 20e29270ae8..f03660ab449 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts @@ -1,19 +1,87 @@ -/* -import type { Nullable } from "../types"; -import { Logger } from "../Misc/logger"; -import type { Observer } from "../Misc/observable"; -import { Observable } from "../Misc/observable"; -import type { Vector3 } from "../Maths/math.vector"; -import type { Mesh } from "../Meshes/mesh"; -import { AbstractMesh } from "../Meshes/abstractMesh"; -import type { ISceneComponent } from "../sceneComponent"; -import { SceneComponentConstants } from "../sceneComponent"; -import { Scene } from "../scene"; -import type { Node } from "../node"; - -import type { IPhysicsEngine, IPhysicsEnginePlugin } from "./IPhysicsEngine"; -import { PhysicsEngine } from "./physicsEngine"; -import type { PhysicsImpostor } from "./physicsImpostor"; -import { PhysicsJoint } from "./physicsJoint"; -*/ -// TODO +import type { Nullable } from "../../types"; +import type { Observer } from "../../Misc/observable"; +import type { Vector3 } from "../../Maths/math.vector"; +import { AbstractMesh } from "../../Meshes/abstractMesh"; +import type { Node } from "../../node"; +import type { PhysicsBody } from "./physicsBody"; + +declare module "../../Meshes/abstractMesh" { + /** + * + */ + export interface AbstractMesh { + /** @internal */ + _physicsBody: Nullable; + + /** + * @see + */ + physicsBody: Nullable; + + /** + * + */ + getPhysicsBody(): Nullable; + + /** Apply a physic impulse to the mesh + * @param force defines the force to apply + * @param contactPoint defines where to apply the force + * @returns the current mesh + */ + applyImpulse(force: Vector3, contactPoint: Vector3): AbstractMesh; + + /** @internal */ + _disposePhysicsObserver: Nullable>; + } +} + +Object.defineProperty(AbstractMesh.prototype, "physicsBody", { + get: function (this: AbstractMesh) { + return this._physicsBody; + }, + set: function (this: AbstractMesh, value: Nullable) { + if (this._physicsBody === value) { + return; + } + if (this._disposePhysicsObserver) { + this.onDisposeObservable.remove(this._disposePhysicsObserver); + } + + this._physicsBody = value; + + if (value) { + this._disposePhysicsObserver = this.onDisposeObservable.add(() => { + // Physics + if (this.physicsBody) { + this.physicsBody.dispose(/*!doNotRecurse*/); + this.physicsBody = null; + } + }); + } + }, + enumerable: true, + configurable: true, +}); + +/** + * Gets the current physics body + * @returns a physics body or null + */ +AbstractMesh.prototype.getPhysicsBody = function (): Nullable { + return this.physicsBody; +}; + +/** + * Apply a physic impulse to the mesh + * @param force defines the force to apply + * @param contactPoint defines where to apply the force + * @returns the current mesh + * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine + */ +AbstractMesh.prototype.applyImpulse = function (force: Vector3, contactPoint: Vector3): AbstractMesh { + if (!this.physicsBody) { + return this; + } + this.physicsBody.applyImpulse(force, contactPoint); + return this; +}; From adadc2978df58e83335fa8d93c3858d9779adf78 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Mon, 28 Nov 2022 18:01:03 +0100 Subject: [PATCH 19/28] cleanup --- packages/dev/core/src/Physics/physicsEngineComponent.ts | 4 ++-- .../dev/core/src/Physics/v1/physicsEngineComponentV1.ts | 8 -------- .../dev/core/src/Physics/v2/physicsEngineComponentV2.ts | 4 ++-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index 375356e65e1..a5f52c4f6f6 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -7,7 +7,7 @@ import type { ISceneComponent } from "../sceneComponent"; import { SceneComponentConstants } from "../sceneComponent"; import { Scene } from "../scene"; import type { IPhysicsEngine } from "./IPhysicsEngine"; -import { PhysicsEngineV1 } from "./v1"; +import { PhysicsEngineV1 } from "./v1/physicsEngineV1"; declare module "../scene" { /** @@ -124,7 +124,7 @@ Scene.prototype.isPhysicsEnabled = function (): boolean { */ Scene.prototype.deleteCompoundImpostor = function (compound: any): void { const mesh: AbstractMesh = compound.parts[0].mesh; - + if (mesh.physicsImpostor) { mesh.physicsImpostor.dispose(/*true*/); mesh.physicsImpostor = null; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts index b8e104dcd15..878990c3219 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts @@ -1,17 +1,9 @@ import type { Nullable } from "../../types"; -//import { Logger } from "../../Misc/logger"; import type { Observer } from "../../Misc/observable"; -//import { Observable } from "../../Misc/observable"; import type { Vector3 } from "../../Maths/math.vector"; import type { Mesh } from "../../Meshes/mesh"; import { AbstractMesh } from "../../Meshes/abstractMesh"; -//import type { ISceneComponent } from "../../sceneComponent"; -//import { SceneComponentConstants } from "../../sceneComponent"; -//import type { Scene } from "../../scene"; import type { Node } from "../../node"; - -//import type { IPhysicsEngine, IPhysicsEnginePlugin } from "../IPhysicsEngine"; -//import { PhysicsEngine } from "../physicsEngine"; import type { PhysicsImpostor } from "./physicsImpostor"; import { PhysicsJoint } from "./physicsJoint"; diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts index f03660ab449..66d09d4b514 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts @@ -14,12 +14,12 @@ declare module "../../Meshes/abstractMesh" { _physicsBody: Nullable; /** - * @see + * @see */ physicsBody: Nullable; /** - * + * */ getPhysicsBody(): Nullable; From 1317b20aba746a21c01e5634dc327af6d8c0e7d7 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 29 Nov 2022 10:31:13 +0100 Subject: [PATCH 20/28] removed commented code --- .../dev/core/src/Physics/IPhysicsEngine.ts | 54 ------------------- .../dev/core/src/Physics/v1/physicsHelper.ts | 1 - .../core/src/Physics/v1/physicsImpostor.ts | 1 - .../src/Physics/v2/IPhysicsEnginePluginV2.ts | 1 - .../dev/core/src/Physics/v2/Plugins/index.ts | 2 +- packages/dev/core/src/Physics/v2/index.ts | 1 - .../core/src/Physics/v2/physicsConstraint.ts | 1 - .../core/src/Physics/v2/physicsEngineV2.ts | 2 - 8 files changed, 1 insertion(+), 62 deletions(-) diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index 2255e99d24f..b3b8f4176c5 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -1,8 +1,4 @@ -//import type { Nullable } from "../types"; import type { Vector3 } from "../Maths/math.vector"; -//import type { AbstractMesh } from "../Meshes/abstractMesh"; -//import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; -//import type { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; /** @@ -61,62 +57,12 @@ export interface IPhysicsEngine { */ getPhysicsPluginName(): string; - /** - * Adding a new impostor for the impostor tracking. - * This will be done by the impostor itself. - * @param impostor the impostor to add - */ - //addImpostor(impostor: PhysicsImpostor): void; - - /** - * Remove an impostor from the engine. - * This impostor and its mesh will not longer be updated by the physics engine. - * @param impostor the impostor to remove - */ - //removeImpostor(impostor: PhysicsImpostor): void; - - /** - * Add a joint to the physics engine - * @param mainImpostor defines the main impostor to which the joint is added. - * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint - * @param joint defines the joint that will connect both impostors. - */ - //addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; - - /** - * Removes a joint from the simulation - * @param mainImpostor defines the impostor used with the joint - * @param connectedImpostor defines the other impostor connected to the main one by the joint - * @param joint defines the joint to remove - */ - //removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void; - /** * Gets the current plugin used to run the simulation * @returns current plugin */ getPhysicsPlugin(): any; - /** - * Gets the list of physic impostors - * @returns an array of PhysicsImpostor - */ - //getImpostors(): Array; - - /** - * Gets the impostor for a physics enabled object - * @param object defines the object impersonated by the impostor - * @returns the PhysicsImpostor or null if not found - */ - //getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable; - - /** - * Gets the impostor for a physics body object - * @param body defines physics body used by the impostor - * @returns the PhysicsImpostor or null if not found - */ - //getImpostorWithPhysicsBody(body: any): Nullable; - /** * Does a raycast in the physics world * @param from when should the ray start? diff --git a/packages/dev/core/src/Physics/v1/physicsHelper.ts b/packages/dev/core/src/Physics/v1/physicsHelper.ts index 06634c36597..4ccadfb5e29 100644 --- a/packages/dev/core/src/Physics/v1/physicsHelper.ts +++ b/packages/dev/core/src/Physics/v1/physicsHelper.ts @@ -7,7 +7,6 @@ import { CreateSphere } from "../../Meshes/Builders/sphereBuilder"; import { CreateCylinder } from "../../Meshes/Builders/cylinderBuilder"; import { Ray } from "../../Culling/ray"; import type { Scene } from "../../scene"; -//import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { PhysicsEngineV1 } from "./physicsEngineV1"; import type { PhysicsImpostor } from "./physicsImpostor"; diff --git a/packages/dev/core/src/Physics/v1/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts index c7a00bb9125..787141cd9e6 100644 --- a/packages/dev/core/src/Physics/v1/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/v1/physicsImpostor.ts @@ -9,7 +9,6 @@ import { Mesh } from "../../Meshes/mesh"; import type { Scene } from "../../scene"; import type { Bone } from "../../Bones/bone"; import type { BoundingInfo } from "../../Culling/boundingInfo"; -//import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { PhysicsEngineV1 } from "./physicsEngineV1"; import type { PhysicsJointData } from "./physicsJoint"; diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts index 1ffca48e999..c82f1586ec8 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts @@ -7,7 +7,6 @@ import type { PhysicsConstraint } from "./physicsConstraint"; import type { BoundingBox } from "../../Culling/boundingBox"; import type { TransformNode } from "../../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; -//import type { PhysicsAggregate } from "./physicsAggregate"; export enum ConstraintAxisLimitMode { FREE, diff --git a/packages/dev/core/src/Physics/v2/Plugins/index.ts b/packages/dev/core/src/Physics/v2/Plugins/index.ts index 5f7c77da42a..0ffdd02fcbc 100644 --- a/packages/dev/core/src/Physics/v2/Plugins/index.ts +++ b/packages/dev/core/src/Physics/v2/Plugins/index.ts @@ -1 +1 @@ -// +// TODO \ No newline at end of file diff --git a/packages/dev/core/src/Physics/v2/index.ts b/packages/dev/core/src/Physics/v2/index.ts index ef91bd4ff2b..3240b6eecda 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"; diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index 9be7fb0e6dc..de0d2be363d 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -2,7 +2,6 @@ import type { Scene } from "../../scene"; import type { Vector3 } from "../../Maths/math.vector"; import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters } from "./IPhysicsEnginePluginV2"; import { ConstraintType, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePluginV2"; -//import { ConstraintType } from "./IPhysicsEngine2"; import type { PhysicsBody } from "./physicsBody"; /** diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index 7d619a5f8f3..4346883767a 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -3,8 +3,6 @@ import { Vector3 } from "../../Maths/math.vector"; import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; -//import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; -//import type { PhysicsJoint } from "./physicsJoint"; import type { PhysicsRaycastResult } from "../physicsRaycastResult"; import { _WarnImport } from "../../Misc/devTools"; From f9bd36ce1cf7137f4b085a9001afdf6abcfe08cd Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 29 Nov 2022 14:34:40 +0100 Subject: [PATCH 21/28] linting... --- packages/dev/core/src/Physics/v2/Plugins/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Physics/v2/Plugins/index.ts b/packages/dev/core/src/Physics/v2/Plugins/index.ts index 0ffdd02fcbc..24b8567385e 100644 --- a/packages/dev/core/src/Physics/v2/Plugins/index.ts +++ b/packages/dev/core/src/Physics/v2/Plugins/index.ts @@ -1 +1 @@ -// TODO \ No newline at end of file +// TODO From 4eea1426bcf6e53d2bba9f5e805422bd006404f3 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 30 Nov 2022 19:12:56 +0100 Subject: [PATCH 22/28] PR feedback --- .../dev/core/src/Physics/IPhysicsEngine.ts | 8 +- .../src/Physics/physicsEngineComponent.ts | 4 +- .../src/Physics/v1/IPhysicsEnginePluginV1.ts | 2 + .../src/Physics/v1/Plugins/ammoJSPlugin.ts | 17 ++++- .../src/Physics/v1/Plugins/cannonJSPlugin.ts | 23 ++++-- .../src/Physics/v1/Plugins/oimoJSPlugin.ts | 16 +++- .../core/src/Physics/v1/physicsEngineV1.ts | 19 ++++- .../src/Physics/v2/IPhysicsEnginePluginV2.ts | 6 +- .../dev/core/src/Physics/v2/physicsBody.ts | 60 +++++++++------ .../core/src/Physics/v2/physicsConstraint.ts | 73 +++++++++++-------- .../Physics/v2/physicsEngineComponentV2.ts | 2 +- .../core/src/Physics/v2/physicsEngineV2.ts | 21 +++++- .../core/src/Physics/v2/physicsMaterial.ts | 16 +++- .../dev/core/src/Physics/v2/physicsShape.ts | 43 ++++++----- 14 files changed, 213 insertions(+), 97 deletions(-) diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index b3b8f4176c5..2a645999555 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -1,5 +1,7 @@ import type { Vector3 } from "../Maths/math.vector"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; +import type { IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePluginV2"; /** * Interface used to define a physics engine @@ -11,6 +13,10 @@ export interface IPhysicsEngine { */ gravity: Vector3; + /** + * + */ + getPluginVersion(): number; /** * Sets the gravity vector used by the simulation * @param gravity defines the gravity vector to use @@ -61,7 +67,7 @@ export interface IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - getPhysicsPlugin(): any; + getPhysicsPlugin(): IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null; /** * Does a raycast in the physics world diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index a5f52c4f6f6..1e18fd3fba8 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -7,6 +7,8 @@ import type { ISceneComponent } from "../sceneComponent"; import { SceneComponentConstants } from "../sceneComponent"; import { Scene } from "../scene"; import type { IPhysicsEngine } from "./IPhysicsEngine"; +import type { IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePluginV2"; import { PhysicsEngineV1 } from "./v1/physicsEngineV1"; declare module "../scene" { @@ -31,7 +33,7 @@ declare module "../scene" { * @param plugin defines the physics engine to be used. defaults to CannonJS. * @returns a boolean indicating if the physics engine was initialized */ - enablePhysics(gravity?: Nullable, plugin?: any /*IPhysicsEnginePlugin*/): boolean; + enablePhysics(gravity?: Nullable, plugin?: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2): boolean; /** * Disables and disposes the physics engine associated with the scene diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts index 538b224307f..527f6ded522 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -63,6 +63,8 @@ export interface IPhysicsEnginePluginV1 { sleepBody(impostor: PhysicsImpostor): void; wakeUpBody(impostor: PhysicsImpostor): void; raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; + raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void; + //Joint Update updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number): void; setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number): void; diff --git a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts index 07352adb633..3f7ce33ddf6 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts @@ -1593,13 +1593,23 @@ export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { * @returns PhysicsRaycastResult */ public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { + this.raycastToRef(from, to, this._raycastResult); + return this._raycastResult; + } + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @param result resulting PhysicsRaycastResult + */ + public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void { this._tmpAmmoVectorRCA = new this.bjsAMMO.btVector3(from.x, from.y, from.z); this._tmpAmmoVectorRCB = new this.bjsAMMO.btVector3(to.x, to.y, to.z); const rayCallback = new this.bjsAMMO.ClosestRayResultCallback(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB); this.world.rayTest(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB, rayCallback); - this._raycastResult.reset(from, to); + result.reset(from, to); if (rayCallback.hasHit()) { // TODO: do we want/need the body? If so, set all the data /* @@ -1608,7 +1618,7 @@ export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { ); var body = {}; */ - this._raycastResult.setHitData( + result.setHitData( { x: rayCallback.get_m_hitNormalWorld().x(), y: rayCallback.get_m_hitNormalWorld().y(), @@ -1620,11 +1630,10 @@ export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { z: rayCallback.get_m_hitPointWorld().z(), } ); - this._raycastResult.calculateHitDistance(); + result.calculateHitDistance(); } this.bjsAMMO.destroy(rayCallback); this.bjsAMMO.destroy(this._tmpAmmoVectorRCA); this.bjsAMMO.destroy(this._tmpAmmoVectorRCB); - return this._raycastResult; } } diff --git a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts index 03708af99c9..983a9d3a25f 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts @@ -11,6 +11,7 @@ import { PhysicsJoint } from "../physicsJoint"; import { PhysicsRaycastResult } from "../../physicsRaycastResult"; import type { TransformNode } from "../../../Meshes/transformNode"; import { PhysicsEngineV1 } from "../physicsEngineV1"; +import { Epsilon } from "../../../Maths/math.constants"; //declare var require: any; declare let CANNON: any; @@ -313,7 +314,7 @@ export class CannonJSPlugin implements IPhysicsEnginePluginV1 { } private _checkWithEpsilon(value: number): number { - return value < PhysicsEngineV1.Epsilon ? PhysicsEngineV1.Epsilon : value; + return value < Epsilon ? Epsilon : value; } private _createShape(impostor: PhysicsImpostor) { @@ -738,13 +739,25 @@ export class CannonJSPlugin implements IPhysicsEnginePluginV1 { * @returns PhysicsRaycastResult */ public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { + this._raycastResult.reset(from, to); + this.raycastToRef(from, to, this._raycastResult); + return this._raycastResult; + } + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @param result resulting PhysicsRaycastResult + */ + public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void { this._cannonRaycastResult.reset(); this.world.raycastClosest(from, to, {}, this._cannonRaycastResult); - this._raycastResult.reset(from, to); + result.reset(from, to); if (this._cannonRaycastResult.hasHit) { // TODO: do we also want to get the body it hit? - this._raycastResult.setHitData( + result.setHitData( { x: this._cannonRaycastResult.hitNormalWorld.x, y: this._cannonRaycastResult.hitNormalWorld.y, @@ -756,10 +769,8 @@ export class CannonJSPlugin implements IPhysicsEnginePluginV1 { z: this._cannonRaycastResult.hitPointWorld.z, } ); - this._raycastResult.setHitDistance(this._cannonRaycastResult.distance); + result.setHitDistance(this._cannonRaycastResult.distance); } - - return this._raycastResult; } } diff --git a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts index d663a08eebf..f274946b59d 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts @@ -2,13 +2,13 @@ import type { IPhysicsEnabledObject } from "..//physicsImpostor"; import { PhysicsImpostor } from "../physicsImpostor"; import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../physicsJoint"; import { PhysicsJoint } from "../physicsJoint"; -import { PhysicsEngineV1 } from "../physicsEngineV1"; import type { AbstractMesh } from "../../../Meshes/abstractMesh"; import { Vector3, Quaternion } from "../../../Maths/math.vector"; import type { Nullable } from "../../../types"; import { Logger } from "../../../Misc/logger"; import { PhysicsRaycastResult } from "../../physicsRaycastResult"; import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import { Epsilon } from "../../../Maths/math.constants"; declare let OIMO: any; @@ -134,7 +134,7 @@ export class OimoJSPlugin implements IPhysicsEnginePluginV1 { addToArray(impostor.object); const checkWithEpsilon = (value: number): number => { - return Math.max(value, PhysicsEngineV1.Epsilon); + return Math.max(value, Epsilon); }; const globalQuaternion: Quaternion = new Quaternion(); @@ -507,4 +507,16 @@ export class OimoJSPlugin implements IPhysicsEnginePluginV1 { return this._raycastResult; } + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @param result resulting PhysicsRaycastResult + */ + public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void { + Logger.Warn("raycast is not currently supported by the Oimo physics plugin"); + + result.reset(from, to); + } } diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts index b7b6876f52b..41021f923a5 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts @@ -15,8 +15,6 @@ export class PhysicsEngineV1 implements IPhysicsEngine { /** * Global value used to control the smallest number supported by the simulation */ - public static Epsilon = 0.001; - private _impostors: Array = []; private _joints: Array = []; private _subTimeStep: number = 0; @@ -27,6 +25,13 @@ export class PhysicsEngineV1 implements IPhysicsEngine { */ public gravity: Vector3; + /** + * + * @returns version + */ + public getPluginVersion(): number { + return 1; + } /** * Factory used to create the default physics plugin. * @returns The default physics plugin @@ -252,4 +257,14 @@ export class PhysicsEngineV1 implements IPhysicsEngine { public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { return this._physicsPlugin.raycast(from, to); } + + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @param result resulting PhysicsRaycastResult + */ + public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult) { + return this._physicsPlugin.raycastToRef(from, to, result); + } } diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts index c82f1586ec8..c1d8ed7229e 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts @@ -117,10 +117,10 @@ export interface IPhysicsEnginePluginV2 { setAngularDamping(body: PhysicsBody, damping: number): void; getAngularDamping(body: PhysicsBody): number; setLinearVelocity(body: PhysicsBody, linVel: Vector3): void; - getLinearVelocity(body: PhysicsBody): Vector3; + getLinearVelocityToRef(body: PhysicsBody, linVel: Vector3): void; applyImpulse(body: PhysicsBody, location: Vector3, impulse: Vector3): void; setAngularVelocity(body: PhysicsBody, angVel: Vector3): void; - getAngularVelocity(body: PhysicsBody): Vector3; + getAngularVelocityToRef(body: PhysicsBody, angVel: Vector3): void; disposeBody(body: PhysicsBody): void; // shape @@ -174,7 +174,7 @@ export interface IPhysicsEnginePluginV2 { disposeConstraint(constraint: PhysicsConstraint): void; // raycast - raycast(from: Vector3, to: Vector3): PhysicsRaycastResult; + raycast(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void; dispose(): void; } diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index 2fde83ff1f5..d718371099a 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -1,6 +1,6 @@ import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEnginePluginV2"; import type { PhysicsShape } from "./physicsShape"; -import { Vector3 } from "../../Maths/math.vector"; +import type { Vector3 } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; /** @@ -10,7 +10,7 @@ export class PhysicsBody { /** @internal */ public _pluginData: any = undefined; - private _physicsPlugin: IPhysicsEnginePluginV2 | undefined; + private _physicsPlugin: IPhysicsEnginePluginV2; /** * @@ -21,15 +21,27 @@ export class PhysicsBody { if (!scene) { return; } - this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; - this._physicsPlugin?.initBody(this); + const physicsEngine = scene.getPhysicsEngine(); + if (!physicsEngine) { + throw new Error("No Physics Engine available."); + } + if (physicsEngine.getPluginVersion() != 2) { + throw new Error("Plugin version is incorrect. Expected version 2."); + } + const physicsPlugin = physicsEngine.getPhysicsPlugin(); + if (!physicsPlugin) { + throw new Error("No Physics Plugin available."); + } + + this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin.initBody(this); } /** * * @param shape */ public setShape(shape: PhysicsShape): void { - this._physicsPlugin?.setShape(this, shape); + this._physicsPlugin.setShape(this, shape); } /** @@ -37,7 +49,7 @@ export class PhysicsBody { * @returns */ public getShape(): PhysicsShape | undefined { - return this._physicsPlugin ? this._physicsPlugin.getShape(this) : undefined; + return this._physicsPlugin.getShape(this); } /** @@ -45,7 +57,7 @@ export class PhysicsBody { * @param group */ public setFilterGroup(group: number): void { - this._physicsPlugin?.setFilterGroup(this, group); + this._physicsPlugin.setFilterGroup(this, group); } /** @@ -53,7 +65,7 @@ export class PhysicsBody { * @returns */ public getFilterGroup(): number { - return this._physicsPlugin ? this._physicsPlugin.getFilterGroup(this) : 0; + return this._physicsPlugin.getFilterGroup(this); } /** @@ -61,7 +73,7 @@ export class PhysicsBody { * @param eventMask */ public setEventMask(eventMask: number): void { - this._physicsPlugin?.setEventMask(this, eventMask); + this._physicsPlugin.setEventMask(this, eventMask); } /** @@ -69,7 +81,7 @@ export class PhysicsBody { * @returns */ public getEventMask(): number { - return this._physicsPlugin ? this._physicsPlugin.getEventMask(this) : 0; + return this._physicsPlugin.getEventMask(this); } /** @@ -77,7 +89,7 @@ export class PhysicsBody { * @param massProps */ public setMassProperties(massProps: MassProperties): void { - this._physicsPlugin?.setMassProperties(this, massProps); + this._physicsPlugin.setMassProperties(this, massProps); } /** @@ -85,7 +97,7 @@ export class PhysicsBody { * @returns */ public getMassProperties(): MassProperties | undefined { - return this._physicsPlugin ? this._physicsPlugin.getMassProperties(this) : undefined; + return this._physicsPlugin.getMassProperties(this); } /** @@ -93,7 +105,7 @@ export class PhysicsBody { * @param damping */ public setLinearDamping(damping: number): void { - this._physicsPlugin?.setLinearDamping(this, damping); + this._physicsPlugin.setLinearDamping(this, damping); } /** @@ -101,7 +113,7 @@ export class PhysicsBody { * @returns */ public getLinearDamping(): number { - return this._physicsPlugin ? this._physicsPlugin.getLinearDamping(this) : 0; + return this._physicsPlugin.getLinearDamping(this); } /** @@ -109,7 +121,7 @@ export class PhysicsBody { * @param damping */ public setAngularDamping(damping: number): void { - this._physicsPlugin?.setAngularDamping(this, damping); + this._physicsPlugin.setAngularDamping(this, damping); } /** @@ -117,7 +129,7 @@ export class PhysicsBody { * @returns */ public getAngularDamping(): number { - return this._physicsPlugin ? this._physicsPlugin.getAngularDamping(this) : 0; + return this._physicsPlugin.getAngularDamping(this); } /** @@ -125,15 +137,15 @@ export class PhysicsBody { * @param linVel */ public setLinearVelocity(linVel: Vector3): void { - this._physicsPlugin?.setLinearVelocity(this, linVel); + this._physicsPlugin.setLinearVelocity(this, linVel); } /** * * @returns */ - public getLinearVelocity(): Vector3 { - return this._physicsPlugin ? this._physicsPlugin.getLinearVelocity(this) : Vector3.Zero(); + public getLinearVelocityToRef(linVel: Vector3): void { + return this._physicsPlugin.getLinearVelocityToRef(this, linVel); } /** @@ -141,15 +153,15 @@ export class PhysicsBody { * @param angVel */ public setAngularVelocity(angVel: Vector3): void { - this._physicsPlugin?.setAngularVelocity(this, angVel); + this._physicsPlugin.setAngularVelocity(this, angVel); } /** * * @returns */ - public getAngularVelocity(): Vector3 { - return this._physicsPlugin ? this._physicsPlugin.getAngularVelocity(this) : Vector3.Zero(); + public getAngularVelocityToRef(angVel: Vector3): void { + return this._physicsPlugin.getAngularVelocityToRef(this, angVel); } /** @@ -158,13 +170,13 @@ export class PhysicsBody { * @param impulse */ public applyImpulse(location: Vector3, impulse: Vector3): void { - this._physicsPlugin?.applyImpulse(this, location, impulse); + this._physicsPlugin.applyImpulse(this, location, impulse); } /** * */ public dispose() { - this._physicsPlugin?.disposeBody(this); + this._physicsPlugin.disposeBody(this); } } diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index de0d2be363d..f57a1dd01c1 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -1,7 +1,7 @@ import type { Scene } from "../../scene"; import type { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters } from "./IPhysicsEnginePluginV2"; -import { ConstraintType, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePluginV2"; +import { ConstraintType } from "./IPhysicsEnginePluginV2"; import type { PhysicsBody } from "./physicsBody"; /** @@ -14,7 +14,7 @@ export class PhysicsConstraint { * */ public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePluginV2 | undefined; + protected _physicsPlugin: IPhysicsEnginePluginV2; /** * @@ -23,9 +23,20 @@ export class PhysicsConstraint { if (!scene) { return; } - const physicsEngine = scene.getPhysicsEngine() as any; - this._physicsPlugin = physicsEngine?.getPhysicsPlugin(); - this._physicsPlugin?.initConstraint(this, type, options); + const physicsEngine = scene.getPhysicsEngine(); + if (!physicsEngine) { + throw new Error("No Physics Engine available."); + } + if (physicsEngine.getPluginVersion() != 2) { + throw new Error("Plugin version is incorrect. Expected version 2."); + } + const physicsPlugin = physicsEngine.getPhysicsPlugin(); + if (!physicsPlugin) { + throw new Error("No Physics Plugin available."); + } + + this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin.initConstraint(this, type, options); } /** @@ -33,7 +44,7 @@ export class PhysicsConstraint { * @param body */ public setParentBody(body: PhysicsBody): void { - this._physicsPlugin?.setParentBody(this, body); + this._physicsPlugin.setParentBody(this, body); } /** @@ -41,7 +52,7 @@ export class PhysicsConstraint { * @returns */ public getParentBody(): PhysicsBody | undefined { - return this._physicsPlugin ? this._physicsPlugin.getParentBody(this) : undefined; + return this._physicsPlugin.getParentBody(this); } /** @@ -49,7 +60,7 @@ export class PhysicsConstraint { * @param body */ public setChildBody(body: PhysicsBody): void { - this._physicsPlugin?.setChildBody(this, body); + this._physicsPlugin.setChildBody(this, body); } /** @@ -57,7 +68,7 @@ export class PhysicsConstraint { * @returns */ public getChildBody(): PhysicsBody | undefined { - return this._physicsPlugin ? this._physicsPlugin.getChildBody(this) : undefined; + return this._physicsPlugin.getChildBody(this); } /** @@ -67,7 +78,7 @@ export class PhysicsConstraint { * @param axisY */ public setAnchorInParent(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { - this._physicsPlugin?.setAnchorInParent(this, pivot, axisX, axisY); + this._physicsPlugin.setAnchorInParent(this, pivot, axisX, axisY); } /** @@ -77,7 +88,7 @@ export class PhysicsConstraint { * @param axisY */ public setAnchorInChild(pivot: Vector3, axisX: Vector3, axisY: Vector3): void { - this._physicsPlugin?.setAnchorInChild(this, pivot, axisX, axisY); + this._physicsPlugin.setAnchorInChild(this, pivot, axisX, axisY); } /** @@ -85,7 +96,7 @@ export class PhysicsConstraint { * @param isEnabled */ public setEnabled(isEnabled: boolean): void { - this._physicsPlugin?.setEnabled(this, isEnabled); + this._physicsPlugin.setEnabled(this, isEnabled); } /** @@ -93,7 +104,7 @@ export class PhysicsConstraint { * @returns */ public getEnabled(): boolean { - return this._physicsPlugin ? this._physicsPlugin.getEnabled(this) : false; + return this._physicsPlugin.getEnabled(this); } /** @@ -101,7 +112,7 @@ export class PhysicsConstraint { * @param isEnabled */ public setCollisionsEnabled(isEnabled: boolean): void { - this._physicsPlugin?.setCollisionsEnabled(this, isEnabled); + this._physicsPlugin.setCollisionsEnabled(this, isEnabled); } /** @@ -109,7 +120,7 @@ export class PhysicsConstraint { * @returns */ public getCollisionsEnabled(): boolean { - return this._physicsPlugin ? this._physicsPlugin.getCollisionsEnabled(this) : false; + return this._physicsPlugin.getCollisionsEnabled(this); } /** @@ -118,7 +129,7 @@ export class PhysicsConstraint { * @param friction */ public setAxisFriction(axis: ConstraintAxis, friction: number): void { - this._physicsPlugin?.setAxisFriction(this, axis, friction); + this._physicsPlugin.setAxisFriction(this, axis, friction); } /** @@ -127,7 +138,7 @@ export class PhysicsConstraint { * @returns */ public getAxisFriction(axis: ConstraintAxis): number { - return this._physicsPlugin ? this._physicsPlugin.getAxisFriction(this, axis) : 0; + return this._physicsPlugin.getAxisFriction(this, axis); } /** @@ -136,91 +147,91 @@ export class PhysicsConstraint { * @param limitMode */ public setAxisMode(axis: ConstraintAxis, limitMode: ConstraintAxisLimitMode): void { - this._physicsPlugin?.setAxisMode(this, axis, limitMode); + this._physicsPlugin.setAxisMode(this, axis, limitMode); } /** * * @param axis */ public getAxisMode(axis: ConstraintAxis): ConstraintAxisLimitMode { - return this._physicsPlugin ? this._physicsPlugin.getAxisMode(this, axis) : ConstraintAxisLimitMode.NONE; + return this._physicsPlugin.getAxisMode(this, axis); } /** * */ public setAxisMinLimit(axis: ConstraintAxis, minLimit: number): void { - this._physicsPlugin?.setAxisMinLimit(this, axis, minLimit); + this._physicsPlugin.setAxisMinLimit(this, axis, minLimit); } /** * */ public getAxisMinLimit(axis: ConstraintAxis): number { - return this._physicsPlugin ? this._physicsPlugin.getAxisMinLimit(this, axis) : 0; + return this._physicsPlugin.getAxisMinLimit(this, axis); } /** * */ public setAxisMaxLimit(axis: ConstraintAxis, limit: number): void { - this._physicsPlugin?.setAxisMaxLimit(this, axis, limit); + this._physicsPlugin.setAxisMaxLimit(this, axis, limit); } /** * */ public getAxisMaxLimit(axis: ConstraintAxis): number { - return this._physicsPlugin ? this._physicsPlugin.getAxisMaxLimit(this, axis) : 0; + return this._physicsPlugin.getAxisMaxLimit(this, axis); } /** * */ public setAxisMotorType(axis: ConstraintAxis, motorType: ConstraintMotorType): void { - this._physicsPlugin?.setAxisMotorType(this, axis, motorType); + this._physicsPlugin.setAxisMotorType(this, axis, motorType); } /** * */ public getAxisMotorType(axis: ConstraintAxis): ConstraintMotorType { - return this._physicsPlugin ? this._physicsPlugin.getAxisMotorType(this, axis) : ConstraintMotorType.NONE; + return this._physicsPlugin.getAxisMotorType(this, axis); } /** * */ public setAxisMotorTarget(axis: ConstraintAxis, target: number): void { - this._physicsPlugin?.setAxisMotorTarget(this, axis, target); + this._physicsPlugin.setAxisMotorTarget(this, axis, target); } /** * */ public getAxisMotorTarget(axis: ConstraintAxis): number { - return this._physicsPlugin ? this._physicsPlugin.getAxisMotorTarget(this, axis) : 0; + return this._physicsPlugin.getAxisMotorTarget(this, axis); } /** * */ public setAxisMotorMaxForce(axis: ConstraintAxis, maxForce: number): void { - this._physicsPlugin?.setAxisMotorMaxForce(this, axis, maxForce); + this._physicsPlugin.setAxisMotorMaxForce(this, axis, maxForce); } /** * */ public getAxisMotorMaxForce(axis: ConstraintAxis): number { - return this._physicsPlugin ? this._physicsPlugin.getAxisMotorMaxForce(this, axis) : 0; + return this._physicsPlugin.getAxisMotorMaxForce(this, axis); } /** * */ public dispose(): void { - this._physicsPlugin?.disposeConstraint(this); + this._physicsPlugin.disposeConstraint(this); } } diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts index 66d09d4b514..628b72eade6 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts @@ -80,7 +80,7 @@ AbstractMesh.prototype.getPhysicsBody = function (): Nullable { */ AbstractMesh.prototype.applyImpulse = function (force: Vector3, contactPoint: Vector3): AbstractMesh { if (!this.physicsBody) { - return this; + throw new Error("No Physics Body for AbstractMesh"); } this.physicsBody.applyImpulse(force, contactPoint); return this; diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index 4346883767a..c95f71627e6 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -3,7 +3,7 @@ import { Vector3 } from "../../Maths/math.vector"; import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; -import type { PhysicsRaycastResult } from "../physicsRaycastResult"; +import { PhysicsRaycastResult } from "../physicsRaycastResult"; import { _WarnImport } from "../../Misc/devTools"; /** @@ -26,12 +26,15 @@ export class PhysicsEngineV2 implements IPhysicsEngine { */ public gravity: Vector3; + public getPluginVersion(): number { + return 2; + } /** * Factory used to create the default physics plugin. * @returns The default physics plugin */ public static DefaultPluginFactory(): IPhysicsEnginePluginV2 { - throw _WarnImport("CannonJSPlugin"); + throw _WarnImport(""); } /** @@ -141,6 +144,16 @@ export class PhysicsEngineV2 implements IPhysicsEngine { return this._physicsPlugin; } + /** + * Does a raycast in the physics world + * @param from when should the ray start? + * @param to when should the ray end? + * @param result resulting PhysicsRaycastResult + */ + public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void { + this._physicsPlugin.raycast(from, to, result); + } + /** * Does a raycast in the physics world * @param from when should the ray start? @@ -148,6 +161,8 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * @returns PhysicsRaycastResult */ public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult { - return this._physicsPlugin.raycast(from, to); + const result = new PhysicsRaycastResult(); + this._physicsPlugin.raycast(from, to, result); + return result; } } diff --git a/packages/dev/core/src/Physics/v2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts index ba0541d1be9..82b1d648445 100644 --- a/packages/dev/core/src/Physics/v2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -19,8 +19,20 @@ export class PhysicsMaterial { * @param scene */ constructor(friction: number, restitution: number, scene: Scene) { - this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; - this._physicsPlugin?.initMaterial(this); + const physicsEngine = scene.getPhysicsEngine(); + if (!physicsEngine) { + throw new Error("No Physics Engine available."); + } + if (physicsEngine.getPluginVersion() != 2) { + throw new Error("Plugin version is incorrect. Expected version 2."); + } + const physicsPlugin = physicsEngine.getPhysicsPlugin(); + if (!physicsPlugin) { + throw new Error("No Physics Plugin available."); + } + + this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin.initMaterial(this); } /** * diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index 764dc8f072c..5592a3cd22d 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -1,9 +1,9 @@ import type { TransformNode } from "../../Meshes/transformNode"; -import { BoundingBox } from "../../Culling/boundingBox"; +import type { BoundingBox } from "../../Culling/boundingBox"; import { ShapeType } from "./IPhysicsEnginePluginV2"; import type { IPhysicsEnginePluginV2, PhysicsShapeParameters } from "./IPhysicsEnginePluginV2"; import type { PhysicsMaterial } from "./physicsMaterial"; -import { Vector3 } from "../../Maths/math.vector"; +import type { Vector3 } from "../../Maths/math.vector"; import type { Quaternion } from "../../Maths/math.vector"; import type { AbstractMesh } from "../../Meshes/abstractMesh"; import type { Scene } from "../../scene"; @@ -32,11 +32,20 @@ export class PhysicsShape { return; } - this._physicsPlugin = scene.getPhysicsEngine()?.getPhysicsPlugin() as any; - if (!this._physicsPlugin) { - return; + const physicsEngine = scene.getPhysicsEngine(); + if (!physicsEngine) { + throw new Error("No Physics Engine available."); + } + if (physicsEngine.getPluginVersion() != 2) { + throw new Error("Plugin version is incorrect. Expected version 2."); } - this._physicsPlugin?.initShape(this, type, options); + const physicsPlugin = physicsEngine.getPhysicsPlugin(); + if (!physicsPlugin) { + throw new Error("No Physics Plugin available."); + } + + this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin.initShape(this, type, options); } /** @@ -51,7 +60,7 @@ export class PhysicsShape { * @param layer */ public setFilterLayer(layer: number): void { - this._physicsPlugin?.setFilterLayer(this, layer); + this._physicsPlugin.setFilterLayer(this, layer); } /** @@ -59,7 +68,7 @@ export class PhysicsShape { * @returns */ public getFilterLayer(): number { - return this._physicsPlugin ? this._physicsPlugin.getFilterLayer(this) : 0; + return this._physicsPlugin.getFilterLayer(this); } /** @@ -67,7 +76,7 @@ export class PhysicsShape { * @param materialId */ public setMaterial(materialId: PhysicsMaterial): void { - this._physicsPlugin?.setMaterial(this, materialId); + this._physicsPlugin.setMaterial(this, materialId); } /** @@ -75,7 +84,7 @@ export class PhysicsShape { * @returns */ public getMaterial(): PhysicsMaterial | undefined { - return this._physicsPlugin ? this._physicsPlugin.getMaterial(this) : undefined; + return this._physicsPlugin.getMaterial(this); } /** @@ -83,14 +92,14 @@ export class PhysicsShape { * @param density */ public setDensity(density: number): void { - this._physicsPlugin?.setDensity(this, density); + this._physicsPlugin.setDensity(this, density); } /** * */ public getDensity(): number { - return this._physicsPlugin ? this._physicsPlugin.getDensity(this) : 0; + return this._physicsPlugin.getDensity(this); } /** @@ -99,7 +108,7 @@ export class PhysicsShape { * @param childTransform */ public addChild(newChild: PhysicsShape, childTransform: TransformNode): void { - this._physicsPlugin?.addChild(this, newChild, childTransform); + this._physicsPlugin.addChild(this, newChild, childTransform); } /** @@ -107,7 +116,7 @@ export class PhysicsShape { * @param childIndex */ public removeChild(childIndex: number): void { - this._physicsPlugin?.removeChild(this, childIndex); + this._physicsPlugin.removeChild(this, childIndex); } /** @@ -115,21 +124,21 @@ export class PhysicsShape { * @returns */ public getNumChildren(): number { - return this._physicsPlugin ? this._physicsPlugin.getNumChildren(this) : 0; + return this._physicsPlugin.getNumChildren(this); } /** * */ public getBoundingBox(): BoundingBox { - return this._physicsPlugin ? this._physicsPlugin.getBoundingBox(this) : new BoundingBox(Vector3.Zero(), Vector3.Zero()); + return this._physicsPlugin.getBoundingBox(this); } /** * */ public dispose() { - this._physicsPlugin?.disposeShape(this); + this._physicsPlugin.disposeShape(this); } } From 5bbabef74131491c5611cb3a9b1e8edaf0d15c81 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 1 Dec 2022 11:47:56 +0100 Subject: [PATCH 23/28] physicsviewer plugin version --- packages/dev/core/src/Debug/physicsViewer.ts | 7 ++++--- .../dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts | 1 + packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts | 8 ++++++++ .../dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts | 8 ++++++++ packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts | 8 ++++++++ packages/dev/core/src/Physics/v1/physicsEngineV1.ts | 2 +- .../dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts | 1 + packages/dev/core/src/Physics/v2/physicsEngineV2.ts | 2 +- 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/dev/core/src/Debug/physicsViewer.ts b/packages/dev/core/src/Debug/physicsViewer.ts index 5fbc4de4b7b..3fb044f2dad 100644 --- a/packages/dev/core/src/Debug/physicsViewer.ts +++ b/packages/dev/core/src/Debug/physicsViewer.ts @@ -10,6 +10,7 @@ import type { Material } from "../Materials/material"; import { EngineStore } from "../Engines/engineStore"; import { StandardMaterial } from "../Materials/standardMaterial"; import type { IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePluginV2 } from "../Physics/v2/IPhysicsEnginePluginV2"; import { PhysicsImpostor } from "../Physics/v1/physicsImpostor"; import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer"; import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder"; @@ -30,7 +31,7 @@ export class PhysicsViewer { /** @internal */ protected _numMeshes = 0; /** @internal */ - protected _physicsEnginePlugin: Nullable; + protected _physicsEnginePlugin: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null; private _renderFunction: () => void; private _utilityLayer: Nullable; @@ -80,8 +81,8 @@ export class PhysicsViewer { } const mesh = this._meshes[i]; - if (mesh && plugin) { - plugin.syncMeshWithImpostor(mesh, impostor); + if (mesh && plugin && plugin.getPluginVersion() === 1) { + (plugin as IPhysicsEnginePluginV1).syncMeshWithImpostor(mesh, impostor); } } } diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts index 527f6ded522..a0597b4fe05 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts @@ -31,6 +31,7 @@ export interface IPhysicsEnginePluginV1 { setTimeStep(timeStep: number): void; getTimeStep(): number; executeStep(delta: number, impostors: Array): void; //not forgetting pre and post events + getPluginVersion(): number; applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void; generatePhysicsBody(impostor: PhysicsImpostor): void; diff --git a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts index 3f7ce33ddf6..20e6dcd742d 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts @@ -124,6 +124,14 @@ export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { this._tmpAmmoVectorD = new this.bjsAMMO.btVector3(0, 0, 0); } + /** + * + * @returns plugin version + */ + public getPluginVersion(): number { + return 1; + } + /** * Sets the gravity of the physics world (m/(s^2)) * @param gravity Gravity to set diff --git a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts index 983a9d3a25f..bf29183f609 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts @@ -46,6 +46,14 @@ export class CannonJSPlugin implements IPhysicsEnginePluginV1 { this._raycastResult = new PhysicsRaycastResult(); } + /** + * + * @returns plugin version + */ + public getPluginVersion(): number { + return 1; + } + public setGravity(gravity: Vector3): void { const vec = gravity; this.world.gravity.set(vec.x, vec.y, vec.z); diff --git a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts index f274946b59d..7a25c86e728 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts @@ -30,6 +30,14 @@ export class OimoJSPlugin implements IPhysicsEnginePluginV1 { this._raycastResult = new PhysicsRaycastResult(); } + /** + * + * @returns plugin version + */ + public getPluginVersion(): number { + return 1; + } + public setGravity(gravity: Vector3) { this.world.gravity.set(gravity.x, gravity.y, gravity.z); } diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts index 41021f923a5..29f9b6523cc 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngineV1.ts @@ -30,7 +30,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * @returns version */ public getPluginVersion(): number { - return 1; + return this._physicsPlugin.getPluginVersion(); } /** * Factory used to create the default physics plugin. diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts index c1d8ed7229e..e9d10c2248b 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts @@ -101,6 +101,7 @@ export interface IPhysicsEnginePluginV2 { setTimeStep(timeStep: number): void; getTimeStep(): number; executeStep(delta: number): void; //not forgetting pre and post events + getPluginVersion(): number; // body initBody(body: PhysicsBody): void; diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index c95f71627e6..b4d794c1c45 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -27,7 +27,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { public gravity: Vector3; public getPluginVersion(): number { - return 2; + return this._physicsPlugin.getPluginVersion(); } /** * Factory used to create the default physics plugin. From 1b2ea116215dd4905428ba11b8ce2183c5c87669 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 1 Dec 2022 14:20:53 +0100 Subject: [PATCH 24/28] @internal for created classes --- packages/dev/core/src/Physics/v2/physicsAggregate.ts | 1 + packages/dev/core/src/Physics/v2/physicsConstraint.ts | 6 ++++++ packages/dev/core/src/Physics/v2/physicsEngineV2.ts | 1 + packages/dev/core/src/Physics/v2/physicsMaterial.ts | 1 + packages/dev/core/src/Physics/v2/physicsShape.ts | 7 +++++++ 5 files changed, 16 insertions(+) diff --git a/packages/dev/core/src/Physics/v2/physicsAggregate.ts b/packages/dev/core/src/Physics/v2/physicsAggregate.ts index c27756f8eb3..43f206f9fa8 100644 --- a/packages/dev/core/src/Physics/v2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics/v2/physicsAggregate.ts @@ -9,6 +9,7 @@ import type { TransformNode } from "../../Meshes/transformNode"; * The interface for the physics aggregate parameters */ export interface PhysicsAggregateParameters { + /** @internal */ /** * The mass of the physics imposter */ diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index f57a1dd01c1..b954e41c2ee 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -10,6 +10,7 @@ import type { PhysicsBody } from "./physicsBody"; * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ export class PhysicsConstraint { + /** @internal */ /** * */ @@ -239,6 +240,7 @@ export class PhysicsConstraint { * */ export class PhysicsConstraintBallAndSocket extends PhysicsConstraint { + /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { super(ConstraintType.BALL_AND_SOCKET, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } @@ -248,6 +250,7 @@ export class PhysicsConstraintBallAndSocket extends PhysicsConstraint { * */ export class PhysicsConstraintDistance extends PhysicsConstraint { + /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { super(ConstraintType.DISTANCE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } @@ -257,6 +260,7 @@ export class PhysicsConstraintDistance extends PhysicsConstraint { * */ export class PhysicsConstraintHinge extends PhysicsConstraint { + /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { super(ConstraintType.HINGE, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } @@ -266,6 +270,7 @@ export class PhysicsConstraintHinge extends PhysicsConstraint { * */ export class PhysicsConstraintSlider extends PhysicsConstraint { + /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { super(ConstraintType.SLIDER, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } @@ -275,6 +280,7 @@ export class PhysicsConstraintSlider extends PhysicsConstraint { * */ export class PhysicsConstraintLock extends PhysicsConstraint { + /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { super(ConstraintType.LOCK, { pivotA: pivotA, pivotB: pivotB, axisA: axisA, axisB: axisB }, scene); } diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts index b4d794c1c45..fc04c4fd499 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineV2.ts @@ -11,6 +11,7 @@ import { _WarnImport } from "../../Misc/devTools"; * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ export class PhysicsEngineV2 implements IPhysicsEngine { + /** @internal */ /** * Global value used to control the smallest number supported by the simulation */ diff --git a/packages/dev/core/src/Physics/v2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts index 82b1d648445..97a12c0a910 100644 --- a/packages/dev/core/src/Physics/v2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -5,6 +5,7 @@ import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; * */ export class PhysicsMaterial { + /** @internal */ /** * */ diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index 5592a3cd22d..ecf61a34d6b 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -146,6 +146,7 @@ export class PhysicsShape { * */ export class PhysicsShapeSphere extends PhysicsShape { + /** @internal */ /** * * @param center @@ -161,6 +162,7 @@ export class PhysicsShapeSphere extends PhysicsShape { * */ export class PhysicsShapeCapsule extends PhysicsShape { + /** @internal */ /** * * @param pointA @@ -177,6 +179,7 @@ export class PhysicsShapeCapsule extends PhysicsShape { * */ export class PhysicsShapeCylinder extends PhysicsShape { + /** @internal */ /** * * @param pointA @@ -193,6 +196,7 @@ export class PhysicsShapeCylinder extends PhysicsShape { * */ export class PhysicsShapeShapeBox extends PhysicsShape { + /** @internal */ /** * * @param center @@ -209,6 +213,7 @@ export class PhysicsShapeShapeBox extends PhysicsShape { * */ export class PhysicsShapeShapeConvexHull extends PhysicsShape { + /** @internal */ /** * * @param mesh @@ -223,6 +228,7 @@ export class PhysicsShapeShapeConvexHull extends PhysicsShape { * */ export class PhysicsShapeShapeMesh extends PhysicsShape { + /** @internal */ /** * * @param mesh @@ -237,6 +243,7 @@ export class PhysicsShapeShapeMesh extends PhysicsShape { * */ export class PhysicsShapeShapeContainer extends PhysicsShape { + /** @internal */ /** * * @param mesh From 18f1dcf7bfb33139e461e1a0c351f4e5339ac200 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 1 Dec 2022 17:50:18 +0100 Subject: [PATCH 25/28] pr feedback --- packages/dev/core/src/Meshes/mesh.ts | 6 +++--- .../dev/core/src/Physics/physicsEngineComponent.ts | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/dev/core/src/Meshes/mesh.ts b/packages/dev/core/src/Meshes/mesh.ts index 70d5830bd6e..ba1de94b3d4 100644 --- a/packages/dev/core/src/Meshes/mesh.ts +++ b/packages/dev/core/src/Meshes/mesh.ts @@ -662,9 +662,9 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData { // Physics clone if (scene.getPhysicsEngine) { - const physicsEngine = scene.getPhysicsEngine() as PhysicsEngineV1; - if (clonePhysicsImpostor && physicsEngine) { - const impostor = physicsEngine.getImpostorForPhysicsObject(source); + const physicsEngine = scene.getPhysicsEngine(); + if (clonePhysicsImpostor && physicsEngine && physicsEngine.getPluginVersion() === 1) { + const impostor = (physicsEngine as PhysicsEngineV1).getImpostorForPhysicsObject(source); if (impostor) { this.physicsImpostor = impostor.clone(this); } diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index 1e18fd3fba8..5d8398daed8 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -10,6 +10,7 @@ import type { IPhysicsEngine } from "./IPhysicsEngine"; import type { IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePluginV1"; import type { IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePluginV2"; import { PhysicsEngineV1 } from "./v1/physicsEngineV1"; +import { PhysicsEngineV2 } from "./v2/physicsEngineV2"; declare module "../scene" { /** @@ -78,7 +79,7 @@ Scene.prototype.getPhysicsEngine = function (): Nullable { * @param plugin defines the physics engine to be used. defaults to CannonJS. * @returns a boolean indicating if the physics engine was initialized */ -Scene.prototype.enablePhysics = function (gravity: Nullable = null, plugin?: any): boolean { +Scene.prototype.enablePhysics = function (gravity: Nullable = null, plugin?: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2): boolean { if (this._physicsEngine) { return true; } @@ -91,7 +92,13 @@ Scene.prototype.enablePhysics = function (gravity: Nullable = null, plu } try { - this._physicsEngine = new PhysicsEngineV1(gravity, plugin); + if (plugin?.getPluginVersion() === 1) { + this._physicsEngine = new PhysicsEngineV1(gravity, plugin as IPhysicsEnginePluginV1); + } else if (plugin?.getPluginVersion() === 2) { + this._physicsEngine = new PhysicsEngineV2(gravity, plugin as IPhysicsEnginePluginV2); + } else { + throw new Error("Unsupported Physics plugin version."); + } this._physicsTimeAccumulator = 0; return true; } catch (e) { From 303e675a90d6e170c0665649ff00d689b6e3c05b Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 2 Dec 2022 15:56:44 +0100 Subject: [PATCH 26/28] naming and imports --- packages/dev/core/src/Debug/physicsViewer.ts | 4 ++-- packages/dev/core/src/Meshes/mesh.ts | 2 +- packages/dev/core/src/Physics/IPhysicsEngine.ts | 4 ++-- packages/dev/core/src/Physics/index.ts | 2 +- .../dev/core/src/Physics/physicsEngineComponent.ts | 8 ++++---- ...sicsEnginePluginV1.ts => IPhysicsEnginePlugin.ts} | 2 +- .../dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts | 4 ++-- .../core/src/Physics/v1/Plugins/cannonJSPlugin.ts | 8 ++++---- .../dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts | 4 ++-- packages/dev/core/src/Physics/v1/index.ts | 6 +++--- .../v1/{physicsEngineV1.ts => physicsEngine.ts} | 10 +++++----- ...ngineComponentV1.ts => physicsEngineComponent.ts} | 0 packages/dev/core/src/Physics/v1/physicsHelper.ts | 12 ++++++------ packages/dev/core/src/Physics/v1/physicsImpostor.ts | 2 +- packages/dev/core/src/Physics/v1/physicsJoint.ts | 6 +++--- ...sicsEnginePluginV2.ts => IPhysicsEnginePlugin.ts} | 2 +- packages/dev/core/src/Physics/v2/index.ts | 2 +- packages/dev/core/src/Physics/v2/physicsBody.ts | 6 +++--- .../dev/core/src/Physics/v2/physicsConstraint.ts | 8 ++++---- .../v2/{physicsEngineV2.ts => physicsEngine.ts} | 11 +++++------ ...ngineComponentV2.ts => physicsEngineComponent.ts} | 0 packages/dev/core/src/Physics/v2/physicsMaterial.ts | 6 +++--- packages/dev/core/src/Physics/v2/physicsShape.ts | 8 ++++---- .../components/actionTabs/tabs/debugTabComponent.tsx | 4 ++-- .../meshes/meshPropertyGridComponent.tsx | 4 ++-- .../propertyGrids/scenePropertyGridComponent.tsx | 4 ++-- 26 files changed, 64 insertions(+), 65 deletions(-) rename packages/dev/core/src/Physics/v1/{IPhysicsEnginePluginV1.ts => IPhysicsEnginePlugin.ts} (97%) rename packages/dev/core/src/Physics/v1/{physicsEngineV1.ts => physicsEngine.ts} (93%) rename packages/dev/core/src/Physics/v1/{physicsEngineComponentV1.ts => physicsEngineComponent.ts} (100%) rename packages/dev/core/src/Physics/v2/{IPhysicsEnginePluginV2.ts => IPhysicsEnginePlugin.ts} (96%) rename packages/dev/core/src/Physics/v2/{physicsEngineV2.ts => physicsEngine.ts} (90%) rename packages/dev/core/src/Physics/v2/{physicsEngineComponentV2.ts => physicsEngineComponent.ts} (100%) diff --git a/packages/dev/core/src/Debug/physicsViewer.ts b/packages/dev/core/src/Debug/physicsViewer.ts index 3fb044f2dad..6f70bb70a57 100644 --- a/packages/dev/core/src/Debug/physicsViewer.ts +++ b/packages/dev/core/src/Debug/physicsViewer.ts @@ -9,8 +9,8 @@ import { Color3 } from "../Maths/math.color"; import type { Material } from "../Materials/material"; import { EngineStore } from "../Engines/engineStore"; import { StandardMaterial } from "../Materials/standardMaterial"; -import type { IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePluginV1"; -import type { IPhysicsEnginePluginV2 } from "../Physics/v2/IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePlugin"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV2 } from "../Physics/v2/IPhysicsEnginePlugin"; import { PhysicsImpostor } from "../Physics/v1/physicsImpostor"; import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer"; import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder"; diff --git a/packages/dev/core/src/Meshes/mesh.ts b/packages/dev/core/src/Meshes/mesh.ts index ba1de94b3d4..a6c1995acf5 100644 --- a/packages/dev/core/src/Meshes/mesh.ts +++ b/packages/dev/core/src/Meshes/mesh.ts @@ -38,7 +38,7 @@ import type { Path3D } from "../Maths/math.path"; import type { Plane } from "../Maths/math.plane"; import type { TransformNode } from "./transformNode"; import type { DrawWrapper } from "../Materials/drawWrapper"; -import type { PhysicsEngineV1 } from "../Physics/v1/physicsEngineV1"; +import type { PhysicsEngine as PhysicsEngineV1 } from "../Physics/v1/physicsEngine"; declare type GoldbergMesh = import("./goldbergMesh").GoldbergMesh; declare type InstancedMesh = import("./instancedMesh").InstancedMesh; diff --git a/packages/dev/core/src/Physics/IPhysicsEngine.ts b/packages/dev/core/src/Physics/IPhysicsEngine.ts index 2a645999555..2cf54d393fb 100644 --- a/packages/dev/core/src/Physics/IPhysicsEngine.ts +++ b/packages/dev/core/src/Physics/IPhysicsEngine.ts @@ -1,7 +1,7 @@ import type { Vector3 } from "../Maths/math.vector"; import type { PhysicsRaycastResult } from "./physicsRaycastResult"; -import type { IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePluginV1"; -import type { IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePlugin"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePlugin"; /** * Interface used to define a physics engine diff --git a/packages/dev/core/src/Physics/index.ts b/packages/dev/core/src/Physics/index.ts index 5a53e2d707e..e5958535f36 100644 --- a/packages/dev/core/src/Physics/index.ts +++ b/packages/dev/core/src/Physics/index.ts @@ -2,4 +2,4 @@ export * from "./v1/index"; export * from "./v2/index"; export * from "./physicsEngineComponent"; -export * from "./v1/physicsEngineComponentV1"; +export * from "./v1/physicsEngineComponent"; diff --git a/packages/dev/core/src/Physics/physicsEngineComponent.ts b/packages/dev/core/src/Physics/physicsEngineComponent.ts index 5d8398daed8..0769d8e9179 100644 --- a/packages/dev/core/src/Physics/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/physicsEngineComponent.ts @@ -7,10 +7,10 @@ import type { ISceneComponent } from "../sceneComponent"; import { SceneComponentConstants } from "../sceneComponent"; import { Scene } from "../scene"; import type { IPhysicsEngine } from "./IPhysicsEngine"; -import type { IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePluginV1"; -import type { IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePluginV2"; -import { PhysicsEngineV1 } from "./v1/physicsEngineV1"; -import { PhysicsEngineV2 } from "./v2/physicsEngineV2"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePlugin"; +import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePlugin"; +import { PhysicsEngine as PhysicsEngineV1 } from "./v1/physicsEngine"; +import { PhysicsEngine as PhysicsEngineV2 } from "./v2/physicsEngine"; declare module "../scene" { /** diff --git a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts b/packages/dev/core/src/Physics/v1/IPhysicsEnginePlugin.ts similarity index 97% rename from packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts rename to packages/dev/core/src/Physics/v1/IPhysicsEnginePlugin.ts index a0597b4fe05..700b580b294 100644 --- a/packages/dev/core/src/Physics/v1/IPhysicsEnginePluginV1.ts +++ b/packages/dev/core/src/Physics/v1/IPhysicsEnginePlugin.ts @@ -18,7 +18,7 @@ export interface PhysicsImpostorJoint { } /** @internal */ -export interface IPhysicsEnginePluginV1 { +export interface IPhysicsEnginePlugin { /** * */ diff --git a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts index 20e6dcd742d..90b41fa5af7 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/ammoJSPlugin.ts @@ -1,5 +1,5 @@ import { Quaternion, Vector3, Matrix } from "../../../Maths/math.vector"; -import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../IPhysicsEnginePlugin"; import { Logger } from "../../../Misc/logger"; import type { IPhysicsEnabledObject } from "../physicsImpostor"; import { PhysicsImpostor } from "../physicsImpostor"; @@ -25,7 +25,7 @@ declare let Ammo: any; * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine * @see https://github.com/kripken/ammo.js/ */ -export class AmmoJSPlugin implements IPhysicsEnginePluginV1 { +export class AmmoJSPlugin implements IPhysicsEnginePlugin { /** * Reference to the Ammo library */ diff --git a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts index bf29183f609..b056010566b 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/cannonJSPlugin.ts @@ -3,21 +3,21 @@ import { Logger } from "../../../Misc/logger"; import { Vector3, Matrix, Quaternion } from "../../../Maths/math.vector"; import { VertexBuffer } from "../../../Buffers/buffer"; import type { AbstractMesh } from "../../../Meshes/abstractMesh"; -import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../IPhysicsEnginePlugin"; import type { IPhysicsEnabledObject } from "..//physicsImpostor"; import { PhysicsImpostor } from "..//physicsImpostor"; import type { IMotorEnabledJoint, DistanceJointData, SpringJointData } from "../physicsJoint"; import { PhysicsJoint } from "../physicsJoint"; import { PhysicsRaycastResult } from "../../physicsRaycastResult"; import type { TransformNode } from "../../../Meshes/transformNode"; -import { PhysicsEngineV1 } from "../physicsEngineV1"; +import { PhysicsEngine } from "../physicsEngine"; import { Epsilon } from "../../../Maths/math.constants"; //declare var require: any; declare let CANNON: any; /** @internal */ -export class CannonJSPlugin implements IPhysicsEnginePluginV1 { +export class CannonJSPlugin implements IPhysicsEnginePlugin { public world: any; public name: string = "CannonJSPlugin"; private _physicsMaterials = new Array(); @@ -782,6 +782,6 @@ export class CannonJSPlugin implements IPhysicsEnginePluginV1 { } } -PhysicsEngineV1.DefaultPluginFactory = () => { +PhysicsEngine.DefaultPluginFactory = () => { return new CannonJSPlugin(); }; diff --git a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts index 7a25c86e728..ff7d39aad07 100644 --- a/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts +++ b/packages/dev/core/src/Physics/v1/Plugins/oimoJSPlugin.ts @@ -7,13 +7,13 @@ import { Vector3, Quaternion } from "../../../Maths/math.vector"; import type { Nullable } from "../../../types"; import { Logger } from "../../../Misc/logger"; import { PhysicsRaycastResult } from "../../physicsRaycastResult"; -import type { IPhysicsEnginePluginV1, PhysicsImpostorJoint } from "../IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../IPhysicsEnginePlugin"; import { Epsilon } from "../../../Maths/math.constants"; declare let OIMO: any; /** @internal */ -export class OimoJSPlugin implements IPhysicsEnginePluginV1 { +export class OimoJSPlugin implements IPhysicsEnginePlugin { public world: any; public name: string = "OimoJSPlugin"; // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/packages/dev/core/src/Physics/v1/index.ts b/packages/dev/core/src/Physics/v1/index.ts index f0253c685e6..3fc1fc56741 100644 --- a/packages/dev/core/src/Physics/v1/index.ts +++ b/packages/dev/core/src/Physics/v1/index.ts @@ -1,7 +1,7 @@ /* eslint-disable import/no-internal-modules */ -export * from "./IPhysicsEnginePluginV1"; -export * from "./physicsEngineV1"; -export * from "./physicsEngineComponentV1"; +export * from "./IPhysicsEnginePlugin"; +export * from "./physicsEngine"; +export * from "./physicsEngineComponent"; export * from "./physicsImpostor"; export * from "./physicsJoint"; export * from "./Plugins/index"; diff --git a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts b/packages/dev/core/src/Physics/v1/physicsEngine.ts similarity index 93% rename from packages/dev/core/src/Physics/v1/physicsEngineV1.ts rename to packages/dev/core/src/Physics/v1/physicsEngine.ts index 29f9b6523cc..c0374f773e7 100644 --- a/packages/dev/core/src/Physics/v1/physicsEngineV1.ts +++ b/packages/dev/core/src/Physics/v1/physicsEngine.ts @@ -1,6 +1,6 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; -import type { PhysicsImpostorJoint, IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; +import type { PhysicsImpostorJoint, IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; import type { IPhysicsEngine } from "../IPhysicsEngine"; import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor"; import type { PhysicsJoint } from "./physicsJoint"; @@ -11,7 +11,7 @@ import { _WarnImport } from "../../Misc/devTools"; * Class used to control physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export class PhysicsEngineV1 implements IPhysicsEngine { +export class PhysicsEngine implements IPhysicsEngine { /** * Global value used to control the smallest number supported by the simulation */ @@ -36,7 +36,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * Factory used to create the default physics plugin. * @returns The default physics plugin */ - public static DefaultPluginFactory(): IPhysicsEnginePluginV1 { + public static DefaultPluginFactory(): IPhysicsEnginePlugin { throw _WarnImport("CannonJSPlugin"); } @@ -45,7 +45,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * @param gravity defines the gravity vector used by the simulation * @param _physicsPlugin defines the plugin to use (CannonJS by default) */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePluginV1 = PhysicsEngineV1.DefaultPluginFactory()) { + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { if (!this._physicsPlugin.isSupported()) { throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "Please make sure it is included."); } @@ -206,7 +206,7 @@ export class PhysicsEngineV1 implements IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - public getPhysicsPlugin(): IPhysicsEnginePluginV1 { + public getPhysicsPlugin(): IPhysicsEnginePlugin { return this._physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts b/packages/dev/core/src/Physics/v1/physicsEngineComponent.ts similarity index 100% rename from packages/dev/core/src/Physics/v1/physicsEngineComponentV1.ts rename to packages/dev/core/src/Physics/v1/physicsEngineComponent.ts diff --git a/packages/dev/core/src/Physics/v1/physicsHelper.ts b/packages/dev/core/src/Physics/v1/physicsHelper.ts index 4ccadfb5e29..c88819c0a6d 100644 --- a/packages/dev/core/src/Physics/v1/physicsHelper.ts +++ b/packages/dev/core/src/Physics/v1/physicsHelper.ts @@ -7,7 +7,7 @@ import { CreateSphere } from "../../Meshes/Builders/sphereBuilder"; import { CreateCylinder } from "../../Meshes/Builders/cylinderBuilder"; import { Ray } from "../../Culling/ray"; import type { Scene } from "../../scene"; -import type { PhysicsEngineV1 } from "./physicsEngineV1"; +import type { PhysicsEngine } from "./physicsEngine"; import type { PhysicsImpostor } from "./physicsImpostor"; /** @@ -16,7 +16,7 @@ import type { PhysicsImpostor } from "./physicsImpostor"; */ export class PhysicsHelper { private _scene: Scene; - private _physicsEngine: Nullable; + private _physicsEngine: Nullable; /** * Initializes the Physics helper @@ -458,7 +458,7 @@ class PhysicsGravitationalFieldEvent { * Represents a physics updraft event */ class PhysicsUpdraftEvent { - private _physicsEngine: PhysicsEngineV1; + private _physicsEngine: PhysicsEngine; private _originTop: Vector3 = Vector3.Zero(); // the most upper part of the cylinder private _originDirection: Vector3 = Vector3.Zero(); // used if the updraftMode is perpendicular private _tickCallback: any; @@ -473,7 +473,7 @@ class PhysicsUpdraftEvent { * @param _options The options for the updraft event */ constructor(private _scene: Scene, private _origin: Vector3, private _options: PhysicsUpdraftEventOptions) { - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine(); this._options = { ...new PhysicsUpdraftEventOptions(), ...this._options }; this._origin.addToRef(new Vector3(0, this._options.height / 2, 0), this._cylinderPosition); @@ -601,7 +601,7 @@ class PhysicsUpdraftEvent { * Represents a physics vortex event */ class PhysicsVortexEvent { - private _physicsEngine: PhysicsEngineV1; + private _physicsEngine: PhysicsEngine; private _originTop: Vector3 = Vector3.Zero(); // the most upper part of the cylinder private _tickCallback: any; private _cylinder: Mesh; @@ -615,7 +615,7 @@ class PhysicsVortexEvent { * @param _options The options for the vortex event */ constructor(private _scene: Scene, private _origin: Vector3, private _options: PhysicsVortexEventOptions) { - this._physicsEngine = this._scene.getPhysicsEngine(); + this._physicsEngine = this._scene.getPhysicsEngine(); this._options = { ...new PhysicsVortexEventOptions(), ...this._options }; this._origin.addToRef(new Vector3(0, this._options.height / 2, 0), this._cylinderPosition); diff --git a/packages/dev/core/src/Physics/v1/physicsImpostor.ts b/packages/dev/core/src/Physics/v1/physicsImpostor.ts index 787141cd9e6..0ff97d8f6c8 100644 --- a/packages/dev/core/src/Physics/v1/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/v1/physicsImpostor.ts @@ -9,7 +9,7 @@ import { Mesh } from "../../Meshes/mesh"; import type { Scene } from "../../scene"; import type { Bone } from "../../Bones/bone"; import type { BoundingInfo } from "../../Culling/boundingInfo"; -import type { PhysicsEngineV1 } from "./physicsEngineV1"; +import type { PhysicsEngine as PhysicsEngineV1 } from "./physicsEngine"; import type { PhysicsJointData } from "./physicsJoint"; import { PhysicsJoint } from "./physicsJoint"; diff --git a/packages/dev/core/src/Physics/v1/physicsJoint.ts b/packages/dev/core/src/Physics/v1/physicsJoint.ts index 9944b34ec2b..83c2f31e06a 100644 --- a/packages/dev/core/src/Physics/v1/physicsJoint.ts +++ b/packages/dev/core/src/Physics/v1/physicsJoint.ts @@ -1,5 +1,5 @@ import type { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEnginePluginV1 } from "./IPhysicsEnginePluginV1"; +import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; /** * Interface for Physics-Joint data * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine @@ -39,7 +39,7 @@ export interface PhysicsJointData { */ export class PhysicsJoint { private _physicsJoint: any; - protected _physicsPlugin: IPhysicsEnginePluginV1; + protected _physicsPlugin: IPhysicsEnginePlugin; /** * Initializes the physics joint @@ -80,7 +80,7 @@ export class PhysicsJoint { /** * Sets the physics plugin */ - public set physicsPlugin(physicsPlugin: IPhysicsEnginePluginV1) { + public set physicsPlugin(physicsPlugin: IPhysicsEnginePlugin) { this._physicsPlugin = physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts similarity index 96% rename from packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts rename to packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index e9d10c2248b..dd4d84d3ff1 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePluginV2.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -88,7 +88,7 @@ export interface MassProperties { } /** @internal */ -export interface IPhysicsEnginePluginV2 { +export interface IPhysicsEnginePlugin { /** * */ diff --git a/packages/dev/core/src/Physics/v2/index.ts b/packages/dev/core/src/Physics/v2/index.ts index 3240b6eecda..14c3f37c93a 100644 --- a/packages/dev/core/src/Physics/v2/index.ts +++ b/packages/dev/core/src/Physics/v2/index.ts @@ -1,5 +1,5 @@ /* eslint-disable import/no-internal-modules */ -export * from "./physicsEngineV2"; +export { PhysicsEngine as PhysicsEngineV2 } from "./physicsEngine"; export * from "./physicsBody"; export * from "./physicsShape"; export * from "./physicsConstraint"; diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index d718371099a..476f9c5d437 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -1,4 +1,4 @@ -import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEnginePlugin"; import type { PhysicsShape } from "./physicsShape"; import type { Vector3 } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; @@ -10,7 +10,7 @@ export class PhysicsBody { /** @internal */ public _pluginData: any = undefined; - private _physicsPlugin: IPhysicsEnginePluginV2; + private _physicsPlugin: IPhysicsEnginePlugin; /** * @@ -33,7 +33,7 @@ export class PhysicsBody { throw new Error("No Physics Plugin available."); } - this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin; this._physicsPlugin.initBody(this); } /** diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index b954e41c2ee..dc3e440d994 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -1,7 +1,7 @@ import type { Scene } from "../../scene"; import type { Vector3 } from "../../Maths/math.vector"; -import type { IPhysicsEnginePluginV2, ConstraintAxis, PhysicsConstraintParameters, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePluginV2"; -import { ConstraintType } from "./IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePlugin, ConstraintAxis, PhysicsConstraintParameters, ConstraintAxisLimitMode, ConstraintMotorType } from "./IPhysicsEnginePlugin"; +import { ConstraintType } from "./IPhysicsEnginePlugin"; import type { PhysicsBody } from "./physicsBody"; /** @@ -15,7 +15,7 @@ export class PhysicsConstraint { * */ public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePluginV2; + protected _physicsPlugin: IPhysicsEnginePlugin; /** * @@ -36,7 +36,7 @@ export class PhysicsConstraint { throw new Error("No Physics Plugin available."); } - this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin; this._physicsPlugin.initConstraint(this, type, options); } diff --git a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts b/packages/dev/core/src/Physics/v2/physicsEngine.ts similarity index 90% rename from packages/dev/core/src/Physics/v2/physicsEngineV2.ts rename to packages/dev/core/src/Physics/v2/physicsEngine.ts index fc04c4fd499..f58694e22f7 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineV2.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngine.ts @@ -1,8 +1,7 @@ import type { Nullable } from "../../types"; import { Vector3 } from "../../Maths/math.vector"; import type { IPhysicsEngine } from "../IPhysicsEngine"; -import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; - +import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; import { PhysicsRaycastResult } from "../physicsRaycastResult"; import { _WarnImport } from "../../Misc/devTools"; @@ -10,7 +9,7 @@ import { _WarnImport } from "../../Misc/devTools"; * Class used to control physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ -export class PhysicsEngineV2 implements IPhysicsEngine { +export class PhysicsEngine implements IPhysicsEngine { /** @internal */ /** * Global value used to control the smallest number supported by the simulation @@ -34,7 +33,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * Factory used to create the default physics plugin. * @returns The default physics plugin */ - public static DefaultPluginFactory(): IPhysicsEnginePluginV2 { + public static DefaultPluginFactory(): IPhysicsEnginePlugin { throw _WarnImport(""); } @@ -43,7 +42,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * @param gravity defines the gravity vector used by the simulation * @param _physicsPlugin defines the plugin to use (CannonJS by default) */ - constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePluginV2 = PhysicsEngineV2.DefaultPluginFactory()) { + constructor(gravity: Nullable, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) { gravity = gravity || new Vector3(0, -9.807, 0); this.setGravity(gravity); this.setTimeStep(); @@ -141,7 +140,7 @@ export class PhysicsEngineV2 implements IPhysicsEngine { * Gets the current plugin used to run the simulation * @returns current plugin */ - public getPhysicsPlugin(): IPhysicsEnginePluginV2 { + public getPhysicsPlugin(): IPhysicsEnginePlugin { return this._physicsPlugin; } diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponent.ts similarity index 100% rename from packages/dev/core/src/Physics/v2/physicsEngineComponentV2.ts rename to packages/dev/core/src/Physics/v2/physicsEngineComponent.ts diff --git a/packages/dev/core/src/Physics/v2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts index 97a12c0a910..4cee7eea3cc 100644 --- a/packages/dev/core/src/Physics/v2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -1,5 +1,5 @@ import type { Scene } from "../../scene"; -import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePluginV2"; +import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; /** * @@ -11,7 +11,7 @@ export class PhysicsMaterial { */ public _pluginData: any = undefined; - protected _physicsPlugin: IPhysicsEnginePluginV2; + protected _physicsPlugin: IPhysicsEnginePlugin; /** * @@ -32,7 +32,7 @@ export class PhysicsMaterial { throw new Error("No Physics Plugin available."); } - this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin; this._physicsPlugin.initMaterial(this); } /** diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index ecf61a34d6b..972dff4b4cc 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -1,7 +1,7 @@ import type { TransformNode } from "../../Meshes/transformNode"; import type { BoundingBox } from "../../Culling/boundingBox"; -import { ShapeType } from "./IPhysicsEnginePluginV2"; -import type { IPhysicsEnginePluginV2, PhysicsShapeParameters } from "./IPhysicsEnginePluginV2"; +import { ShapeType } from "./IPhysicsEnginePlugin"; +import type { IPhysicsEnginePlugin, PhysicsShapeParameters } from "./IPhysicsEnginePlugin"; import type { PhysicsMaterial } from "./physicsMaterial"; import type { Vector3 } from "../../Maths/math.vector"; import type { Quaternion } from "../../Maths/math.vector"; @@ -15,7 +15,7 @@ export class PhysicsShape { /** @internal */ public _pluginData: any = undefined; - private _physicsPlugin: IPhysicsEnginePluginV2; + private _physicsPlugin: IPhysicsEnginePlugin; private _type: ShapeType; @@ -44,7 +44,7 @@ export class PhysicsShape { throw new Error("No Physics Plugin available."); } - this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2; + this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin; this._physicsPlugin.initShape(this, type, options); } diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx index 9958b68dc9a..0b925242f00 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx @@ -9,8 +9,8 @@ import { StandardMaterial } from "core/Materials/standardMaterial"; import type { Mesh } from "core/Meshes/mesh"; import "core/Physics/physicsEngineComponent"; -import "core/Physics/v1/physicsEngineComponentV1"; -import "core/Physics/v1/physicsEngineComponentV2"; +import "core/Physics/v1/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponent"; export class DebugTabComponent extends PaneComponent { private _physicsViewersEnabled = false; diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx index 549b0280368..f64eece0eed 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/meshes/meshPropertyGridComponent.tsx @@ -38,8 +38,8 @@ import type { IInspectableOptions } from "core/Misc/iInspectable"; import { NormalMaterial } from "materials/normal/normalMaterial"; import "core/Physics/physicsEngineComponent"; -import "core/Physics/v1/physicsEngineComponentV1"; -import "core/Physics/v1/physicsEngineComponentV2"; +import "core/Physics/v1/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponent"; import { ParentPropertyGridComponent } from "../parentPropertyGridComponent"; import { Tools } from "core/Misc/tools"; diff --git a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx index dfd844a0023..b353be09a41 100644 --- a/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx +++ b/packages/dev/inspector/src/components/actionTabs/tabs/propertyGrids/scenePropertyGridComponent.tsx @@ -27,8 +27,8 @@ import { ButtonLineComponent } from "shared-ui-components/lines/buttonLineCompon import { AnimationGridComponent } from "./animations/animationPropertyGridComponent"; import "core/Physics/physicsEngineComponent"; -import "core/Physics/v1/physicsEngineComponentV1"; -import "core/Physics/v1/physicsEngineComponentV2"; +import "core/Physics/v1/physicsEngineComponent"; +import "core/Physics/v1/physicsEngineComponent"; interface IScenePropertyGridComponentProps { globalState: GlobalState; From 0e9707da7249090ad25563c0399dff3d801b81d2 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 6 Dec 2022 10:13:54 +0100 Subject: [PATCH 27/28] ES6 procies --- packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts | 2 ++ packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts | 2 ++ packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts | 2 ++ packages/dev/core/src/Physics/physicsEngine.ts | 2 ++ packages/dev/core/src/Physics/physicsHelper.ts | 2 ++ packages/dev/core/src/Physics/physicsImpostor.ts | 2 ++ packages/dev/core/src/Physics/physicsJoint.ts | 2 ++ 7 files changed, 14 insertions(+) create mode 100644 packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts create mode 100644 packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts create mode 100644 packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts create mode 100644 packages/dev/core/src/Physics/physicsEngine.ts create mode 100644 packages/dev/core/src/Physics/physicsHelper.ts create mode 100644 packages/dev/core/src/Physics/physicsImpostor.ts create mode 100644 packages/dev/core/src/Physics/physicsJoint.ts diff --git a/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts new file mode 100644 index 00000000000..1680c47937b --- /dev/null +++ b/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "../v1/Plugins/ammoJSPlugin"; diff --git a/packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts b/packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts new file mode 100644 index 00000000000..4fe9c6c8f6f --- /dev/null +++ b/packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "../v1/Plugins/cannonJSPlugin"; diff --git a/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts new file mode 100644 index 00000000000..b0dd415c1e4 --- /dev/null +++ b/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "../v1/Plugins/oimoJSPlugin"; diff --git a/packages/dev/core/src/Physics/physicsEngine.ts b/packages/dev/core/src/Physics/physicsEngine.ts new file mode 100644 index 00000000000..fb426bbc974 --- /dev/null +++ b/packages/dev/core/src/Physics/physicsEngine.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export { PhysicsEngine } from "./v1/physicsEngine"; diff --git a/packages/dev/core/src/Physics/physicsHelper.ts b/packages/dev/core/src/Physics/physicsHelper.ts new file mode 100644 index 00000000000..f930608428d --- /dev/null +++ b/packages/dev/core/src/Physics/physicsHelper.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "./v1/physicsHelper"; diff --git a/packages/dev/core/src/Physics/physicsImpostor.ts b/packages/dev/core/src/Physics/physicsImpostor.ts new file mode 100644 index 00000000000..a5e2b9c1f83 --- /dev/null +++ b/packages/dev/core/src/Physics/physicsImpostor.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "./v1/physicsImpostor"; diff --git a/packages/dev/core/src/Physics/physicsJoint.ts b/packages/dev/core/src/Physics/physicsJoint.ts new file mode 100644 index 00000000000..97026e901dd --- /dev/null +++ b/packages/dev/core/src/Physics/physicsJoint.ts @@ -0,0 +1,2 @@ +// ES 6 Compatibility +export * from "./v1/physicsJoint"; From 899e9d821dd9f259bf0ef009c72959e922a31d47 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 7 Dec 2022 09:37:29 +0100 Subject: [PATCH 28/28] internals --- packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts | 9 +++++++++ packages/dev/core/src/Physics/v2/physicsAggregate.ts | 1 + packages/dev/core/src/Physics/v2/physicsBody.ts | 1 + packages/dev/core/src/Physics/v2/physicsConstraint.ts | 6 ++++++ packages/dev/core/src/Physics/v2/physicsEngine.ts | 1 + .../dev/core/src/Physics/v2/physicsEngineComponent.ts | 3 +++ packages/dev/core/src/Physics/v2/physicsMaterial.ts | 1 + packages/dev/core/src/Physics/v2/physicsShape.ts | 8 ++++++++ 8 files changed, 30 insertions(+) diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index dd4d84d3ff1..ed9cf1aa8a9 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -8,6 +8,7 @@ import type { BoundingBox } from "../../Culling/boundingBox"; import type { TransformNode } from "../../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; +/** @internal */ export enum ConstraintAxisLimitMode { FREE, LIMITED, @@ -15,6 +16,7 @@ export enum ConstraintAxisLimitMode { NONE, } +/** @internal */ export enum ConstraintAxis { LINEAR_X, LINEAR_Y, @@ -24,6 +26,8 @@ export enum ConstraintAxis { ANGULAR_Z, LINEAR_DISTANCE, } + +/** @internal */ export enum ConstraintType { BALL_AND_SOCKET, DISTANCE, @@ -32,6 +36,7 @@ export enum ConstraintType { LOCK, } +/** @internal */ export enum ShapeType { SPHERE, CAPSULE, @@ -42,12 +47,14 @@ export enum ShapeType { MESH, } +/** @internal */ export enum ConstraintMotorType { NONE, VELOCITY, POSITION, } +/** @internal */ export interface PhysicsShapeParameters { center?: Vector3; radius?: number; @@ -58,6 +65,7 @@ export interface PhysicsShapeParameters { mesh?: AbstractMesh; } +/** @internal */ export interface PhysicsConstraintParameters { pivotA?: Vector3; pivotB?: Vector3; @@ -68,6 +76,7 @@ export interface PhysicsConstraintParameters { /** * */ +/** @internal */ export interface MassProperties { /** * diff --git a/packages/dev/core/src/Physics/v2/physicsAggregate.ts b/packages/dev/core/src/Physics/v2/physicsAggregate.ts index 43f206f9fa8..cbca6336f25 100644 --- a/packages/dev/core/src/Physics/v2/physicsAggregate.ts +++ b/packages/dev/core/src/Physics/v2/physicsAggregate.ts @@ -8,6 +8,7 @@ import type { TransformNode } from "../../Meshes/transformNode"; /** * The interface for the physics aggregate parameters */ +/** @internal */ export interface PhysicsAggregateParameters { /** @internal */ /** diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index 476f9c5d437..ab7daf93465 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -6,6 +6,7 @@ import type { Scene } from "../../scene"; /** * */ +/** @internal */ export class PhysicsBody { /** @internal */ public _pluginData: any = undefined; diff --git a/packages/dev/core/src/Physics/v2/physicsConstraint.ts b/packages/dev/core/src/Physics/v2/physicsConstraint.ts index dc3e440d994..2bd116efdc6 100644 --- a/packages/dev/core/src/Physics/v2/physicsConstraint.ts +++ b/packages/dev/core/src/Physics/v2/physicsConstraint.ts @@ -9,6 +9,7 @@ import type { PhysicsBody } from "./physicsBody"; * It holds a set of functions to control the underlying constraint * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ +/** @internal */ export class PhysicsConstraint { /** @internal */ /** @@ -239,6 +240,7 @@ export class PhysicsConstraint { /** * */ +/** @internal */ export class PhysicsConstraintBallAndSocket extends PhysicsConstraint { /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { @@ -249,6 +251,7 @@ export class PhysicsConstraintBallAndSocket extends PhysicsConstraint { /** * */ +/** @internal */ export class PhysicsConstraintDistance extends PhysicsConstraint { /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { @@ -259,6 +262,7 @@ export class PhysicsConstraintDistance extends PhysicsConstraint { /** * */ +/** @internal */ export class PhysicsConstraintHinge extends PhysicsConstraint { /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { @@ -269,6 +273,7 @@ export class PhysicsConstraintHinge extends PhysicsConstraint { /** * */ +/** @internal */ export class PhysicsConstraintSlider extends PhysicsConstraint { /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { @@ -279,6 +284,7 @@ export class PhysicsConstraintSlider extends PhysicsConstraint { /** * */ +/** @internal */ export class PhysicsConstraintLock extends PhysicsConstraint { /** @internal */ constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene) { diff --git a/packages/dev/core/src/Physics/v2/physicsEngine.ts b/packages/dev/core/src/Physics/v2/physicsEngine.ts index f58694e22f7..cf625dec093 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngine.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngine.ts @@ -9,6 +9,7 @@ import { _WarnImport } from "../../Misc/devTools"; * Class used to control physics engine * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ +/** @internal */ export class PhysicsEngine implements IPhysicsEngine { /** @internal */ /** diff --git a/packages/dev/core/src/Physics/v2/physicsEngineComponent.ts b/packages/dev/core/src/Physics/v2/physicsEngineComponent.ts index 628b72eade6..0b1719de930 100644 --- a/packages/dev/core/src/Physics/v2/physicsEngineComponent.ts +++ b/packages/dev/core/src/Physics/v2/physicsEngineComponent.ts @@ -9,6 +9,7 @@ declare module "../../Meshes/abstractMesh" { /** * */ + /** @internal */ export interface AbstractMesh { /** @internal */ _physicsBody: Nullable; @@ -67,6 +68,7 @@ Object.defineProperty(AbstractMesh.prototype, "physicsBody", { * Gets the current physics body * @returns a physics body or null */ +/** @internal */ AbstractMesh.prototype.getPhysicsBody = function (): Nullable { return this.physicsBody; }; @@ -78,6 +80,7 @@ AbstractMesh.prototype.getPhysicsBody = function (): Nullable { * @returns the current mesh * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine */ +/** @internal */ AbstractMesh.prototype.applyImpulse = function (force: Vector3, contactPoint: Vector3): AbstractMesh { if (!this.physicsBody) { throw new Error("No Physics Body for AbstractMesh"); diff --git a/packages/dev/core/src/Physics/v2/physicsMaterial.ts b/packages/dev/core/src/Physics/v2/physicsMaterial.ts index 4cee7eea3cc..e3da31d5ab3 100644 --- a/packages/dev/core/src/Physics/v2/physicsMaterial.ts +++ b/packages/dev/core/src/Physics/v2/physicsMaterial.ts @@ -4,6 +4,7 @@ import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin"; /** * */ +/** @internal */ export class PhysicsMaterial { /** @internal */ /** diff --git a/packages/dev/core/src/Physics/v2/physicsShape.ts b/packages/dev/core/src/Physics/v2/physicsShape.ts index 972dff4b4cc..6d6a311f682 100644 --- a/packages/dev/core/src/Physics/v2/physicsShape.ts +++ b/packages/dev/core/src/Physics/v2/physicsShape.ts @@ -11,6 +11,7 @@ import type { Scene } from "../../scene"; /** * */ +/** @internal */ export class PhysicsShape { /** @internal */ public _pluginData: any = undefined; @@ -145,6 +146,7 @@ export class PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeSphere extends PhysicsShape { /** @internal */ /** @@ -161,6 +163,7 @@ export class PhysicsShapeSphere extends PhysicsShape { /*** * */ +/** @internal */ export class PhysicsShapeCapsule extends PhysicsShape { /** @internal */ /** @@ -178,6 +181,7 @@ export class PhysicsShapeCapsule extends PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeCylinder extends PhysicsShape { /** @internal */ /** @@ -195,6 +199,7 @@ export class PhysicsShapeCylinder extends PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeShapeBox extends PhysicsShape { /** @internal */ /** @@ -212,6 +217,7 @@ export class PhysicsShapeShapeBox extends PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeShapeConvexHull extends PhysicsShape { /** @internal */ /** @@ -227,6 +233,7 @@ export class PhysicsShapeShapeConvexHull extends PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeShapeMesh extends PhysicsShape { /** @internal */ /** @@ -242,6 +249,7 @@ export class PhysicsShapeShapeMesh extends PhysicsShape { /** * */ +/** @internal */ export class PhysicsShapeShapeContainer extends PhysicsShape { /** @internal */ /**