From 8450cbe02f26927e4b3d04932bf26ae67078f7a0 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 22 Mar 2023 11:12:13 +0100 Subject: [PATCH 1/3] collision observable --- .../src/Physics/v2/IPhysicsEnginePlugin.ts | 35 ++++++++++--------- .../dev/core/src/Physics/v2/physicsBody.ts | 34 +++++++----------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index 5bdda909f1a..5204eadbaa0 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 { TransformNode } from "../../Meshes/transformNode"; import type { PhysicsMaterial } from "./physicsMaterial"; import type { Mesh } from "../../Meshes/mesh"; import type { Nullable } from "core/types"; +import type { Observable } from "core/Misc/observable"; /** @internal */ export enum ConstraintAxisLimitMode { @@ -178,24 +179,31 @@ export enum PhysicsMotionType { /** @internal */ export interface IPhysicsEnginePluginV2 { /** - * + * Physics plugin world instance */ world: any; /** - * + * Physics plugin name */ name: string; + + /** + * Collision observable + */ + onCollisionObservable: Observable<{ + collider: PhysicsBody; + collidedAgainst: PhysicsBody; + point: Nullable; + distance: number; + impulse: number; + normal: Nullable; + }>; + setGravity(gravity: Vector3): void; setTimeStep(timeStep: number): void; getTimeStep(): number; executeStep(delta: number, bodies: Array): void; //not forgetting pre and post events getPluginVersion(): number; - registerOnCollide( - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void; - unregisterOnCollide( - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void; // body initBody(body: PhysicsBody, motionType: PhysicsMotionType, position: Vector3, orientation: Quaternion): void; @@ -229,16 +237,11 @@ export interface IPhysicsEnginePluginV2 { getAngularVelocityToRef(body: PhysicsBody, angVel: Vector3): void; getBodyGeometry(body: PhysicsBody): {}; disposeBody(body: PhysicsBody): void; - registerOnBodyCollide( - body: PhysicsBody, - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void; - unregisterOnBodyCollide( - body: PhysicsBody, - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void; setCollisionCallbackEnabled(body: PhysicsBody, enabled: boolean): void; addConstraint(body: PhysicsBody, childBody: PhysicsBody, constraint: PhysicsConstraint): void; + getCollisionObservable( + body: PhysicsBody + ): Observable<{ collider: PhysicsBody; collidedAgainst: PhysicsBody; point: Nullable; distance: number; impulse: number; normal: Nullable }>; // shape initShape(shape: PhysicsShape, type: ShapeType, options: PhysicsShapeParameters): void; diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index 9c15b2904bb..bb6399d6ae1 100644 --- a/packages/dev/core/src/Physics/v2/physicsBody.ts +++ b/packages/dev/core/src/Physics/v2/physicsBody.ts @@ -8,7 +8,7 @@ import type { Nullable } from "core/types"; import type { PhysicsConstraint } from "./physicsConstraint"; import type { Bone } from "core/Bones/bone"; import { Space } from "core/Maths/math.axis"; -import type { Observer } from "../../Misc/observable"; +import type { Observable, Observer } from "../../Misc/observable"; import type { Node } from "../../node"; /** @@ -368,31 +368,23 @@ export class PhysicsBody { } /** - * Register a collision callback that is called when the body collides - * Filtering by body is inefficient. It's more preferable to register a collision callback for the entire world - * and do the filtering on the user side. + * Returns an observable that will be notified for all collisions happening for event-enabled bodies + * @returns Observable */ - public registerOnCollide( - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void { - return this._physicsPlugin.registerOnBodyCollide(this, func); - } - - /** - * Unregister a collision callback that is called when the body collides - */ - public unregisterOnCollide( - func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable, distance: number, impulse: number, normal: Nullable) => void - ): void { - return this._physicsPlugin.unregisterOnBodyCollide(this, func); + public getCollisionObservable(): Observable<{ + collider: PhysicsBody; + collidedAgainst: PhysicsBody; + point: Nullable; + distance: number; + impulse: number; + normal: Nullable; + }> { + return this._physicsPlugin.getCollisionObservable(this); } /** * Enable or disable collision callback for this PhysicsBody. - * `registerOnCollide` method will enable collision callback and `unregisterOnCollide` will disable them. - * Registering a collision callback on the plugin and enabling collision per body is faster than - * registering callback per PhysicsBody. - * @param enabled true if PhysicsBody's collision will rise a collision event and call the callback + * @param enabled true if PhysicsBody's collision will rise a collision event and notifies the observable */ public setCollisionCallbackEnabled(enabled: boolean): void { return this._physicsPlugin.setCollisionCallbackEnabled(this, enabled); From 8b211fff0fa28eb9ed2d57ef514b12d53f0b01b3 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 22 Mar 2023 13:37:30 +0100 Subject: [PATCH 2/3] collision event interface --- .../src/Physics/v2/IPhysicsEnginePlugin.ts | 34 +++++++++++++++++-- .../dev/core/src/Physics/v2/physicsBody.ts | 11 ++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index 5204eadbaa0..271ea0d4d1f 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -58,6 +58,36 @@ export enum ConstraintMotorType { POSITION, } +/** + * Collision object that is the parameter when notification for collision fires. + */ +export interface CollisionEvent { + /** + * 1st physics body that collided + */ + collider: PhysicsBody; + /** + * 2nd physics body that collided + */ + collidedAgainst: PhysicsBody; + /** + * World position where the collision occured + */ + point: Nullable; + /** + * Penetration distance + */ + distance: number; + /** + * Impulse value computed by the solver response + */ + impulse: number; + /** + * Collision world normal direction + */ + normal: Nullable; +} + /** @internal */ export interface PhysicsShapeParameters { /** @@ -239,9 +269,7 @@ export interface IPhysicsEnginePluginV2 { disposeBody(body: PhysicsBody): void; setCollisionCallbackEnabled(body: PhysicsBody, enabled: boolean): void; addConstraint(body: PhysicsBody, childBody: PhysicsBody, constraint: PhysicsConstraint): void; - getCollisionObservable( - body: PhysicsBody - ): Observable<{ collider: PhysicsBody; collidedAgainst: PhysicsBody; point: Nullable; distance: number; impulse: number; normal: Nullable }>; + getCollisionObservable(body: PhysicsBody): Observable; // shape initShape(shape: PhysicsShape, type: ShapeType, options: PhysicsShapeParameters): void; diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index bb6399d6ae1..b1a1e7f7d8e 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, PhysicsMotionType } from "./IPhysicsEnginePlugin"; +import type { CollisionEvent, IPhysicsEnginePluginV2, MassProperties, PhysicsMotionType } from "./IPhysicsEnginePlugin"; import type { PhysicsShape } from "./physicsShape"; import { Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; @@ -371,14 +371,7 @@ export class PhysicsBody { * Returns an observable that will be notified for all collisions happening for event-enabled bodies * @returns Observable */ - public getCollisionObservable(): Observable<{ - collider: PhysicsBody; - collidedAgainst: PhysicsBody; - point: Nullable; - distance: number; - impulse: number; - normal: Nullable; - }> { + public getCollisionObservable(): Observable { return this._physicsPlugin.getCollisionObservable(this); } From e089a7051b596058e9dc52446ddd184808f1e106 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 22 Mar 2023 16:13:55 +0100 Subject: [PATCH 3/3] naming --- packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts | 4 ++-- packages/dev/core/src/Physics/v2/physicsBody.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts index 271ea0d4d1f..39c4cdb6012 100644 --- a/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts +++ b/packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts @@ -61,7 +61,7 @@ export enum ConstraintMotorType { /** * Collision object that is the parameter when notification for collision fires. */ -export interface CollisionEvent { +export interface IPhysicsCollisionEvent { /** * 1st physics body that collided */ @@ -269,7 +269,7 @@ export interface IPhysicsEnginePluginV2 { disposeBody(body: PhysicsBody): void; setCollisionCallbackEnabled(body: PhysicsBody, enabled: boolean): void; addConstraint(body: PhysicsBody, childBody: PhysicsBody, constraint: PhysicsConstraint): void; - getCollisionObservable(body: PhysicsBody): Observable; + getCollisionObservable(body: PhysicsBody): Observable; // shape initShape(shape: PhysicsShape, type: ShapeType, options: PhysicsShapeParameters): void; diff --git a/packages/dev/core/src/Physics/v2/physicsBody.ts b/packages/dev/core/src/Physics/v2/physicsBody.ts index b1a1e7f7d8e..5b923f56e99 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 { CollisionEvent, IPhysicsEnginePluginV2, MassProperties, PhysicsMotionType } from "./IPhysicsEnginePlugin"; +import type { IPhysicsCollisionEvent, IPhysicsEnginePluginV2, MassProperties, PhysicsMotionType } from "./IPhysicsEnginePlugin"; import type { PhysicsShape } from "./physicsShape"; import { Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector"; import type { Scene } from "../../scene"; @@ -371,7 +371,7 @@ export class PhysicsBody { * Returns an observable that will be notified for all collisions happening for event-enabled bodies * @returns Observable */ - public getCollisionObservable(): Observable { + public getCollisionObservable(): Observable { return this._physicsPlugin.getCollisionObservable(this); }