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

distance and normal in collision callback for ammojs #12986

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 23 additions & 2 deletions packages/dev/core/src/Physics/Plugins/ammoJSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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,
});
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Physics/Plugins/oimoJSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/dev/core/src/Physics/physicsImpostor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector3>) => void;
callback: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor, point: Nullable<Vector3>, distance: number, impulse: number, normal: Nullable<Vector3>) => void;
otherImpostors: Array<PhysicsImpostor>;
}> = [];

Expand Down Expand Up @@ -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<Vector3> }) => {
public onCollide = (e: { body: any; point: Nullable<Vector3>; distance: number; impulse: number; normal: Nullable<Vector3> }) => {
if (!this._onPhysicsCollideCallbacks.length && !this.onCollideEvent) {
return;
}
Expand All @@ -933,7 +936,7 @@ export class PhysicsImpostor {
return obj.otherImpostors.indexOf(<PhysicsImpostor>otherImpostor) !== -1;
})
.forEach((obj) => {
obj.callback(this, <PhysicsImpostor>otherImpostor, e.point);
obj.callback(this, <PhysicsImpostor>otherImpostor, e.point, e.distance, e.impulse, e.normal);
});
}
};
Expand Down