我无法得到一个与physics.js 0.7.0一起运行的例子

时间:2015-11-09 20:01:20

标签: javascript physicsjs

我发现这个例子适用于physics.js 0.6.0 http://jsfiddle.net/REGCU/22/

但我无法使用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>

我没有收到任何错误消息(输出上没有任何反应)。

我也可以在我的电脑上运行0.6.0版本,但是对于0.7.0版本有类似的非结果。

关于我需要改变什么的想法?

0 个答案:

没有答案