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

Get Three.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.