# Physics collision callbacks using ammo

**URL:** <https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853>\
**Category:** Questions\
**Created:** [December 22, 2020, 8:46am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853 "2020-12-22T08:46:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/shahin/32/12464_2.png) [@Shahin](https://forum.babylonjs.com/u/Shahin)\
**Post date:** [December 22, 2020, 8:46am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/1 "2020-12-22T08:46:46Z")

</div>

From the documentation it seems like we need to get physics collision callbacks as per this demo: [Babylon.js Playground](https://playground.babylonjs.com/#1NASOD)  
Is there a better and more general way to get callbacks?  
Ideally I would like to register a callback that fires regardless of what bodies collide (I don’t want to provide a list of _ALL_ bodies in the world every time I register one). I would also need to get more information from the collision, for example the incoming and outgoing velocities of both bodies so I can determine the energy loss.  
Thanks.

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [December 22, 2020, 9:09am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/2 "2020-12-22T09:09:21Z")

</div>

Hi Shahin, I think this PG can help: [https://playground.babylonjs.com/#1NASOD#127](https://playground.babylonjs.com/#1NASOD#127)

```auto
sphere.physicsImpostor.onCollideEvent = (self, other) => {
    console.log(`onCollideEvent: self velocity: ${self.getLinearVelocity()}, other velocity: ${other.getLinearVelocity()}`);
};

```

`self` and `other` can give useful information for both bodies

---

<div class="post-metadata">

**Author:** ![Shahin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/shahin/32/12464_2.png) [@Shahin](https://forum.babylonjs.com/u/Shahin)\
**Post date:** [December 22, 2020, 10:40am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/3 "2020-12-22T10:40:43Z")

</div>

Thanks. I did see that function but it said it was for legacy code:

> **[PhysicsImpostor | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.physicsimpostor#oncollideevent)**
>
> Represents a physics imposter \[API\]

Note that I’m using ammo, not cannon, and it seems like I actually need to call registerOnPhysicsCollide() otherwise onCollideEvent() never gets called because the physicsImpostor.\_onPhysicsCollideCallbacks needs to have a length \> 0 otherwise we get no callbacks at all (for ammo only).  
So now that I have a workaround, the problem remaining is that I need both the pre solve and post solve collision velocities of the bodies so I can determine the energy lost. It seems like the velocities I get in at the point of callback are presolve… any good way to get the velocities after collision?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 22, 2020, 1:04pm UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/4 "2020-12-22T13:04:12Z")

</div>

Adding @Cedric who will be able to provide guidance after the break 🙂

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [December 22, 2020, 6:18pm UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/5 "2020-12-22T18:18:09Z")

</div>

I’m not sure how well this would work, but maybe we could use `onBeforeStepObservable` and `onAfterStepObservable` in conjunction with `registerOnPhysicsCollide` to get before and after velocities

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 22, 2020, 7:03pm UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/6 "2020-12-22T19:03:47Z")

</div>

Yup this is a great idea 🙂 @Shahin you might be able to rely on both observables `onBeforeStepObservable` and `onAfterStepObservable` availabled on the scene to do this.

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [December 23, 2020, 1:09am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/7 "2020-12-23T01:09:08Z")

</div>

My mistake, I think `onBeforeStepObservable` and `onAfterStepObservable` is only available when using Deterministic Lockstep ([Advanced Animation Methods | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/animation/advanced_animations#deterministic-lockstep))

I think `onBeforePhysicsObservable` and `onAfterPhysicsObservable` should work if not using Deterministic Lockstep

---

<div class="post-metadata">

**Author:** ![Shahin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/shahin/32/12464_2.png) [@Shahin](https://forum.babylonjs.com/u/Shahin)\
**Post date:** [December 29, 2020, 8:58am UTC](https://forum.babylonjs.com/t/physics-collision-callbacks-using-ammo/16853/8 "2020-12-29T08:58:56Z")

</div>

I ended up using

- physicsImpostor.registerOnPhysicsCollide: to get pre collision velocity and mark the body
- physicsImpostor.registerBeforePhysicsStep: to get post collision velocity of marked body
- physicsImpostor.registerAfterPhysicsStep: not really needed

I couldn’t get physicsImpostor.onCollideEvent() to work in ammo.js because this code ([Babylon.js/ammoJSPlugin.ts at 38bc17b050db8f854fc27069ecea552114cb21f2 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/blob/38bc17b050db8f854fc27069ecea552114cb21f2/src/Physics/Plugins/ammoJSPlugin.ts#L226)) filters out any bodies that haven’t been passed to registerOnPhysicsCollide. It isn’t ideal, but it works.
