如何制作逼真的轮盘球旋转动画

Raz*_*zor 7 javascript physicsjs

我正在使用PhysicsJS制作2D轮盘球旋转动画.

到目前为止,我已经实现了以下内容:

  • 使用约束使球不会"飞走":
    rigidConstraints.distanceConstraint( wheel, ball, 1 );
  • 拖动减速球:
    world.add(Physics.integrator('verlet', { drag: 0.9 }));
  • 让车轮吸引球,当阻力足够慢时,球会落到它上面

我的问题:

  1. 我如何逐渐减慢球的旋转速度?
    我已经有了很高的drag价值,但它看起来并没有做任何事情
  2. 如何吸引车轮工作?
    距离约束应该防止球逃脱,而不是靠近车轮.
  3. 为什么 angularVelocity: -0.005 在车轮上根本不起作用?

我的代码,也在JSfiddle上

Physics(function (world) {
    var viewWidth = window.innerWidth
        ,viewHeight = window.innerHeight
        ,renderer
        ;

    world.add(Physics.integrator('verlet', {
        drag: 0.9
    }));

    var rigidConstraints = Physics.behavior('verlet-constraints', {
        iterations: 10
    });

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

    // create some bodies
    var ball = Physics.body('circle', {
        x: viewWidth / 2
        ,y: viewHeight / 2 - 300
        ,vx: -0.05
        ,mass: 0.1
        ,radius: 10
        ,cof: 0.99
        ,styles: {
            fillStyle: '#cb4b16'
            ,angleIndicator: '#72240d'
        }
    })

    var wheel = Physics.body('circle', {
        x: viewWidth / 2
        ,y: viewHeight / 2
        ,angularVelocity: -0.005
        ,radius: 100
        ,mass: 100
        ,restitution: 0.35
        // ,cof: 0.99
        ,styles: {
            fillStyle: '#6c71c4'
            ,angleIndicator: '#3b3e6b'
        }
        ,treatment: "static"
    });

    world.add(ball);
    world.add(wheel);

    rigidConstraints.distanceConstraint( wheel, ball, 1 );

    world.add( rigidConstraints );

    // add things to the world
    world.add([
        Physics.behavior('interactive', { el: renderer.el })
        ,Physics.behavior('newtonian', { strength: 5 })
        ,Physics.behavior('body-impulse-response')
        ,Physics.behavior('body-collision-detection')
        ,Physics.behavior('sweep-prune')
    ]);

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

    // start the ticker
    Physics.util.ticker.start();
});
Run Code Online (Sandbox Code Playgroud)

Jas*_*per 4

  1. Drag 在该版本的PhysicsJS 中有一个错误,请尝试使用github 上的最新版本。https://github.com/wellcaffeinated/PhysicsJS/issues/94

  2. 不幸的是,距离约束强加了固定距离。因此,为了防止球以这种方式逃脱,您需要实现自己的行为。(更多内容见下文)

  3. 你必须behavior: "static"改变behavior: "kinematic"。静态物体永远不会自行移动。

要创建自定义行为,请查看此处的文档: https: //github.com/wellcaffeminate/PhysicsJS/wiki/Behaviors#creating-a-custom-behavior

为了获得您所描述的功能,您需要执行以下操作:

// in the behave method
// calculate the displacement of the ball from the wheel... something like....
disp.clone( wheel.state.pos ).vsub( ball.state.pos );
// if it's greater than max distance, then move it back inside the max radius
if ( disp.norm() > maxDist ){
    var moveBy = disp.norm() - maxDist;
    disp.normalize(); // unit vector towards the wheel
    disp.mult( moveBy );
    ball.state.pos.vadd( disp ); // move it back inside the max radius
}
Run Code Online (Sandbox Code Playgroud)

当然,这是一种“只管完成”的方法,但它应该有效。