PhysicsJS - 两个身体之间的碰撞永无止境

时间:2014-11-16 18:38:03

标签: javascript physicsjs

我有这个简单的js:



Physics(function( world ){

        //Defining object
        var renderer = Physics.renderer('canvas', {
            el: 'viewport', // id of the canvas element
            width: 500,
            height: 200
        });
        
        var ball = Physics.body('circle', {
            x: 250,
            y: 0,
            radius:10,
            restitution: .8,
            mass: 3,
            vx: 0
        });
        
        var box = Physics.body('rectangle', 
            {
                x: 250,
                y: 150,
                width: 50,
                height: 20,
                restitution:.5,
                treatment: "static"
            }
        );
                
        // Add them to the world
        world.add( renderer );
        world.add( ball );
        world.add( box );        
        
        //Add physical behaviours
        world.add( Physics.behavior('constant-acceleration') );//add gravity
        world.add( Physics.behavior('body-impulse-response') );//make obdies bounce (react to impulse)
        world.add( Physics.behavior('body-collision-detection') );//detect collision between bodies
        world.add( Physics.behavior('sweep-prune') );
        world.add( Physics.behavior('edge-collision-detection', //dectect collision with edges
                {
                    aabb: Physics.aabb(0, 0, 500, 500),
                    restitution:.3
                }
            )  
        );
        
        //START playing
        world.on('render', function( data ){
            var renderer = data.renderer;
        });

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

        world.on('step', function(){
            world.render();
            console.log(ball.state.vel._[1]);
        });

        // start the ticker
        Physics.util.ticker.start();        

    });

canvas {border:1px solid;}

<script src="http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/physicsjs-0.6.0/physicsjs-full-0.6.0.min.js"></script>
<canvas id="viewport" width="500" height="200"></canvas>
&#13;
&#13;
&#13;

创造一个带有静态平台和落在其上的球的世界。球落下并弹跳,然后弹跳缓慢而缓慢,但它永远不会结束。我已在body属性中设置restitution < 1

我哪里错了?

1 个答案:

答案 0 :(得分:1)

这是当前版本的physicsJS的一个错误。您的代码是正确的。尝试从github上的0.7分支获取。您需要查看回购并运行&#34; grunt dist&#34;。

https://github.com/wellcaffeinated/PhysicsJS/tree/0.7.0

请注意,这不是0.7版本。此代码仍在开发中。我建议监视physicsjs并在它准备就绪时获得实际的0.7版本。应该很快。