首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhysicsJS更改圆半径

PhysicsJS更改圆半径
EN

Stack Overflow用户
提问于 2014-11-18 09:16:47
回答 1查看 198关注 0票数 1

在两个点的碰撞后,我想让他们做一个双半径的点,所以我的代码

代码语言:javascript
复制
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()
})

半径不变,有时两个点瞬间消失的奇怪行为。

我的代码正确吗?

EN

回答 1

Stack Overflow用户

发布于 2014-11-20 02:19:13

你不能有一个质量为零的。如果你想试着把质量设置得很小。

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

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

这样,如果你添加更多的物体到你的模拟中,它仍然可以工作。例如:

代码语言:javascript
复制
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()
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26984994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档