在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):schteppe/p2.js开源软件地址(OpenSource Url):https://github.com/schteppe/p2.js开源编程语言(OpenSource Language):JavaScript 99.4%开源软件介绍(OpenSource Introduction):p2.js2D 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 Featured projects using p2.js
DemosThese 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.
ExamplesExamples showing how to use p2.js with your favorite renderer.
Sample codeThe 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 world.on('postStep', function(event){
// Add horizontal spring force
circleBody.force[0] -= 100 * circleBody.position[0];
}); InstallBrowserDownload 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 <script type="text/javascript">P2_ARRAY_TYPE = Array;</script>
<script src="p2.js" type="text/javascript"></script> Node.js
Then require it like so: var p2 = require('p2'); Supported collision pairs
Note that concave polygon shapes can be created using Body.fromPolygon. InstallMake sure you have git, Node.js, NPM and grunt installed.
Grunt tasksList all tasks using
Release process
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论