【问题标题】:PhysicsJS change circle radiusPhysicsJS改变圆半径
【发布时间】:2014-12-02 10:02:29
【问题描述】:

在 2 个点碰撞后,我希望他们用双半径制作 1 个点,所以我的代码

world.on("collisions:detected", function(data) {
    data.collisions[0].bodyA.mass *=2
    data.collisions[0].bodyA.radius *=2
    data.collisions[0].bodyB.mass = 0
    data.collisions[0].bodyA.recalc()
    data.collisions[0].bodyB.recalc()
})

半径不会改变,有时会出现 2 个点同时消失的奇怪行为。

我的代码正确吗?

【问题讨论】:

    标签: physicsjs


    【解决方案1】:

    质量不能为零。如果你想尝试将质量设置得非常小。

    您还可能遇到渲染器视图未刷新的问题。这很简单,只需将每个主体上的.view 设置为null

    我还建议使用此处描述的一种策略使您的代码更通用: https://github.com/wellcaffeinated/PhysicsJS/wiki/Collisions

    这样,如果您在模拟中添加更多实体,它仍然可以工作。例如:

    myCatBody.label = 'cat;
    myDogBody.label = 'dog;
    
    // query to find a collision between a body with label "cat" and a body with label "dog"
    var query = Physics.query({
        $or: [
            { bodyA: { label: 'cat' }, bodyB: { label: 'dog' } }
            ,{ bodyB: { label: 'dog' }, bodyA: { label: 'cat' } }
        ]
    });
    
    // monitor collisions
    world.on('collisions:detected', function( data, e ){
        // find the first collision that matches the query
        var found = Physics.util.findOne( data.collisions, query );
        if ( found ){
            found.bodyA.mass *= 2;
            found.bodyA.geometry.radius *= 2;
            found.bodyB.mass = 0.001;
            found.bodyA.view = null;
            found.bodyB.view = null;
            found.bodyA.recalc();
            found.bodyB.recalc()
        }
    });
    

    【讨论】:

    • 碰撞后我需要 2 个点使 1 个更大。按照你的方式改变半径是行不通的。
    • 我的错误。您需要修改 .geometry.radius 属性。不是身体上的半径。如果您不需要 bodyB,那么请考虑在它与 world.remove() 碰撞后将其从世界中删除
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2020-10-29
    相关资源
    最近更新 更多