PhysicsEngine

PhysicsEngine.js

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.


Features


Supported Physics Properties

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

Installation

No package manager required. Just include the script:

<script src="PhysicsEngine.js"></script>

Or, if you’re using modules:

import { PhysicsEngine } from "./PhysicsEngine.js";

Basic Usage (2D)

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
});

The engine automatically syncs:


Updating the Engine

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 API

object.applyForce({ x: 0, y: -10, z: 0 });
object.setVelocity({ x: 5, y: 0, z: 0 });
object.stop();

Configuration Options

new PhysicsEngine({
  dimension: "3D",
  gravity: 9.81,
  maxVelocity: 100,
  globalFriction: 0.98
});

License

MIT (or specify your license here)

If you’re using modules:

import { PhysicsEngine } from "./PhysicsEngine.js";

Basic Usage (2D)

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
});

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 API

object.applyForce({ x: 0, y: -10, z: 0 });
object.setVelocity({ x: 5, y: 0, z: 0 });
object.stop();

Configuration Options

new PhysicsEngine({
  dimension: "3D",
  gravity: 9.81,
  maxVelocity: 100,
  globalFriction: 0.98
});