Tip
This course was updated in 2024. For the newer content, please visit Physics with Rapier
Video Lecture
Description
Animation can also be achieved using a Physics library. We can use a library called Cannon.js. But, rather than using the original Cannon.js, which is no longer maintained, we can install a newer fork of it named Cannon-es instead.
The Cannon physics library is ideal for simulating rigid bodies. You don't have to use it with Three.js, but it was originally built to be used with Three.js, so it will be quite easy to begin using in your project.
We will use it to make objects move and interact in a more realistic way and provide collision detection possibilities.
Basic Concepts
- Shape : A geometrical shape, such as a sphere, cube or plane, used for the the physics calculations.
- Rigid Body : A rigid body has a shape and a number of other properties used in the calculations such as mass and inertia.
- Constraint : A 3D body has 6 degrees of freedom, 3 for position and three to describe the rotation vector. A constraint is a limit on one of the degrees of freedom.
- Contact constraint : A type of constraint to simulate friction and restitution. These are like the faces of an object where the constraint is applied.
- World : A collection of bodies and constraints that interact together.
- Solver : The algorithm that is passed over the bodies and constraints to calculate there physical properties and adjust them accordingly.
Collision Detection
Collision detection algorithms determine what pairs of objects may be colliding. Collision detection is a computationally expensive process, so various methods can be used to simplify the collision detection.
- Narrowphase : Outright body vs body collision detection. This is the most computationally expensive.
- Broadphase : Is a compromise on Narrowphase where various other techniques can be used to improve collision detection performance.
Cannon provides several options for broadphase detection.
Phase | Description |
---|---|
NaiveBroadphase | Default. The NaiveBroadphase looks at all possible pairs without restriction, therefore it has complexity N^2. It is similar to the Narrowphase technique, except it decides first whether objects are close enough before checking if there bodies touch. NaiveBroadphase is the default and is suitable for the most common use cases, but becomes less performant if there are many objects in the physics world. |
SAPBroadphase | The Sweep and Prune algorithm sorts bodies along an axis and then moves down that list finding pairs by looking at body size and position of the next bodies. For best performance, choose an axis that the bodies are spread out more on. Set 0 for X axis, 1 for Y axis and 2 for Z axis. Default axisIndex is 0 (X axis). ; E.g.,// TypeScript world.broadphase = new CANNON.SAPBroadphase(world) ;(world.broadphase as CANNON.SAPBroadphase).axisIndex = 2 |
GridBroadphase | Axis aligned uniform grid broadphase. Divides space into a grid of cells. Bodies are placed into the cells they overlap and bodies in the same cell are paired. GridBroadphase needs to know the size of the space ahead of time. Set number of cells when you create the object. Default number of cells is X = 10, Y = 10, Z = 10. |
Iterations
The Solver algorithms decide what force to add to bodies in contact. The solver is iterative, which means that it solves the equations incrementally on each animation pass. It will get closer to the solution for each iteration during the loop. A number too low for the solver iterations will result in increasingly inaccurate contact forces, which can appear as jittering or vibrations on the object, and a higher number will increase precision and stability, but also compromise performance.
The default solver iterations is 10.
//JavaScript (Cannon.js version)world.solver.iterations = 10//TypeScript (Cannon-es version);(world.solver as CANNON.GSSolver).iterations = 10
Supported Cannon.js Shape Collisions
. | Box | Sphere | Cylinder | Plane | Convex | Trimesh |
---|---|---|---|---|---|---|
Box | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
Sphere | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Cylinder | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
Plane | ✔ | ✔ | ✔ | ❌ | ✔ | ✔ |
Convex | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
Trimesh | ❌ | ✔ | ❌ | ✔ | ❌ | ❌ |
Setup
Install Cannon-es
npm install cannon-es --save-dev
Start Scripts
./src/client/client.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
|
Final Scripts
./src/client/client.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
|
Troubleshooting
geometry.attributes.position.array error : Property 'array' does not exist on type 'BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute'
Since the addition of the GLBufferAttribute
as a new type of BufferGeometry.attributes
, it does not contain the array
property. So, we now need to cast the attribute as THREE.BufferAttribute
when we want to use a property or method of a BufferAttribute
that isn't included in the GLBufferAttribute
type.
E.g.,
@three/types@0.148 and earlier
const position = geometry.attributes.position.arrayconst normals = geometry.attributes.normal.arrayconst uvs = geometry.attributes.uv.array
@three/types@0.149 and later
const position = (geometry.attributes.position as THREE.BufferAttribute).arrayconst normals = (geometry.attributes.normal as THREE.BufferAttribute).arrayconst uvs = (geometry.attributes.uv as THREE.BufferAttribute).array