A lightweight JavaScript physics engine for both 2D and 3D, with built-in support for Three.js. Perfect for games, simulations, and interactive projects that need simple but robust physics without a heavy dependency.
| Property | Description |
|---|---|
| mass | Weight of the object |
| gravity | Gravity strength applied |
| velocity | Current movement speed |
| acceleration | Change in velocity |
| friction | Resistance to motion |
| drag | Air resistance |
| bounce | Elasticity on collision |
| rotation | Spin (2D or 3D) |
| angularVelocity | Rotational speed |
| isStatic | If true, object does not move |
| useGravity | Toggle gravity per object |
No package manager required. Just include the script:
<script src="PhysicsEngine.js"></script>
Or, if you’re using modules:
import { PhysicsEngine } from "./PhysicsEngine.js";
const engine = new PhysicsEngine({
dimension: "2D",
gravity: 9.8
});
const box = engine.createObject({
x: 100,
y: 0,
width: 50,
height: 50,
mass: 2,
useGravity: true
});
engine.update(deltaTime);
const engine = new PhysicsEngine({
dimension: "3D",
gravity: 9.81
});
const cube = new THREE.Mesh(
new THREE.BoxGeometry(1,1,1),
new THREE.MeshStandardMaterial()
);
scene.add(cube);
engine.attachThreeObject(cube, {
mass: 1,
useGravity: true,
bounce: 0.6
});
The engine automatically syncs:
Call this inside your animation loop:
function animate(time) {
requestAnimationFrame(animate);
const deltaTime = time - lastTime;
lastTime = time;
engine.update(deltaTime / 1000);
renderer.render(scene, camera);
}
object.applyForce({ x: 0, y: -10, z: 0 });
object.setVelocity({ x: 5, y: 0, z: 0 });
object.stop();
new PhysicsEngine({
dimension: "3D",
gravity: 9.81,
maxVelocity: 100,
globalFriction: 0.98
});
MIT (or specify your license here)
import { PhysicsEngine } from "./PhysicsEngine.js";
const engine = new PhysicsEngine({
dimension: "2D",
gravity: 9.8
});
const box = engine.createObject({
x: 100,
y: 0,
width: 50,
height: 50,
mass: 2,
useGravity: true
});
engine.update(deltaTime);
Three.js Usage (3D)
const engine = new PhysicsEngine({
dimension: "3D",
gravity: 9.81
});
const cube = new THREE.Mesh(
new THREE.BoxGeometry(1,1,1),
new THREE.MeshStandardMaterial()
);
scene.add(cube);
engine.attachThreeObject(cube, {
mass: 1,
useGravity: true,
bounce: 0.6
});
Position
Rotation
Velocity
Updating the Engine
function animate(time) {
requestAnimationFrame(animate);
const deltaTime = time - lastTime;
lastTime = time;
engine.update(deltaTime / 1000);
renderer.render(scene, camera);
}
object.applyForce({ x: 0, y: -10, z: 0 });
object.setVelocity({ x: 5, y: 0, z: 0 });
object.stop();
new PhysicsEngine({
dimension: "3D",
gravity: 9.81,
maxVelocity: 100,
globalFriction: 0.98
});