在Physicsjs的固定位置制作一个身体

时间:2015-03-23 11:14:07

标签: javascript jquery physicsjs

    require.config({
    baseUrl: 'http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/',
    packages: [
    {
    name: 'physicsjs',
    location: 'physicsjs-current',
    main: 'physicsjs-full.min'
    }
    ]
    });

    var colors = [
    ['0x268bd2', '0x0d394f']
    ,['0xc93b3b', '0x561414']
    ,['0xe25e36', '0x79231b']
    ,['0x6c71c4', '0x393f6a']
    ,['0x58c73c', '0x30641c']
    ,['0xcac34c', '0x736a2c']
    ];

    function initWorld(world, Physics) {

    // bounds of the window
    var viewWidth = window.innerWidth
    ,viewHeight = window.innerHeight
    ,viewportBounds = Physics.aabb(0, 0, window.innerWidth-50, window.innerHeight-50)
    ,edgeBounce
    ,renderer
    ,styles = {
    'circle': {
    fillStyle: colors[0][0],
    lineWidth: 1,
    strokeStyle: colors[0][1],
    angleIndicator: colors[0][1]
    }
    ,'rectangle': {
    fillStyle: colors[1][0],
    lineWidth: 1,
    strokeStyle: colors[1][1],
    angleIndicator: colors[1][1]
    }

    }
    ;

    // create a renderer
    renderer = Physics.renderer('pixi', { el: 'viewport', styles: styles });
    // 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: 0.8
    });

    // resize events
    window.addEventListener('resize', function () {

    // as of 0.7.0 the renderer will auto resize... so we just take the values from the renderer
    viewportBounds = Physics.aabb(0, 0, renderer.width, renderer.height);
    // update the boundaries
    edgeBounce.setAABB(viewportBounds);

    }, true);

    // add behaviors to the world
    world.add([
    Physics.behavior('constant-acceleration')
    ,Physics.behavior('body-impulse-response')
    ,Physics.behavior('body-collision-detection')
    ,Physics.behavior('sweep-prune')
    ,edgeBounce
    ]);  
    }

    function startWorld( world, Physics ){
    // subscribe to ticker to advance the simulation
    Physics.util.ticker.on(function( time ) {
    world.step( time );
    });
    }

    //
    // Add some interaction
    //
    function addInteraction( world, Physics ){
    // add the mouse interaction
    world.add(Physics.behavior('interactive', { el: world.renderer().container }));
    // add some fun extra interaction
    var attractor = Physics.behavior('attractor', {
    order: 0,
    strength: 0.002
    });

    world.on({
    'interact:poke': function( pos ){
    world.wakeUpAll();
    attractor.position( pos );
    world.add( attractor );
    }
    ,'interact:move': function( pos ){
    attractor.position( pos );
    }
    ,'interact:release': function(){
    world.wakeUpAll();
    world.remove( attractor );
    }
    });
    }
    // helper function (bind "this" to Physics)
    function makeBody( obj ){ 
    return this.body( obj.name, obj );
    }

    //
    // Add bodies to the world
    //
    function addBodies( world, Physics ){
    var v = Physics.geometry.regularPolygonVertices;
    var bodies = [
    { name: 'circle', x: 100, y: 100, vx: 0.1, radius: 60,mass:10 }

    ];
    var wallbody =[{
    name: 'rectangle', x: (innerWidth / 2) - 60,fixed:true, y: innerHeight - 30, vx: 0, width: 60, height: 300, mass: 1000, restitution: 0.0, cof: 1000
    }];//want this rectangle to be fixed like a wall

    world.add(bodies.map(makeBody.bind(Physics)));
    world.add(wallbody.map(makeBody.bind(Physics)));



    }

    //
    // Load the libraries with requirejs and create the simulation
    //
    require([
    'physicsjs',
    'pixi'
    ], function( Physics, PIXI ){
    window.PIXI = PIXI;

    var worldConfig = {
    // timestep
    timestep: 6,
    // maximum number of iterations per step
    maxIPF: 4,
    // default integrator
    integrator: 'verlet',
    // is sleeping disabled?
    sleepDisabled: false,
    // speed at which bodies wake up
    sleepSpeedLimit: 0.1,
    // variance in position below which bodies fall asleep
    sleepVarianceLimit: 2,
    // time (ms) before sleepy bodies fall asleep
    sleepTimeLimit: 500
    };

    Physics( worldConfig, [
    initWorld,
    addInteraction,
    addBodies,
    startWorld
    ]);

    });

我正在尝试制作一个桌子游戏类型的游戏,其中我想要将身体中的矩形固定在该位置作为一个墙壁,但是当一些物体因为物理而被弹跳时,所以有人可以帮助我关闭那些物体(矩形)物理,使它在那一点保持固定

1 个答案:

答案 0 :(得分:2)

创建正文时,您必须将treatment属性调整为"static"。否则,您只需设置myBody.treatment = "static"

请参阅: http://wellcaffeinated.net/PhysicsJS/docs/#Physics-body