JavaScript 2D rigid body physics engine.
- Written in pure JavaScript
- For Node.js and browser
- Detailed documentation
- Rigid body physics
- Collision detection (no CCD)
- Contacts, friction
- Stiff constraints: Distance, Revolute, Prismatic
- Motors
- Springs
- Dynamic, kinematic, static bodies
- Many supported shapes (see table below)
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>
Until the code gets somewhat more stable, use the git url to install:
npm install git://github.com/schteppe/p2.js
Or add the dependency to your package.json
:
...
"dependencies" : {
"p2" : "git://github.com/schteppe/p2.js"
}
...
Then require it like so:
var p2 = require('p2');
// Setup our world
var world = new p2.World({ gravity:[0,-9.82] });
// Create a circle
var radius = 1,
circleShape = new p2.Circle(radius),
circleBody = new p2.Body({ mass:5, position:[0,10] });
circleBody.addShape(circleShape);
// Create a plane
var groundShape = new p2.Plane(),
groundBody = new p2.Body({ mass:0 });
groundBody.addShape(groundShape);
// Add the bodies to the world
world.addBody(circleBody);
world.addBody(groundBody);
// Step the simulation
setInterval(function(){
world.step(1.0/60.0);
console.log("Circle y position: " + circleBody.position[1]);
}, 1000.0/60.0);
Circle | Plane | Rectangle | Convex | Particle | Line | Capsule | |
---|---|---|---|---|---|---|---|
Circle | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Plane | - | - | Yes | Yes | Yes | Yes | Yes |
Rectangle | - | - | Yes | Yes | Yes | (todo) | (todo) |
Convex | - | - | - | Yes | Yes | (todo) | (todo) |
Particle | - | - | - | - | - | - | Yes |
Line | - | - | - | - | - | (todo) | (todo) |
Capsule | - | - | - | - | - | - | (todo) |
Note that concave polygon shapes can be created using Body.prototype.fromPolygon
.
- Added
Utils
- The returned array by
Broadphase.prototype.getCollisionPairs
is now reused in between calls. - Added method
.addEquations
toSolver
to faster add an array ofEquation
s. - Added method
.updateArea
and property.area
to Shape - Added methods
.adjustCenterOfMass
and.fromPolygon
toBody
. - Renamed
Nearphase
toNarrowphase
. - Added methods
.upgradeJSON
,.runNarrowphase
and.integrateBody
toWorld
. - Renamed
PointToPointConstraint
toRevoluteConstraint
. - Added static method
GSSolver.iterateEquation
. - Added methods
.addConstraintVelocity
,.resetConstraintVelocity
,.setZeroForce
toBody
. - Added static method
SAP1DBroadphase.checkBounds
. body.shapeOffsets
and.shapeAngles
may now only be vectors and numbers.
- Removed
World.collidingBodies
sinceWorld.bodies
can be used equivalently. - Added property
DistanceConstraint.distance
- Added contact response between a lot of Shape types. Check table above.
- Added friction for most of the contact types
- Added
PointToPointConstraint
- Added
Line
- Added method
Shape.computeMomentOfInertia
- Added method
Body.updateMassProperties
- Removed
mat2
as it is not needed inside the library for now. - Changed
Shape
usage. AShape
is now added to aBody
viaBody.addShape(shape,offset,angle)
. - Removed
Body.shape
, addedBody.shapes
,.shapeOffsets
,.shapeAngles
- Added
Rectangle
- Added method
Convex.updateTriangles
- Added method
Convex.updateCenterOfMass
- Fixed
Convex.computeMomentOfInertia
, now it should be correct. - Updated the Node.js API so that each class is required using the pattern
var ClassName = require('./ClassName')
instead ofvar ClassName = require('./ClassName').ClassName
- Added
Nearphase
class - Removed
World.contacts
and.frictionEquations
in favor ofWorld.nearphase.contactEquations
andWorld.nearphase.frictionEquations
- Added
Capsule
class - Added
Spring
properties.localAnchorA
,.localAnchorB
. - Added
Spring
methods.getWorldAnchorA
,.setWorldAnchorA
and the corresponding for B. - Added
p2.version
for browser. - Added
PrismaticConstraint
. - Added properties
.collisionGroup
and.collisionMask
toShape
. This enables collision filtering. - Added method
World.hitTest
. - Renamed equation properties
.k
and.d
ofEquation
andGSSolver
to.stiffness
and.relaxation
, respectively. Removed properties.a
,.b
,.eps
from both and instead compute them on the fly in the solver. - Added property
GSSolver.useGlobalEquationParameters
so that it is possible to turn on and off per-equation solver settings (.stiffness
and.relaxation
). - Removed
Broadphase.<shapeTypeA><shapeTypeB>
since they are not used in any broadphase implementation any more. - Added experimental
QuadTree
broadphase. - Added
SAP1DBroadphase
- Renamed
World.friction
toWorld.defaultFriction
- Added class
RotationalVelocityEquation
- Added methods
.enableMotor
,.disableMotor
,.setMotorSpeed
toPointToPointConstraint
- Added
EventEmitter
World
now emits the following events:postStep
,addBody
,addSpring
- Moved properties of
Body.MotionState
toBody
. Usage is nowBody.STATIC
,Body.DYNAMIC
,Body.KINEMATIC
. - Removed asynchronous behaviour of
World.step()
.
Make sure you have git, Node.js, NPM and grunt installed.
git clone https://github.com/schteppe/p2.js.git; # Clone the repo
cd p2.js;
npm install; # Install dependencies
# (make changes to source)
grunt; # Builds build/p2.js and build/p2.min.js
The most recent commits are currently pushed to the master
branch. Thanks for contributing!