schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

Explosion effect/behavior #236

Open GuillaumeNachury opened 8 years ago

GuillaumeNachury commented 8 years ago

Hi there, I'd like to create a kind of explosive barrel that may impact the surrounding objects. Can I get such behavior with cannon.js ?

Ty, G.

josephrocca commented 8 years ago

This should be pretty easy. I think you'll want to use body.applyImpulse to objects within the radius of the explosion of the barrel.

Apply a greater impulse to objects which are further away from the barrel (1/radius, maybe?).

Calculate the direction of the impulse with something like new CANNON.vec3.subVectors( otherBody.position, barrelBody.position). Not sure if the method is called subVectors - but just use the method for vector subtraction.

Here is the impulse demo: https://schteppe.github.io/cannon.js/demos/impulses.html https://github.com/schteppe/cannon.js/blob/master/demos/impulses.html

Good luck!