Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 213
Jim Wiberley Avatar asked Nov 14 '25 22:11

Jim Wiberley


1 Answers

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
    }
});
like image 128
schteppe Avatar answered Nov 17 '25 20:11

schteppe