I’m trying to get the point of collision between a ball and any meshes on the scene (AmmoJS). From the search, I found that I can use registerOnPhysicsCollide to register collisions between two meshes:
ball.impostor.registerOnPhysicsCollide(scene.getPhysicsEngine()._impostors, function(main, collided) {
// get the point of collision between `main` and `collided`
});
The question is how do I get the point on the ball where the collision happened?
Bonus question: Is it possible to register a collision event without providing impostors as an argument? I found this:
ball.impostor.onCollideEvent = function (self, other) {...}
@Cedric@RaananW OMG guys, I just asked a question yesterday evening and this morning already have the answer with the snippet , you are so good. Overall working with BabylonJS is a sheer pleasure. Thanks!
I recently needed to do something similar, and I found a potential defect with the registerOnPhysicsCollide and the onCollide callback - when the onCollide is overridden, the function registered in registerOnPhysicsCollid is never invoked. See this PG:
truckModel.physicsImpostor.onCollide = pointCollider;
let collOb = truckModel.physicsImpostor.registerOnPhysicsCollide(ground.physicsImpostor,
(c, a) => {
// BUG: this is never called when onCollide is set to a custom function
console.log(c, a);
});
You can accomplish this more simply using just registerOnPhysicsCollide, whose callback function is passed e.point as its third parameter.
Looks like the problem is that the type of the callback parameter to registerOnPhysicsCollide and unregisterOnPhysicsCollide doesn’t have the parameter point, which is why it doesn’t show up in the code completion or API documentation so isn’t widely know about… Here’s a simple PR to fix that issue.
Beautiful - thank you! I recall a similar thing with the Scene.onBeforeRenderObservable, where the method is passed the render index and other info that isn’t widely known either. Every bit helps!