Skip to content

Commit

Permalink
Merge pull request #13654 from CedricGuillemet/physicsIteration12
Browse files Browse the repository at this point in the history
collision observable
  • Loading branch information
CedricGuillemet committed Mar 22, 2023
2 parents 689afcc + e089a70 commit 84e3303
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
63 changes: 47 additions & 16 deletions packages/dev/core/src/Physics/v2/IPhysicsEnginePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,6 +58,36 @@ export enum ConstraintMotorType {
POSITION,
}

/**
* Collision object that is the parameter when notification for collision fires.
*/
export interface IPhysicsCollisionEvent {
/**
* 1st physics body that collided
*/
collider: PhysicsBody;
/**
* 2nd physics body that collided
*/
collidedAgainst: PhysicsBody;
/**
* World position where the collision occured
*/
point: Nullable<Vector3>;
/**
* Penetration distance
*/
distance: number;
/**
* Impulse value computed by the solver response
*/
impulse: number;
/**
* Collision world normal direction
*/
normal: Nullable<Vector3>;
}

/** @internal */
export interface PhysicsShapeParameters {
/**
Expand Down Expand Up @@ -178,24 +209,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<Vector3>;
distance: number;
impulse: number;
normal: Nullable<Vector3>;
}>;

setGravity(gravity: Vector3): void;
setTimeStep(timeStep: number): void;
getTimeStep(): number;
executeStep(delta: number, bodies: Array<PhysicsBody>): void; //not forgetting pre and post events
getPluginVersion(): number;
registerOnCollide(
func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void
): void;
unregisterOnCollide(
func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void
): void;

// body
initBody(body: PhysicsBody, motionType: PhysicsMotionType, position: Vector3, orientation: Quaternion): void;
Expand Down Expand Up @@ -229,16 +267,9 @@ 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<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void
): void;
unregisterOnBodyCollide(
body: PhysicsBody,
func: (collider: PhysicsBody, collidedAgainst: PhysicsBody, point: Nullable<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void
): void;
setCollisionCallbackEnabled(body: PhysicsBody, enabled: boolean): void;
addConstraint(body: PhysicsBody, childBody: PhysicsBody, constraint: PhysicsConstraint): void;
getCollisionObservable(body: PhysicsBody): Observable<IPhysicsCollisionEvent>;

// shape
initShape(shape: PhysicsShape, type: ShapeType, options: PhysicsShapeParameters): void;
Expand Down
29 changes: 7 additions & 22 deletions packages/dev/core/src/Physics/v2/physicsBody.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { 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";
Expand All @@ -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";

/**
Expand Down Expand Up @@ -368,31 +368,16 @@ 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<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => 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<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void
): void {
return this._physicsPlugin.unregisterOnBodyCollide(this, func);
public getCollisionObservable(): Observable<IPhysicsCollisionEvent> {
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);
Expand Down

0 comments on commit 84e3303

Please sign in to comment.