1

PhysicsJS の使用方法を理解しようとしています。まず、クリックしたオブジェクトの位置や速度を変更する方法を簡単に理解したいだけです...しかし、私はそれを理解することができません!

( 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();

});
})();

どんな助けでも大歓迎です

4

1 に答える 1

4

1 つのオプションは、作成されたがワールドに追加されなかった重力を取得し、onclick を実行することです。

world.add(gravity);

オブジェクトの位置または速度の変更について質問したという意味で、それはごまかしです。そのためには、ボールの状態を変更します。Bodies、特にプロパティのドキュメントを参照してください。state.pos を設定して移動できます。それを動かすには、速度を設定します。

ball.state.vel.set(.1,-.5); // move right and upward

速度を設定するjsfiddle

于 2014-02-22T19:49:40.837 に答える