phy6-js
An experimental physics engine for JavaScript written in ES6.
NOTE: This is highly experimental, it's nowhere near ready. It's more of a toy, and if you use it you may cause mass extinction of pandas and who knows what else, so be careful.
NOTE 2: phy6-js
is highly inspired by Matter.js.
Matter.js
is a true full-fledged physical engine: use Matter.js
if you need to make
something serious.
How to install
The most simple way to install phy6-js
is to use npm
:
npm install phy6-js
And then you can use it like any other module:
Examples
A box falling down on a bar:
; // `rect` is an helper function that constructs a `Body` with a rectangular shape.// If you want, you can construct bodies directly using the `Body` constructor.const bigOne = BodyFactory;const bar = BodyFactory; // Create the engine passing an array of bodiesconst engine = bigOne bar ; // Create a Renderer and connect it to the engine and to a canvas.// Whenever the engine updates it's status, it will be automatically redrawn.engine document0start; // Start the timer that at every tick will update the engine.// By default the timer ticks 60 times per second.const timer = ;timer;timerstart;
A stack of boxes:
// `stack` is an helper function that allows constructing stack of object// in a fast and easy way. Here we are building a stack of 5x4 objects,// located at (100, 310), and each object is a rectangle 30x30.// Note that `stack` is an array.const stack = BodyFactory; const engine = stack; ...
Some common properties on bodies:
const body = // Array of `Vector`s representing the vertices of the body. // Note that the shape must be convex. vertices: 0 0 100 0 100 50 // Density of the material of the body. // This value is used to compute mass from the area (which is in turn // computed from the shape expressed by the vertices) density: 0005 // Forces applied to the body *in this frame*. // The forces (torque included) are cleared at every timestep to allow // dynamic forces to be recomputed every time the state of the simulation changes. // The correct way to apply changing forces (like gravitational attraction between bodies) // is to listen to the `preUpdate` event on the engine and recompute the forces // in the event handler. force: 03 16 torque: 046 // A static body is a body that will never change its position. // Other bodies can bounce off this one, but a static body will never move. isStatic: false // Inclination of the body (in radians) angle: MathPI / 4 // Coefficient of restitution used during collision resolution. // It's like the "bouncyness" of the body: `0` means that will never bounce // and `1` is a perfectly elastic collision. (Note that to have a perfectly // elastic collision you should also set to `0` the friction.) restitution: 099 // Friction for sliding bodies and for bodies flying in the air. // These values are also between `0` and `1`. friction: 01 frictionAir: 001 ;
There are other properties that are automatically computed (like mass
, area
,
inertia
and bounds
) and they can be both read and written, even though it will
rarely be necessary.