In the recipes so far, we've added animations and detection collisions to the scene manually. In this recipe, we'll show you how to use an external physics engine to add gravity, collision detection, and other physics effects to your scene.
For this recipe, we need to use a couple of external libraries. At the top of your HTML page, you have to add the following:
<script src="../libs/physi.js"></script>
This library contains the main implementation of the physics engine. This library in itself uses two additional libraries that need to be provided. You first need to make sure the ammo.js
library is stored in the same location as the physi.js
library, and at the beginning of your JavaScript code, you should add this:
Physijs.scripts.worker = "../libs/physijs_worker.js";
This points to a web worker (http://www.w3.org/TR/workers/) that handles the physics calculations in a separate thread. There is, of course, a ready-to-use example of this recipe...