4

I have two Cannon.js Objects, and have attached the "collide" event listener to both.

carBody.addEventListener("collide",function(e){
});

I want to be able to react differently depending on how much force the collision has is there a way to do this?

Jim Wiberley
  • 41
  • 1
  • 3
  • What have you tried so far? We are not a code writing service. Please refer to [How to ask a good question?](https://stackoverflow.com/help/how-to-ask). – GrumpyCrouton Jun 15 '17 at 19:10

1 Answers1

5

You can get the relative velocity in the contact point to determine the amount of energy in the collision. Example:

carBody.addEventListener("collide",function(e){
    var relativeVelocity = e.contact.getImpactVelocityAlongNormal();
    if(Math.abs(relativeVelocity) > 10){
        // More energy
    } else {
        // Less energy
    }
});
schteppe
  • 1,984
  • 13
  • 15