# How to get the force of a collision

**URL:** <https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892>\
**Category:** Questions\
**Created:** [April 11, 2021, 2:54pm UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892 "2021-04-11T14:54:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maziac](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/maziac/32/15495_2.png) [@maziac](https://forum.babylonjs.com/u/maziac)\
**Post date:** [April 11, 2021, 2:54pm UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/1 "2021-04-11T14:54:33Z")

</div>

Hi,

I’m using a physics engine (cannon) and I would like to generate a sound if 2 objects collide.

There I ran into 2 problems:

1. I would like get a notification if any collision happens. Either any collision of any object with any other object (PhysicsImposter). Or, any collision with one specific PhysicsImposter.  
I have found only OnCollideEvent but that is deprecated. The other collision events seem to require to register certain objects beforehand.

2. I would like to know with which force/impulse the 2 objects collided. I would like to use this to control the volume of the played sound.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 11, 2021, 8:58pm UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/2 "2021-04-11T20:58:11Z")

</div>

Welcome aboard!

Adding @Cedric

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 12, 2021, 8:16am UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/3 "2021-04-12T08:16:56Z")

</div>

Hi @maziac

There is also `onCollide` for cannon [PhysicsImpostor | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.physicsimpostor#oncollide)

Unfortunately, I didn’t find any way to get the inter penetration depth. I can add that support for ammojs but I didn’t find anything for cannon. I guess something exists.  
As a workaround, you can compute the distance between the position of impostor and the contact point. The shorter it is, the bigger the penetration.

@RaananW did you used penetration depth for your latest VR sound demo ?

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [April 12, 2021, 9:17am UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/4 "2021-04-12T09:17:20Z")

</div>

> [@maziac](#):
>
> I have found only OnCollideEvent but that is deprecated. The other collision events seem to require to register certain objects beforehand.

The reason it is deprecated is performance. It does work however, and you can use it without the fear of it being removed any time soon.

> [@maziac](#):
>
> I would like to know with which force/impulse the 2 objects collided. I would like to use this to control the volume of the played sound.

Babylon.js doesn’t provide the native cannon.js event in the onCollideEvent callback, but you can register to it yourself. Something like this:

> **[Babylon.js Playground](https://playground.babylonjs.com/#3B3135%2332)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Lines 37-40 would be the ones interesting to you.

> [@Cedric](#):
>
> did you used penetration depth for your latest VR sound demo ?

No, i didn’t. I register the collision for each generated sphere when it is created, against all existing colliders in the scene.

Demo will be released later today 🙂

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [April 12, 2021, 11:33am UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/5 "2021-04-12T11:33:57Z")

</div>

The way I’m doing it, is by taking the difference in velocity of the last frame and the current frame. I then use this to calculate the impact, when a physics body is colliding with something. It’s not exactly the most accurate way, but it seems to work for me.

---

<div class="post-metadata">

**Author:** ![maziac](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/maziac/32/15495_2.png) [@maziac](https://forum.babylonjs.com/u/maziac)\
**Post date:** [April 12, 2021, 4:28pm UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/6 "2021-04-12T16:28:14Z")

</div>

Thanks a lot for all the good answers.  
I have tried the addEventListener(“collide”, …) with getImpactVelocityAlongNormal().  
This works very well for me.

---

<div class="post-metadata">

**Author:** ![gabrieljbaker](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gabrieljbaker/32/12670_2.png) [@gabrieljbaker](https://forum.babylonjs.com/u/gabrieljbaker)\
**Post date:** [April 13, 2021, 5:18am UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/7 "2021-04-13T05:18:03Z")

</div>

@Cedric do you know if that ^ approach would work in ammo.js too?

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 13, 2021, 8:35am UTC](https://forum.babylonjs.com/t/how-to-get-the-force-of-a-collision/19892/8 "2021-04-13T08:35:58Z")

</div>

I think so, yes. I don’t see anything to would prevent it to work.
