Book Image

Three.js Cookbook

By : Jos Dirksen
Book Image

Three.js Cookbook

By: Jos Dirksen

Overview of this book

Table of Contents (15 chapters)
Three.js Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 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...