Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
A Pen by Gabriel Fernandes on CodePen.
| <canvas id="viewport" width="800" height="800"></canvas> |
Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
Forked from Well Caffeinated's Pen PhysicsJS Beginner demo.
A Pen by Gabriel Fernandes on CodePen.
| Physics(function(world){ | |
| var viewWidth = 800; | |
| var viewHeight = 800; | |
| var renderer = Physics.renderer('canvas', { | |
| el: 'viewport', | |
| width: viewWidth, | |
| height: viewHeight, | |
| meta: false, // don't display meta data | |
| styles: { | |
| // set colors for the circle bodies | |
| 'circle' : { | |
| strokeStyle: 'hsla(0, 70%, 20%, 1)', | |
| lineWidth: 5, | |
| fillStyle: 'hsla(0, 100%, 50%, 1)', | |
| angleIndicator: 'hsla(0, 70%, 20%, 1)' | |
| } | |
| } | |
| }); | |
| // add the renderer | |
| world.add( renderer ); | |
| // render on each step | |
| world.subscribe('step', function(){ | |
| world.render(); | |
| }); | |
| // bounds of the window | |
| var viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight); | |
| // constrain objects to these bounds | |
| world.add(Physics.behavior('edge-collision-detection', { | |
| aabb: viewportBounds, | |
| restitution: 0.8, | |
| cof: 0.1 | |
| })); | |
| // add circles | |
| var m = 70; | |
| var n = 0.55 | |
| for (var x = 0; x < 4; x++) { | |
| world.add( | |
| Physics.body('circle', { | |
| x: m, // x-coordinate | |
| y: 400, // y-coordinate | |
| vx: 0, // velocity in x-direction | |
| vy: n, // velocity in y-direction | |
| radius: 10, | |
| mass: 1 | |
| //angularVelocity: 1, | |
| //fixed: true | |
| }) | |
| ); | |
| m = m + 50; | |
| n = n + 0.05 | |
| } | |
| var a = 400; | |
| var b = 400; | |
| for (var x = 0; x < 1; x++) { | |
| world.add( | |
| Physics.body('circle', { | |
| x: a, // x-coordinate | |
| y: b, // y-coordinate | |
| vx: 0, // velocity in x-direction | |
| vy: 0, // velocity in y-direction | |
| fixed:true, | |
| mass: 5000, | |
| radius: 50 | |
| }) | |
| ); | |
| a = a + 10; | |
| b = b + 30; | |
| } | |
| // ensure objects bounce when edge collision is detected | |
| world.add( Physics.behavior('body-impulse-response') ); | |
| // add some gravity | |
| //world.add( Physics.behavior('constant-acceleration') ); | |
| world.add( Physics.behavior('newtonian', { strength: .02 }) ); | |
| world.add( Physics.behavior('sweep-prune') ); | |
| world.add( Physics.behavior('body-collision-detection') ); | |
| // subscribe to ticker to advance the simulation | |
| Physics.util.ticker.subscribe(function( time, dt ){ | |
| world.step( time * 1); | |
| }); | |
| // start the ticker | |
| Physics.util.ticker.start(); | |
| }); |
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
| <script src="http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/physicsjs-0.5.0/physicsjs-full-0.5.0.min.js"></script> |
| body { | |
| background: #121212; | |
| } | |
| .pjs-meta { | |
| display: none; | |
| } | |
| #viewport { | |
| border: 2px solid #666666; | |
| } |