首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Physicsjs中创建固定位置的实体

在Physicsjs中创建固定位置的实体
EN

Stack Overflow用户
提问于 2015-03-23 19:14:07
回答 1查看 589关注 0票数 0
代码语言:javascript
复制
    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
    ]);

    });

我试着做一个桌球式的游戏,我想让身体中的矩形固定在那个位置就像一堵墙一样,但是当一些物体由于物理原因与它相撞时会反弹,所以有人能帮我关闭那个物体(矩形)的物理吗?这样它就保持固定在那个点上。

EN

回答 1

Stack Overflow用户

发布于 2015-03-27 04:51:34

在创建主体时,必须将treatment属性调整为"static"。否则,您可以只设置myBody.treatment = "static"

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29208837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档