Reputation: 61
I found this example that works for physics.js 0.6.0 http://jsfiddle.net/REGCU/22/
but I cannot get it to work with 0.7.0 http://jsfiddle.net/REGCU/23/
javascript:
Physics(function (world) {
var viewWidth = window.innerWidth
,viewHeight = window.innerHeight
// bounds of the window
,viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight)
,edgeBounce
,renderer
;
// create a renderer
renderer = Physics.renderer('canvas', {
el: 'viewport'
,width: viewWidth
,height: viewHeight
});
// add the renderer
world.add(renderer);
// render on each step
world.on('step', function () {
world.render();
});
// constrain objects to these bounds
edgeBounce = Physics.behavior('edge-collision-detection', {
aabb: viewportBounds
,restitution: 0.2
,cof: 1
});
var gravity = Physics.behavior('constant-acceleration', {
acc: { x : 0, y: 0.0004 } // this is the default: .0004
});
// resize events
window.addEventListener('resize', function () {
viewWidth = window.innerWidth;
viewHeight = window.innerHeight;
renderer.el.width = viewWidth;
renderer.el.height = viewHeight;
viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight);
// update the boundaries
edgeBounce.setAABB(viewportBounds);
}, true);
// for constraints
var rigidConstraints = Physics.behavior('verlet-constraints', {
iterations: 10
});
// the "basket"
var basket = [];
var fpos = window.innerWidth / 2;
var epos = window.innerHeight / 2;
for ( var i = fpos; i < fpos + epos; i += 5 ){
l = basket.push(
Physics.body('circle', {
x: i
,y: 50 - (i-fpos)
,radius: 1
,restitution: 0
,mass: 1000
,conf: 1
,hidden: true
})
);
rigidConstraints.distanceConstraint( basket[ l - 1 ], basket[ l - 2 ], 2 );
}
var box = Physics.body('rectangle', {
x: i
,y: 50 - (i-fpos)
,width: 60
,height: 60
,styles: { fillStyle: '#fff' }
});
rigidConstraints.distanceConstraint( basket[ l - 1 ], box, 2 );
world.add(box);
world.on('render', function( data ){
var renderer = data.renderer;
for ( var i = 1, l = basket.length; i < l; ++i ){
renderer.drawLine(basket[ i - 1 ].state.pos, basket[ i ].state.pos, {
strokeStyle: '#ccc'
,lineWidth: 5
,restitution: 0
,mass: 1000
,conf: 1
});
}
});
// fix the ends
basket[ 0 ].treatment = 'static';
world.add( basket );
world.add( rigidConstraints );
// add things to the world
world.add([
Physics.behavior('interactive', { el: renderer.el })
,Physics.behavior('constant-acceleration')
,Physics.behavior('body-impulse-response')
// ,Physics.behavior('body-collision-detection')
,Physics.behavior('sweep-prune')
,edgeBounce
,gravity
]);
// subscribe to ticker to advance the simulation
Physics.util.ticker.on(function( time ) {
world.step( time );
});
// start the ticker
Physics.util.ticker.start();
});
body{
background: #1d1d1d;
margin: 0;
padding: 0;
}
<script src="http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/physicsjs-current/physicsjs-full.js"></script>
and I get no error messages (just nothing happens on the output).
I have also been able to run the 0.6.0 version on my computer, but had similar non-results for the 0.7.0 version.
Any ideas on what I need to change?
Upvotes: 0
Views: 211