Graphics Reference
In-Depth Information
Adding a physics engine
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.
Getting ready
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 it-
self 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 that you can use as a reference or to experiment with. The example for this re-
cipe is called 07.09-add-a-physics-engine.html , and when this is opened in
the browser you will see something similar to what is shown in the following screen-
shot:
Search WWH ::




Custom Search