UNPKG

p2s

Version:

A JavaScript 2D physics engine.

61 lines (54 loc) 2.16 kB
<!DOCTYPE html> <html> <head> <title>Prismatic demo - p2.js physics engine</title> <script src="../build/p2.js"></script> <script src="../build/p2.renderer.js"></script> <link href="css/demo.css" rel="stylesheet"/> <meta name="description" content="Shows how to constrain two bodies using a PrismaticConstraint."> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> </head> <body> <script> // Create demo application var app = new p2.WebGLRenderer(function(){ // Create the physics world var world = new p2.World({ gravity : [0,-10] }); this.setWorld(world); // Create two rectangle bodies var bodyA = new p2.Body({ mass: 1, position: [-1,0], }); bodyA.addShape(new p2.Box({ width: 1, height: 1 })); world.addBody(bodyA); var bodyB = new p2.Body({ mass: 1, position: [1,0], }); bodyB.addShape(new p2.Box({ width: 1, height: 1 })); world.addBody(bodyB); // Create PrismaticConstraint, aka "slider". // It lets two bodies slide along an axis. var prismatic = new p2.PrismaticConstraint(bodyA, bodyB, { localAnchorA : [ 1, 0], // Anchor point in bodyA, where the axis starts localAnchorB : [-1, 0], // Anchor point in bodyB, that will slide along the axis localAxisA : [ 0, 1], // An axis defined locally in bodyA upperLimit : 0.5, // Upper limit along the axis lowerLimit : -0.5, // Lower limit along the axis }); world.addConstraint(prismatic); // Create ground var planeShape = new p2.Plane(); var plane = new p2.Body({ position:[0,-1], }); plane.addShape(planeShape); world.addBody(plane); }); </script> </body> </html>