From 0d28de4d5a4a380c26771f9d1e104f89761a3e92 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Thu, 15 Sep 2022 18:19:25 +0200 Subject: [PATCH] distance and normal in collision callback for ammojs --- .../core/src/Physics/Plugins/ammoJSPlugin.ts | 25 +++++++++++++++++-- .../core/src/Physics/Plugins/oimoJSPlugin.ts | 4 +-- .../dev/core/src/Physics/physicsImpostor.ts | 9 ++++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts b/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts index b623ee53705..bccac254c65 100644 --- a/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts +++ b/packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts @@ -60,6 +60,9 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin { private _tmpAmmoVectorRCB: any; private _raycastResult: PhysicsRaycastResult; private _tmpContactPoint = new Vector3(); + private _tmpContactNormal = new Vector3(); + private _tmpContactDistance: number; + private _tmpContactImpulse: number; private _tmpVec3 = new Vector3(); private static readonly _DISABLE_COLLISION_FLAG = 4; @@ -97,9 +100,15 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin { this._tmpAmmoConcreteContactResultCallback.addSingleResult = (contactPoint: any) => { contactPoint = this.bjsAMMO.wrapPointer(contactPoint, this.bjsAMMO.btManifoldPoint); const worldPoint = contactPoint.getPositionWorldOnA(); + const worldNormal = contactPoint.m_normalWorldOnB; this._tmpContactPoint.x = worldPoint.x(); this._tmpContactPoint.y = worldPoint.y(); this._tmpContactPoint.z = worldPoint.z(); + this._tmpContactNormal.x = worldNormal.x(); + this._tmpContactNormal.y = worldNormal.y(); + this._tmpContactNormal.z = worldNormal.z(); + this._tmpContactImpulse = contactPoint.getAppliedImpulse(); + this._tmpContactDistance = contactPoint.getDistance(); this._tmpContactCallbackResult = true; }; @@ -242,8 +251,20 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin { for (const otherImpostor of collideCallback.otherImpostors) { if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) { if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) { - mainImpostor.onCollide({ body: otherImpostor.physicsBody, point: this._tmpContactPoint }); - otherImpostor.onCollide({ body: mainImpostor.physicsBody, point: this._tmpContactPoint }); + mainImpostor.onCollide({ + body: otherImpostor.physicsBody, + point: this._tmpContactPoint, + distance: this._tmpContactDistance, + impulse: this._tmpContactImpulse, + normal: this._tmpContactNormal, + }); + otherImpostor.onCollide({ + body: mainImpostor.physicsBody, + point: this._tmpContactPoint, + distance: this._tmpContactDistance, + impulse: this._tmpContactImpulse, + normal: this._tmpContactNormal, + }); } } } diff --git a/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts b/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts index 72edbbc524a..a7edbfb70ad 100644 --- a/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts +++ b/packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts @@ -75,8 +75,8 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin { continue; } - mainImpostor.onCollide({ body: collidingImpostor.physicsBody, point: null }); - collidingImpostor.onCollide({ body: mainImpostor.physicsBody, point: null }); + mainImpostor.onCollide({ body: collidingImpostor.physicsBody, point: null, distance: 0, impulse: 0, normal: null }); + collidingImpostor.onCollide({ body: mainImpostor.physicsBody, point: null, distance: 0, impulse: 0, normal: null }); contact = contact.next; } } diff --git a/packages/dev/core/src/Physics/physicsImpostor.ts b/packages/dev/core/src/Physics/physicsImpostor.ts index 76be1274ca1..728016bf0a5 100644 --- a/packages/dev/core/src/Physics/physicsImpostor.ts +++ b/packages/dev/core/src/Physics/physicsImpostor.ts @@ -225,7 +225,7 @@ export class PhysicsImpostor { private _onAfterPhysicsStepCallbacks = new Array<(impostor: PhysicsImpostor) => void>(); /** @hidden */ public _onPhysicsCollideCallbacks: Array<{ - callback: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor, point: Nullable) => void; + callback: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor, point: Nullable, distance: number, impulse: number, normal: Nullable) => void; otherImpostors: Array; }> = []; @@ -913,8 +913,11 @@ export class PhysicsImpostor { * @param e * @param e.body * @param e.point + * @param e.distance + * @param e.impulse + * @param e.normal */ - public onCollide = (e: { body: any; point: Nullable }) => { + public onCollide = (e: { body: any; point: Nullable; distance: number; impulse: number; normal: Nullable }) => { if (!this._onPhysicsCollideCallbacks.length && !this.onCollideEvent) { return; } @@ -933,7 +936,7 @@ export class PhysicsImpostor { return obj.otherImpostors.indexOf(otherImpostor) !== -1; }) .forEach((obj) => { - obj.callback(this, otherImpostor, e.point); + obj.callback(this, otherImpostor, e.point, e.distance, e.impulse, e.normal); }); } };