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

contact point in registerOnPhysicsCollide callback #9086

Merged
merged 3 commits into from Oct 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Expand Up @@ -121,6 +121,7 @@

- Fixed time steps or delta time with sub time step for Oimo.js and Cannon.js ([cedricguillemet](https://github.com/cedricguillemet))
- Ammo.js collision group and mask supported by impostor parameters ([cedricguillemet](https://github.com/cedricguillemet))
- Contact point parameter in registerOnPhysicsCollide callback ([cedricguillemet](https://github.com/cedricguillemet))
- `collisionResponse` flag to disable response but still get onCollide events ([cedricguillemet](https://github.com/cedricguillemet))
- Ammo.js IDL exposed property update and raycast vehicle stablization support ([MackeyK24](https://github.com/MackeyK24))
- Recast.js plugin nav mesh and crowd agent to ref performance optimizations. ([MackeyK24](https://github.com/MackeyK24))
Expand Down
14 changes: 11 additions & 3 deletions src/Physics/Plugins/ammoJSPlugin.ts
Expand Up @@ -55,6 +55,7 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
private _tmpAmmoVectorRCA: any;
private _tmpAmmoVectorRCB: any;
private _raycastResult: PhysicsRaycastResult;
private _tmpContactPoint = new Vector3();

private static readonly DISABLE_COLLISION_FLAG = 4;
private static readonly KINEMATIC_FLAG = 2;
Expand Down Expand Up @@ -87,7 +88,14 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
this.world = new this.bjsAMMO.btSoftRigidDynamicsWorld(this._dispatcher, this._overlappingPairCache, this._solver, this._collisionConfiguration, this._softBodySolver);

this._tmpAmmoConcreteContactResultCallback = new this.bjsAMMO.ConcreteContactResultCallback();
this._tmpAmmoConcreteContactResultCallback.addSingleResult = () => { this._tmpContactCallbackResult = true; };
this._tmpAmmoConcreteContactResultCallback.addSingleResult = (contactPoint: any, colObj0Wrap: any, partId0: any, index0: any) => {
contactPoint = this.bjsAMMO.wrapPointer(contactPoint, Ammo.btManifoldPoint);
const worldPoint = contactPoint.getPositionWorldOnA();
this._tmpContactPoint.x = worldPoint.x();
this._tmpContactPoint.y = worldPoint.y();
this._tmpContactPoint.z = worldPoint.z();
this._tmpContactCallbackResult = true;
};

this._raycastResult = new PhysicsRaycastResult();

Expand Down Expand Up @@ -219,8 +227,8 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
for (var otherImpostor of collideCallback.otherImpostors) {
if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) {
if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) {
mainImpostor.onCollide({ body: otherImpostor.physicsBody });
otherImpostor.onCollide({ body: mainImpostor.physicsBody });
mainImpostor.onCollide({ body: otherImpostor.physicsBody, point: this._tmpContactPoint });
otherImpostor.onCollide({ body: mainImpostor.physicsBody, point: this._tmpContactPoint });
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Physics/Plugins/oimoJSPlugin.ts
Expand Up @@ -72,8 +72,8 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
continue;
}

mainImpostor.onCollide({ body: collidingImpostor.physicsBody });
collidingImpostor.onCollide({ body: mainImpostor.physicsBody });
mainImpostor.onCollide({ body: collidingImpostor.physicsBody, point: null});
collidingImpostor.onCollide({ body: mainImpostor.physicsBody, point: null });
contact = contact.next;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Physics/physicsImpostor.ts
Expand Up @@ -222,7 +222,7 @@ export class PhysicsImpostor {
private _onBeforePhysicsStepCallbacks = new Array<(impostor: PhysicsImpostor) => void>();
private _onAfterPhysicsStepCallbacks = new Array<(impostor: PhysicsImpostor) => void>();
/** @hidden */
public _onPhysicsCollideCallbacks: Array<{ callback: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor) => void; otherImpostors: Array<PhysicsImpostor> }> = [];
public _onPhysicsCollideCallbacks: Array<{ callback: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor, point: Nullable<Vector3>) => void; otherImpostors: Array<PhysicsImpostor> }> = [];

private _deltaPosition: Vector3 = Vector3.Zero();
private _deltaRotation: Quaternion;
Expand Down Expand Up @@ -885,7 +885,7 @@ export class PhysicsImpostor {
/**
* event and body object due to cannon's event-based architecture.
*/
public onCollide = (e: { body: any }) => {
public onCollide = (e: { body: any, point: Nullable<Vector3> }) => {
if (!this._onPhysicsCollideCallbacks.length && !this.onCollideEvent) {
return;
}
Expand All @@ -904,7 +904,7 @@ export class PhysicsImpostor {
return obj.otherImpostors.indexOf(<PhysicsImpostor>otherImpostor) !== -1;
})
.forEach((obj) => {
obj.callback(this, <PhysicsImpostor>otherImpostor);
obj.callback(this, <PhysicsImpostor>otherImpostor, e.point);
});
}
};
Expand Down