Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhysicsPlugin refactor #13279

Merged
merged 28 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bd9e60c
PhysicsPlugin refactor
CedricGuillemetMS Nov 23, 2022
8bf793a
ctor
CedricGuillemetMS Nov 24, 2022
95f14c5
more work on the interfaces
CedricGuillemetMS Nov 24, 2022
8935336
format
CedricGuillemetMS Nov 24, 2022
986f376
fix build errors
CedricGuillemetMS Nov 25, 2022
94a3f31
more fixes
CedricGuillemetMS Nov 25, 2022
dd23470
yet another refactor
CedricGuillemetMS Nov 25, 2022
4018dc0
v1 / v2 folders
CedricGuillemetMS Nov 25, 2022
a654890
finishing rough edges of refactor
CedricGuillemetMS Nov 25, 2022
e1cb850
code format
CedricGuillemetMS Nov 25, 2022
d007878
moving helpers to v1
CedricGuillemetMS Nov 25, 2022
1c66795
dependencies
CedricGuillemetMS Nov 25, 2022
b15e752
linting
CedricGuillemetMS Nov 25, 2022
ef125c0
linting again
CedricGuillemetMS Nov 25, 2022
702cd96
import fixes
CedricGuillemetMS Nov 25, 2022
0ea0734
imports
CedricGuillemetMS Nov 28, 2022
5c04ce7
es6 import
CedricGuillemetMS Nov 28, 2022
b59fab0
engine components
CedricGuillemetMS Nov 28, 2022
adadc29
cleanup
CedricGuillemetMS Nov 28, 2022
1317b20
removed commented code
CedricGuillemetMS Nov 29, 2022
f9bd36c
linting...
CedricGuillemetMS Nov 29, 2022
4eea142
PR feedback
CedricGuillemetMS Nov 30, 2022
5bbabef
physicsviewer plugin version
CedricGuillemetMS Dec 1, 2022
1b2ea11
@internal for created classes
CedricGuillemetMS Dec 1, 2022
18f1dcf
pr feedback
CedricGuillemetMS Dec 1, 2022
303e675
naming and imports
CedricGuillemetMS Dec 2, 2022
0e9707d
ES6 procies
CedricGuillemetMS Dec 6, 2022
899e9d8
internals
CedricGuillemetMS Dec 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/dev/core/src/Debug/physicsViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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 { 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";
import type { ICreateCapsuleOptions } from "../Meshes/Builders/capsuleBuilder";
Expand All @@ -30,7 +31,7 @@ export class PhysicsViewer {
/** @internal */
protected _numMeshes = 0;
/** @internal */
protected _physicsEnginePlugin: Nullable<IPhysicsEnginePlugin>;
protected _physicsEnginePlugin: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null;
private _renderFunction: () => void;
private _utilityLayer: Nullable<UtilityLayerRenderer>;

Expand Down Expand Up @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 5 additions & 4 deletions packages/dev/core/src/Meshes/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { PhysicsEngine as PhysicsEngineV1 } from "../Physics/v1/physicsEngine";

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
Expand Down Expand Up @@ -662,8 +663,8 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
// Physics clone
if (scene.getPhysicsEngine) {
const physicsEngine = scene.getPhysicsEngine();
if (clonePhysicsImpostor && physicsEngine) {
const impostor = physicsEngine.getImpostorForPhysicsObject(source);
if (clonePhysicsImpostor && physicsEngine && physicsEngine.getPluginVersion() === 1) {
const impostor = (physicsEngine as PhysicsEngineV1).getImpostorForPhysicsObject(source);
if (impostor) {
this.physicsImpostor = impostor.clone(this);
}
Expand Down
126 changes: 8 additions & 118 deletions packages/dev/core/src/Physics/IPhysicsEngine.ts
Original file line number Diff line number Diff line change
@@ -1,71 +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 { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint";
import type { Vector3 } from "../Maths/math.vector";
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<PhysicsImpostor>): 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<Vector3>): void;
setAngularVelocity(impostor: PhysicsImpostor, velocity: Nullable<Vector3>): void;
getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3>;
getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3>;
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;
}
import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "./v1/IPhysicsEnginePlugin";
import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV2 } from "./v2/IPhysicsEnginePlugin";

/**
* Interface used to define a physics engine
Expand All @@ -77,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
Expand Down Expand Up @@ -123,61 +63,11 @@ 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(): IPhysicsEnginePlugin;

/**
* Gets the list of physic impostors
* @returns an array of PhysicsImpostor
*/
getImpostors(): Array<PhysicsImpostor>;

/**
* 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<PhysicsImpostor>;

/**
* 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<PhysicsImpostor>;
getPhysicsPlugin(): IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null;

/**
* Does a raycast in the physics world
Expand Down
Loading