Matter.js
Introductory Matter.js notes
Updated: 03 September 2023
Some notes on using Matter.js based on The Coding Train YouTube Videos
Physics Engine Overview
At a high level to move things around we make use of basic physics concepts using vector math, collisions, etc. In order to do this we can make use of pre-implemented things like a physics engine in order to make things move around and interact
In general when using a physics engine we have some of the following steps:
- Setup allows us to define the inital state of the world
- We then tell the physics engine to start
- The state we defined will iterate over a render loop updating the different properties of the objects in our world
Physics Engines usually have some common concepts:
- The World which is a defined space with specific rules
- A Body which is an entity in that world
- A Connection which states how different bodies are related to one another
Matter.js
Matter.js is a 2D rigd body physics engine that runs in the browser. The general Getting Started Buide can be found on GitHub
Matter.js has a built-in render and runner for simulations that does very basic rendering for the purpose of debugging. For our purpose we’ll use p5.js for rendering
Setup the Application
To get started with the Library we should initilize a new package and add the matter.js
library to it
Thereafter we need to create a few files for our code:
index.html
which will just reference the relevant filesindex.js
which is where we will implement our javascript
In our index.html
file we’ll include the following to reference matter-js
and our own index.js
file:
index.html
Within our page context the Matter
variable should allow us to access the functionality of the library
Basic Setup
As a basic setup to our page we can then create an Engine and Renderer instance using Matter
in a setup function as well as define the different bodies and add them to the World
index.js
You can also see the call to
createCanvas
which is what processing will use to create the canvas for the site
Rendering
Now that the engine is running, we can start looking at how to render the specific objects as they move through our canvas
Each of the respective bodies we create have some basic properties that we can use to render the body. In the case of Matter.Bodies
we have a vertices
property. We can use p5
to render the vertices as follows:
We can then implement this in a draw
function which p5
will call and pass it the relevant shapes we want to be drawn like so:
index.js
Taking into consideration the render
properties of the body which we can assign from the Matter.Bodies
instances, we can see that each body has render.fillStyle
, render.strokeStyle
, render.opacity
, and a render.visible
property
We can update our render function to make use of these properties like so:
Next, we can simply always render all the bodies in our world like so:
So using all of the above, our draw
function becomes:
While all the above technically works, I think it may just make sense to write a canvas renderer which should work more or less the same. So hopefully I’ll do that doo