Point of collision

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) {...}

but looks like it doesn’t work with AmmoJS.

Thanks!

1 Like

pinging @Cedric

1 Like

Hi @Artod_ds and welcome to the forum!

You have to register a callback with registerOnPhysicsCollide, then you can override onCollide and get the contact point from there.

collision contact point | Babylon.js Playground (babylonjs.com)

3 Likes

@Cedric @RaananW OMG guys, I just asked a question yesterday evening and this morning already have the answer with the snippet :smile: , you are so good. Overall working with BabylonJS is a sheer pleasure. Thanks!

5 Likes

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);
            });
1 Like

Yes, I noticed the same behavior. But in my case, I don’t need the callback inside registerOnPhysicsCollide.

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. :slight_smile:

1 Like

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!

1 Like