1

After collision of 2 dots i want they make 1 dot with double radius so my code

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

Radius doesn't change and sometimes strange behavior that 2 dots dissapear at one moment.

Is my code correct?

4

1 回答 1

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()
    }
});
于 2014-11-19T18:19:13.470 回答