首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhysicsJS -移动对象

PhysicsJS -移动对象
EN

Stack Overflow用户
提问于 2014-02-06 22:28:05
回答 1查看 1.4K关注 0票数 1

我正在努力弄清楚如何使用PhysicsJS。我首先只想知道如何改变,让我们说一个位置或一个物体的速度点击.但我就是搞不懂!

代码语言:javascript
复制
( function()
{
    var viewWidth = 500,
    viewHeight = 300,
    renderer = Physics.renderer( 'canvas', 
    {
        el: 'viewport',
        width: viewWidth,
        height: viewHeight,
        meta: false,
        styles: 
        {
            'circle' : 
            {
                strokeStyle: 'hsla(60, 37%, 17%, 1)',
                lineWidth: 1,
                fillStyle: 'hsla(60, 37%, 57%, 0.8)',
                angleIndicator: 'hsla(60, 37%, 17%, 0.4)'
            }
        }
    }),
    viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight),
    constraint = {
        aabb: viewportBounds,
        restitution: 0.99,
        cof: 0.99
    },
    ballOptions = {
        x: 100,         // x-coordinate
        y: 100,         // y-coordinate
        vx: 0.0,    // velocity in x-direction
        vy: 0.0,    // velocity in y-direction
        radius: 20
    },
    gravity = Physics.behavior('constant-acceleration', 
    {
        acc: { x : 0, y: 0.0004 } 
    }),
    ball = Physics.body('circle', ballOptions );

    Physics( function( world )
    {

    // add the renderer
    world.add( renderer );
    // add circle
    world.add( ball );

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

    // on every step...
    world.subscribe( 'step', function()
    {
        world.render();
    });

    world.subscribe( 'collisions:detected', function( $collision )
    {

    });

    var onElementClick = function()
    {
        // do something
    };

    document.getElementById( 'viewport' ).addEventListener( 'click', onElementClick, false );

    // Lets GO!
    Physics.util.ticker.start();

});
})();

任何帮助都很感激

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-22 19:49:40

一种选择是将创建但从未添加到世界上的重力进行单击。

代码语言:javascript
复制
world.add(gravity);

这是欺骗,因为你要求改变物体的位置或速度。要做到这一点,修改球的状态。看身体上的医生,特别是属性。您可以设置state.pos来移动它。为了启动它,设定速度:

代码语言:javascript
复制
ball.state.vel.set(.1,-.5); // move right and upward

设定速度的小提琴

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

https://stackoverflow.com/questions/21615444

复制
相关文章

相似问题

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