Zurück zum Ranking

schteppe/p2.js

JavaScript

JavaScript 2D physics library

physics-enginephysicsgamedev
Sterne-Wachstum
Sterne
2.7k
Forks
330
Wochenwachstum
Issues
89
1k2k
März 2013Aug. 2017Feb. 2022Juli 2026
Artefaktenpmnpm install p2.js
README

p2.js

2D rigid body physics engine written in JavaScript. Includes collision detection, contacts, friction, restitution, motors, springs, advanced constraints and various shape types.

Demos | Examples | Documentation | Download | CDN | Wiki

Demos

These demos use the p2 Demo framework, which provides rendering and interactivity. Use mouse/touch to throw or create objects. Use the right menu (or console!) to tweak parameters. Or just check the source to see how to programmatically build the current scene using p2.

Examples

Examples showing how to use p2.js with your favorite renderer.

Sample code

The following example uses the World, Circle, Body and Plane classes to set up a simple physics scene with a ball on a plane.

// Create a physics world, where bodies and constraints live
var world = new p2.World({
    gravity:[0, -9.82]
});

// Create an empty dynamic body
var circleBody = new p2.Body({
    mass: 5,
    position: [0, 10]
});

// Add a circle shape to the body
var circleShape = new p2.Circle({ radius: 1 });
circleBody.addShape(circleShape);

// ...and add the body to the world.
// If we don't add it to the world, it won't be simulated.
world.addBody(circleBody);

// Create an infinite ground plane body
var groundBody = new p2.Body({
    mass: 0 // Setting mass to 0 makes it static
});
var groundShape = new p2.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);

// To animate the bodies, we must step the world forward in time, using a fixed time step size.
// The World will run substeps and interpolate automatically for us, to get smooth animation.
var fixedTimeStep = 1 / 60; // seconds
var maxSubSteps = 10; // Max sub steps to catch up with the wall clock
var lastTime;

// Animation loop
function animate(time){
	requestAnimationFrame(animate);

    // Compute elapsed time since last render frame
    var deltaTime = lastTime ? (time - lastTime) / 1000 : 0;

    // Move bodies forward in time
    world.step(fixedTimeStep, deltaTime, maxSubSteps);

    // Render the circle at the current interpolated position
    renderCircleAtPosition(circleBody.interpolatedPosition);

    lastTime = time;
}

// Start the animation loop
requestAnimationFrame(animate);

To interact with bodies, you need to do it after each internal step. Simply attach a "postStep" listener to the world, and make sure to use body.position here - body.interpolatedPosition is only for rendering.

world.on('postStep', function(event){
    // Add horizontal spring force
    circleBody.force[0] -= 100 * circleBody.position[0];
});

Install

Browser

Download either p2.js or the minified p2.min.js and include the script in your HTML:

<script src="p2.js" type="text/javascript"></script>

If you would like to use ordinary Array instead of Float32Array, define P2_ARRAY_TYPE globally before loading the library.

<script type="text/javascript">P2_ARRAY_TYPE = Array;</script>
<script src="p2.js" type="text/javascript"></script>
Node.js
npm install p2

Then require it like so:

var p2 = require('p2');

Supported collision pairs

Circle Plane Box Convex Particle Line Capsule Heightfield Ray
Circle Yes - - - - - - - -
Plane Yes - - - - - - - -
Box Yes Yes Yes - - - - - -
Convex Yes Yes Yes Yes - - - - -
Particle Yes Yes Yes Yes - - - - -
Line Yes Yes (todo) (todo) - - - - -
Capsule Yes Yes Yes Yes Yes (todo) Yes - -
Heightfield Yes - Yes Yes (todo) (todo) (todo) - -
Ray Yes Yes Yes Yes - Yes Yes Yes -

Note that concave polygon shapes can be created using Body.fromPolygon.

Install

Make sure you have git, Node.js, NPM and grunt installed.

git clone https://github.com/schteppe/p2.js.git;
cd p2.js;
npm install; # Install dependencies
grunt;

Grunt tasks

List all tasks using grunt --help.

grunt        # Run tests, build, minify
grunt dev    # Run tests, build
grunt test   # Run tests
grunt yuidoc # Build docs
grunt watch  # Watch for changes and run the "dev" task

Release process

  1. Bump version number.
  2. Build and commit files in build/ and docs/.
  3. Tag the commit with the version number e.g. vX.Y.Z
  4. Add relase notes to github
  5. Publish to NPM
Ähnliche Repositories
liabru/matter-js

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

JavaScriptnpmMIT Licensephysicsjavascript
18.3k2k
jrouwe/JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West and Death Stranding 2.

C++MIT Licensephysicsvr
11k813
chrxh/alien

ALIEN is a CUDA-powered artificial life simulation program.

C++BSD 3-Clause "New" or "Revised" Licenseartificial-lifeopen-ended-evolution
alien-project.org
5.5k188
piqnt/planck.js

2D JavaScript Physics Engine

TypeScriptnpmMIT Licensegame-developmentjavascript
piqnt.com/planck.js/
5.3k254
schteppe/cannon.js

A lightweight 3D physics engine written in JavaScript.

JavaScriptnpmMIT Licensephysics-enginejavascript
schteppe.github.com/cannon.js
5k740
FormidableLabs/react-game-kit

Component library for making games with React & React Native

JavaScriptnpmMIT Licensephysics-bodiesspritesheet
reactnext.surge.sh
4.6k311
cyberbotics/webots

Webots Robot Simulator

C++Apache License 2.0roboticsrobot
cyberbotics.com
4.5k2k
avianphysics/avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.

Rustcrates.ioApache License 2.0bevyphysics
crates.io/crates/avian3d
3.1k265
projectchrono/chrono

High-performance C++ library for multiphysics and multibody dynamics simulations

C++BSD 3-Clause "New" or "Revised" Licensesimulationphysics-simulation
projectchrono.org
2.9k617
bepu/bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.

C#Apache License 2.0physics-3dphysics-engine
2.9k310
simbody/simbody

High-performance C++ multibody dynamics/physics library for simulating articulated biomechanical and mechanical systems like vehicles, robots, and the human skeleton.

C++Apache License 2.0multibody-dynamicsphysics-engine
simtk.org/home/simbody
2.5k496
doyubkim/fluid-engine-dev

Fluid simulation engine for computer graphics applications

C++MIT Licensephysics-enginefluid-simulation-engine
fluidenginedevelopment.org
2.1k283