在两个点的碰撞后,我想让他们做一个双半径的点,所以我的代码
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()
})半径不变,有时两个点瞬间消失的奇怪行为。
我的代码正确吗?
发布于 2014-11-20 02:19:13
你不能有一个质量为零的。如果你想试着把质量设置得很小。
您可能还会遇到渲染器视图未刷新的问题。这很简单,只需将每个主体上的.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()
}
});https://stackoverflow.com/questions/26984994
复制相似问题