Skip to content

PhysicsEngine — a learning-friendly, hackable physics layer.

Notifications You must be signed in to change notification settings

houselearning/PhysicsEngine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • ✅ 2D & 3D physics support
  • ✅ Three.js integration
  • ✅ Simple gravity system
  • ✅ Collision-ready object model
  • ✅ Extendable physics properties
  • ✅ No external physics libraries required

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:

  • Position
  • Rotation
  • Velocity

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:

  • Position

  • Rotation

  • Velocity

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

About

PhysicsEngine — a learning-friendly, hackable physics layer.

Topics

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published