【发布时间】:2018-10-17 19:30:57
【问题描述】:
我将 Cannon.js 与 Three.js 一起使用。我已经设置了一个场景,其中有 5 列 4 块堆叠在一起。
我希望这些可以与我计划添加到场景中的其他对象进行交互。然而,列中的块似乎会导致大量的微碰撞,并且随着时间的推移,会出现位置抖动。我希望他们在与他们互动之前一直保持一致。
如果您查看 codepen 并等待大约 20/30 秒,您会看到积木开始移动。我需要在这些块上设置什么特定的东西来防止这种情况发生吗?
这是我整理的一个例子 - https://codepen.io/danlong/pen/XxZROj
顺便说一句,当场景中有这些我没有预料到的块时,性能也会有相当大的下降。我打算向场景中添加更多对象,但不确定性能下降的原因?
这与我的 animate() 循环中的以下内容有关吗?
this.world.step(1 / 30);
专门用于设置我的“大炮世界”和“列”的代码如下:
大炮世界:
this.world = new CANNON.World();
this.world.defaultContactMaterial.contactEquationStiffness = 1e6;
this.world.defaultContactMaterial.contactEquationRegularizationTime = 3;
this.world.solver.iterations = 20;
this.world.gravity.set(0,-25,0);
this.world.allowSleep = true;
this.world.broadphase = new CANNON.SAPBroadphase(this.world);
列:
var geometry = new THREE.BoxBufferGeometry(5,5,5);
var material = new THREE.MeshNormalMaterial();
var shape = new CANNON.Box(new CANNON.Vec3(5/2, 5/2, 5/2));
for (var rows = 0, yPos = 2.5; rows < 4; rows++, yPos+=5) {
for (var i = -20; i <= 20; i+=10) {
// physics
var body = new CANNON.Body({
mass: 0.5,
position: new CANNON.Vec3(i, yPos, 0),
friction: 0.1,
restitution: 0.3
});
body.allowSleep = true;
body.sleepSpeedLimit = 0.01;
body.sleepTimeLimit = 1.0;
body.addShape(shape);
this.world.addBody(body);
this.bodies.push(body);
// material
var mesh = new THREE.Mesh(geometry, material);
this.scene.add(mesh);
this.meshes.push(mesh);
}
}
【问题讨论】:
-
对于任何检查 Codepen 的人,批准的解决方案已在第 125 行实施。
标签: three.js game-physics cannon.js