Jim Wiberley
Jim Wiberley

Reputation: 41

Cannon JS Collision detect amount of force

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?

Upvotes: 4

Views: 2805

Answers (1)

schteppe
schteppe

Reputation: 2084

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

Upvotes: 6

Related Questions