Convert Figma logo to code with AI

chandlerprall logoPhysijs

Physics plugin for Three.js

2,763
455
2,763
150

Top Related Projects

3,047

Lightweight 3d physics engine for javascript

4,134

Direct port of the Bullet physics engine to JavaScript using Emscripten

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

Quick Overview

Physijs is a physics plugin for Three.js, providing a simple way to integrate realistic physics simulations into 3D web applications. It wraps the ammo.js physics engine and offers an intuitive API for creating and manipulating physics-enabled objects within a Three.js scene.

Pros

  • Easy integration with Three.js projects
  • Simplified API for complex physics simulations
  • Supports various types of physics objects and constraints
  • Good performance for web-based 3D physics

Cons

  • No longer actively maintained (last update in 2017)
  • Limited documentation and examples
  • May have compatibility issues with newer versions of Three.js
  • Lacks some advanced features found in more modern physics engines

Code Examples

Creating a basic physics scene:

var scene = new Physijs.Scene();
scene.setGravity(new THREE.Vector3(0, -10, 0));

var ground = new Physijs.BoxMesh(
    new THREE.BoxGeometry(100, 1, 100),
    new THREE.MeshBasicMaterial({ color: 0x888888 })
);
scene.add(ground);

var box = new Physijs.BoxMesh(
    new THREE.BoxGeometry(5, 5, 5),
    new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
box.position.set(0, 20, 0);
scene.add(box);

Adding a constraint between two objects:

var constraint = new Physijs.DOFConstraint(
    objectA, objectB, new THREE.Vector3(0, 10, 0)
);
scene.addConstraint(constraint);

Applying forces to objects:

box.applyCentralImpulse(new THREE.Vector3(10, 0, 0));
box.applyTorque(new THREE.Vector3(0, 10, 0));

Getting Started

  1. Include Three.js and Physijs in your HTML:
<script src="js/three.min.js"></script>
<script src="js/physi.js"></script>
  1. Initialize Physijs:
Physijs.scripts.worker = 'js/physijs_worker.js';
Physijs.scripts.ammo = 'ammo.js';
  1. Create a Physijs scene and add objects:
var scene = new Physijs.Scene();
scene.setGravity(new THREE.Vector3(0, -9.8, 0));

var ground = new Physijs.PlaneMesh(
    new THREE.PlaneGeometry(100, 100),
    new THREE.MeshBasicMaterial({ color: 0x888888 })
);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);

// Add more objects and set up your Three.js renderer and camera
  1. Simulate physics in your render loop:
function render() {
    scene.simulate();
    renderer.render(scene, camera);
    requestAnimationFrame(render);
}
render();

Competitor Comparisons

3,047

Lightweight 3d physics engine for javascript

Pros of Oimo.js

  • Lightweight and fast physics engine
  • Supports both 2D and 3D physics simulations
  • Active development and regular updates

Cons of Oimo.js

  • Less extensive documentation compared to Physijs
  • Smaller community and fewer resources available
  • May require more manual setup for complex scenarios

Code Comparison

Oimo.js:

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

Physijs:

Physijs.scripts.worker = '/js/physijs_worker.js';
Physijs.scripts.ammo = '/js/ammo.js';

var scene = new Physijs.Scene;
scene.setGravity(new THREE.Vector3(0, -10, 0));

Key Differences

  • Oimo.js is a standalone physics engine, while Physijs is built on top of Three.js
  • Physijs offers tighter integration with Three.js, making it easier to use for 3D graphics
  • Oimo.js provides more flexibility and can be used with various rendering engines
  • Physijs has a more extensive set of pre-built constraints and joint types
  • Oimo.js may offer better performance for simpler physics simulations

Both libraries have their strengths and are suitable for different use cases. The choice between them depends on the specific requirements of your project and your familiarity with Three.js.

4,134

Direct port of the Bullet physics engine to JavaScript using Emscripten

Pros of Ammo.js

  • Direct port of Bullet physics engine, offering more comprehensive physics simulation
  • Better performance for complex physics scenarios
  • Wider range of physics features and customization options

Cons of Ammo.js

  • Steeper learning curve due to lower-level API
  • Requires more manual setup and management of physics objects
  • Less seamless integration with Three.js compared to Physijs

Code Comparison

Physijs example:

var box = new Physijs.BoxMesh(
    new THREE.BoxGeometry(5, 5, 5),
    new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
scene.add(box);

Ammo.js example:

var transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin(new Ammo.btVector3(0, 0, 0));
var motionState = new Ammo.btDefaultMotionState(transform);
var shape = new Ammo.btBoxShape(new Ammo.btVector3(2.5, 2.5, 2.5));
var body = new Ammo.btRigidBody(new Ammo.btRigidBodyConstructionInfo(0, motionState, shape));
physicsWorld.addRigidBody(body);

Physijs provides a higher-level abstraction for physics integration with Three.js, while Ammo.js offers more control but requires more detailed setup. Physijs is easier to use for simple physics simulations, whereas Ammo.js is more suitable for complex physics scenarios and custom implementations.

A lightweight 3D physics engine written in JavaScript.

Pros of cannon.js

  • Pure JavaScript implementation, allowing for easier integration and customization
  • More active development and community support
  • Supports a wider range of physics simulations, including soft-body physics

Cons of cannon.js

  • Generally slower performance compared to Physijs, which uses Ammo.js (C++ port)
  • Steeper learning curve for beginners due to lower-level API
  • Less seamless integration with Three.js compared to Physijs

Code Comparison

Physijs:

var scene = new Physijs.Scene();
var box = new Physijs.BoxMesh(
    new THREE.BoxGeometry(5, 5, 5),
    new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
scene.add(box);

cannon.js:

var world = new CANNON.World();
var shape = new CANNON.Box(new CANNON.Vec3(2.5, 2.5, 2.5));
var body = new CANNON.Body({ mass: 1, shape: shape });
world.addBody(body);

Both libraries offer robust physics simulations for web applications, but they cater to different needs. Physijs provides a more straightforward integration with Three.js and better performance, while cannon.js offers more flexibility and a wider range of physics simulations. The choice between the two depends on the specific requirements of your project and your familiarity with physics engines.

16,610

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

Pros of matter-js

  • Pure JavaScript implementation, making it easier to integrate and use in web projects
  • More active development and community support
  • Extensive documentation and examples

Cons of matter-js

  • Limited to 2D physics simulations
  • May have performance limitations for complex simulations compared to WebGL-based solutions

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

Physijs:

var scene = new THREE.Scene();
var box = new Physijs.BoxMesh(
    new THREE.BoxGeometry(50, 50, 50),
    new THREE.MeshBasicMaterial({ color: 0x888888 })
);
scene.add(box);

Key Differences

  • matter-js is a standalone 2D physics engine, while Physijs is a 3D physics plugin for Three.js
  • Physijs leverages WebGL for rendering, potentially offering better performance for complex 3D simulations
  • matter-js has a more straightforward API and setup process, making it easier for beginners to get started
  • Physijs integrates seamlessly with Three.js, providing a familiar workflow for Three.js developers

Both libraries have their strengths and are suitable for different use cases. Choose matter-js for 2D web-based physics simulations or Physijs for 3D physics integrated with Three.js projects.

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

Pros of use-cannon

  • More recent and actively maintained project
  • Designed specifically for React and Three.js integration
  • Supports modern JavaScript features and React hooks

Cons of use-cannon

  • Steeper learning curve for developers not familiar with React
  • May have less comprehensive documentation compared to Physijs

Code Comparison

Physijs:

var scene = new Physijs.Scene();
var box = new Physijs.BoxMesh(
    new THREE.BoxGeometry(5, 5, 5),
    new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
scene.add(box);

use-cannon:

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

function Box(props) {
  const [ref] = useBox(() => ({ mass: 1, ...props }))
  return <mesh ref={ref}><boxGeometry /></mesh>
}

function Scene() {
  return (
    <Physics>
      <Box position={[0, 5, 0]} />
    </Physics>
  )
}

Summary

Physijs is an older physics engine for Three.js, while use-cannon is a more modern solution specifically designed for React applications using Three.js. use-cannon offers better integration with React and modern JavaScript features, but may have a steeper learning curve for developers not familiar with React. Physijs might be easier to get started with for pure Three.js projects, but lacks the React-specific optimizations and hooks that use-cannon provides.

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

Physijs

Physics plugin for three.js

Physijs brings a very easy to use interface to the three.js framework. One of the reasons three.js is so popular is because it is so incredibly easy for graphics newbies to get into 3D programming. Physijs takes that philosophy to heart and makes physics simulations just as easy to run. In fact, there are just five easy steps that must be taken to make a 3D scene come alive.

How does Physijs work?

Physijs is built on top of ammo.js (although there is also a cannon.js branch) and runs the physics simulation in a separate thread (via web worker) to avoid impacting in your application's performance and taking up your 3D rendering time.

A lot of effort has been made to keep the style of code the same when using Physijs. Apart from updating an object's position, all of the normal three.js conventions remain the same. If you are used to three.js, you already know how to use the Physijs plugin.

Who is this for?

You, hopefully. If you are familiar with three.js and want to add physics to your scene, this is the plugin for you. No mucking about with shape definitions, keeping objects in their correct positions, or identifying collisions - simply use a few Physijs objects in place of three.js's and you'll automatically have a dynamic environment.

If you need (or want) a feature not already included then add it to the issue tracker or implement it yourself and send over a pull request.

Examples

rigid bodies collisions compound shapes all shapes jenga car constraints vehicle

Features

  • Support for multiple object shapes, including custom convex or concave objects as well as heightmaps
  • Material system provides simple control over friction and restitution ("bounciness")
  • Integrated collision detection and events
  • Compound objects using the hierarchy system in three.js
  • Vehicle system
  • Constraint systems such as point-to-point and hinge
  • Rotations using either euler or quaternion systems - your preference
  • Built seamlessly on top of three.js to keep the same convention and coding style

In the Future

  • More (and better) optimizations
  • It would be awesome to have concave shape decomposition