Convert Figma logo to code with AI

wellcaffeinated logoPhysicsJS

A modular, extendable, and easy-to-use physics engine for javascript

3,599
406
3,599
59

Top Related Projects

A lightweight 3D physics engine written in JavaScript.

16,610

a 2D rigid body physics engine for the web ▲● ■

👋💣 physics based hooks for @react-three/fiber

4,134

Direct port of the Bullet physics engine to JavaScript using Emscripten

3,047

Lightweight 3d physics engine for javascript

2,763

Physics plugin for Three.js

Quick Overview

PhysicsJS is a modular, extendable, and easy-to-use physics engine for JavaScript. It provides a robust foundation for creating physics-based simulations and games in web browsers. The library offers a wide range of features, including various body types, behaviors, and renderers.

Pros

  • Modular architecture allows for easy customization and extension
  • Supports multiple rendering options (Canvas, SVG, and WebGL)
  • Comprehensive documentation and examples
  • Active community and ongoing development

Cons

  • Performance may be limited for complex simulations with many objects
  • Learning curve can be steep for beginners
  • Limited built-in constraints compared to some other physics engines
  • May require additional libraries for more advanced features

Code Examples

Creating a simple world with a circle:

var world = Physics();

world.add(
    Physics.body('circle', {
        x: 50,
        y: 30,
        radius: 20
    })
);

Physics.util.ticker.on(function(time) {
    world.step(time);
});

Physics.util.ticker.start();

Adding gravity to the world:

world.add(Physics.behavior('constant-acceleration'));

Handling collisions:

world.on('collisions:detected', function(data) {
    data.collisions.forEach(function(collision) {
        console.log('Collision between', collision.bodyA.uid, 'and', collision.bodyB.uid);
    });
});

Getting Started

  1. Include PhysicsJS in your project:
<script src="https://cdnjs.cloudflare.com/ajax/libs/PhysicsJS/0.7.0/physicsjs-full.min.js"></script>
  1. Create a world and add bodies:
var world = Physics();

world.add(
    Physics.body('circle', {
        x: 50,
        y: 30,
        radius: 20
    }),
    Physics.body('rectangle', {
        x: 100,
        y: 50,
        width: 50,
        height: 30
    })
);

// Add behaviors
world.add([
    Physics.behavior('constant-acceleration'),
    Physics.behavior('body-impulse-response'),
    Physics.behavior('body-collision-detection'),
    Physics.behavior('sweep-prune')
]);

// Start the simulation
Physics.util.ticker.on(function(time) {
    world.step(time);
});
Physics.util.ticker.start();

This basic setup creates a world with a circle and a rectangle, adds gravity and collision detection, and starts the simulation.

Competitor Comparisons

A lightweight 3D physics engine written in JavaScript.

Pros of cannon.js

  • More advanced 3D physics simulation capabilities
  • Better performance for complex simulations
  • Larger community and more frequent updates

Cons of cannon.js

  • Steeper learning curve for beginners
  • Less focus on 2D physics compared to PhysicsJS
  • Requires more setup and configuration for basic scenarios

Code Comparison

cannon.js:

const world = new CANNON.World();
world.gravity.set(0, -9.82, 0);

const sphereShape = new CANNON.Sphere(0.5);
const sphereBody = new CANNON.Body({
  mass: 5,
  shape: sphereShape
});
world.addBody(sphereBody);

PhysicsJS:

const world = Physics();
world.add(Physics.behavior('constant-acceleration'));

const sphere = Physics.body('circle', {
  x: 0,
  y: 0,
  radius: 0.5,
  mass: 5
});
world.add(sphere);

Both libraries offer physics simulation capabilities, but cannon.js is more focused on 3D physics with advanced features, while PhysicsJS provides a simpler API for 2D physics. cannon.js may be better suited for complex 3D games or simulations, while PhysicsJS might be preferable for simpler 2D projects or educational purposes. The code comparison shows that cannon.js requires more explicit setup, while PhysicsJS offers a more concise API for basic scenarios.

16,610

a 2D rigid body physics engine for the web ▲● ■

Pros of Matter.js

  • More active development and larger community support
  • Better documentation and examples
  • Wider range of features, including constraints and composites

Cons of Matter.js

  • Slightly steeper learning curve for beginners
  • Potentially higher performance overhead for complex simulations

Code Comparison

Matter.js:

var engine = Matter.Engine.create();
var world = engine.world;
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(world, box);
Matter.Engine.run(engine);

PhysicsJS:

var world = Physics();
var box = Physics.body('rectangle', {
  x: 200,
  y: 200,
  width: 80,
  height: 80
});
world.add(box);
world.step();

Summary

Both Matter.js and PhysicsJS are 2D physics engines for JavaScript, but Matter.js has a more active community and extensive features. PhysicsJS may be simpler for beginners but has less ongoing development. Matter.js offers more advanced capabilities like constraints and composites, while PhysicsJS focuses on a modular approach. The code comparison shows that Matter.js uses a more object-oriented style, while PhysicsJS employs a functional approach. Choose Matter.js for more complex simulations and ongoing support, or PhysicsJS for simpler projects with a gentler learning curve.

👋💣 physics based hooks for @react-three/fiber

Pros of use-cannon

  • Integrates seamlessly with React and Three.js ecosystems
  • Offers a more modern, hooks-based API for physics simulations
  • Actively maintained with frequent updates and community support

Cons of use-cannon

  • Limited to React applications, less versatile than PhysicsJS
  • Steeper learning curve for developers not familiar with React and Three.js
  • Potentially higher performance overhead due to React's rendering cycle

Code Comparison

use-cannon:

import { useCannon, Physics } from '@react-three/cannon'

function Box() {
  const [ref] = useCannon(() => ({
    mass: 1,
    position: [0, 5, 0],
    shape: 'Box',
  }))
  return <mesh ref={ref} />
}

PhysicsJS:

var world = Physics();
var box = Physics.body('rectangle', {
  x: 0,
  y: 5,
  width: 1,
  height: 1,
});
world.add(box);

Summary

use-cannon is tailored for React and Three.js developers, offering a modern API but with limited scope. PhysicsJS provides a more traditional, versatile approach to physics simulations, suitable for various JavaScript environments. The choice between them depends on the project's requirements and the developer's familiarity with React and Three.js ecosystems.

4,134

Direct port of the Bullet physics engine to JavaScript using Emscripten

Pros of Ammo.js

  • Based on the Bullet physics engine, providing robust and accurate physics simulations
  • Supports a wide range of physics features, including rigid body dynamics and soft body physics
  • Offers better performance for complex simulations due to its C++ core compiled to WebAssembly

Cons of Ammo.js

  • Steeper learning curve due to its complexity and lower-level API
  • Larger file size, which may impact load times for web applications
  • Less intuitive for simple 2D physics simulations compared to PhysicsJS

Code Comparison

PhysicsJS (simple circle creation):

var world = Physics();
var circle = Physics.body('circle', {
  x: 50,
  y: 30,
  radius: 20
});
world.add(circle);

Ammo.js (simple sphere creation):

var transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin(new Ammo.btVector3(0, 0, 0));
var sphere = new Ammo.btSphereShape(1);
var body = new Ammo.btRigidBody(new Ammo.btRigidBodyConstructionInfo(
  1, new Ammo.btDefaultMotionState(transform), sphere));

The code comparison demonstrates that PhysicsJS has a more straightforward API for basic shapes, while Ammo.js requires more setup but offers greater control and flexibility for complex simulations.

3,047

Lightweight 3d physics engine for javascript

Pros of Oimo.js

  • Lightweight and fast 3D physics engine
  • Supports various joint types and constraints
  • Easier integration with Three.js for 3D graphics

Cons of Oimo.js

  • Less extensive documentation compared to PhysicsJS
  • Fewer built-in renderers and integrations
  • Limited 2D physics capabilities

Code Comparison

Oimo.js:

var world = new OIMO.World({ 
    timestep: 1/60, 
    iterations: 8, 
    broadphase: 2,
    worldscale: 1, 
    random: true
});

PhysicsJS:

var world = Physics({ 
    timestep: 1000.0 / 160,
    maxIPF: 16,
    integrator: 'verlet'
});

Summary

Oimo.js is a lightweight 3D physics engine that excels in performance and integration with 3D graphics libraries like Three.js. It offers various joint types and constraints, making it suitable for complex 3D simulations. However, it has less extensive documentation and fewer built-in renderers compared to PhysicsJS.

PhysicsJS, on the other hand, provides more comprehensive documentation and a wider range of built-in renderers and integrations. It also offers better support for 2D physics simulations. However, it may not be as optimized for 3D physics as Oimo.js.

The choice between the two depends on the specific requirements of your project, particularly whether you need 2D or 3D physics simulations and the level of integration with other libraries you require.

2,763

Physics plugin for Three.js

Pros of Physijs

  • Integrates directly with Three.js, making it easier for developers already familiar with this popular 3D library
  • Supports a wider range of physics simulations, including soft-body physics
  • More actively maintained with recent updates and contributions

Cons of Physijs

  • Relies on Ammo.js, which can be more complex to set up and use
  • May have a steeper learning curve for beginners due to its integration with Three.js
  • Performance can be slower for large-scale simulations compared to PhysicsJS

Code Comparison

PhysicsJS:

var world = Physics();
var ball = Physics.body('circle', {
  x: 50,
  y: 30,
  radius: 20
});
world.add(ball);

Physijs:

var scene = new Physijs.Scene();
var ball = new Physijs.SphereMesh(
  new THREE.SphereGeometry(20, 32, 32),
  new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
scene.add(ball);

Both libraries offer physics simulations for JavaScript applications, but they cater to different use cases. PhysicsJS is more lightweight and easier to get started with, while Physijs provides more advanced features and integrates well with Three.js for complex 3D simulations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

This project is no longer maintained

It's been many years since writing this library. And I don't have time to dedicate to maintain it. On the up side, Matter.js has really stepped up and taken the spotlight of javascript physics. It has everything PhysicsJS has and more. :)

PhysicsJS

A modular, extendable, and easy-to-use physics engine for javascript.

Latest version: 0.7.0 (beta)

Usage

Please visit the website for details about installation and usage.

Distribution files are in the dist/ directory.

Contributing

Source code is kept in the src/ directory. After any source code modifications it will be necessary to run the grunt build task to rebuild the source and run unit tests.

First install grunt.

Next install dev dependencies:

$ npm install

then run grunt

$ grunt

The default grunt task will create a _working/ directory with the PhysicsJS development build. You can play around with that. NOTE: the _working/ directory won't be committed (it is in .gitignore).

After you run this you can use (Mr.doob's) htmleditor in editor/ to play around.

If you want grunt to automatically create the development build when you modify the source in src/ then run:

$ grunt watch

Note grunt watch won't run unit tests.

Pull Requests

If you are contributing a bug-fix or a very minor addition, feel free to do a pull request on the master branch.

If it is something else create a new (or existing) feature branch (eg: feature/MY_FEAT) and issue a pull request on that.

If unsure, create an issue to discuss.

Please ensure that:

  • the files in dist/ are unmodified.
  • the features you add are well documented with jsdoc comments if applicable.
  • the code is indented with 4 space characters.

License MIT

Copyright (c) 2013 Jasper Palfree http://wellcaffeinated.net/PhysicsJS/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.