我正在使用简单的设置 Cannon.js,遵循在线示例,但是当我在构造函数中设置任何属性时,位置和角速度 x、y 和 z 都是 NaN。
这可以工作,但不会移动,因为身体没有质量。
const body = new CANNON.Body();
console.log(body.position.x, body.mass); //logs 0, 0
然而,这并不...
const body = new CANNON.Body({
mass: 1,
});
console.log(body.position.x, body.mass); //logs NaN, 1
此外,如果我实例化身体,然后设置质量,它仍然不会移动。
更多上下文代码(我在动画循环中调用更新函数,并且一切正常)。
export const init = () => {
world = new CANNON.World();
world.gravity.set(0,1,0);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
for (let i = 0; i < BODIES_COUNT; i++) {
const shape = new CANNON.Box(new CANNON.Vec3(4,4,4));
const body = new CANNON.Body({
mass: 1,
});
const body = new CANNON.Body();
body.addShape(shape);
body.position.set(0, 0, 0);
body.mass = 1;
body.angularVelocity.set(0, 2, 0);
body.velocity.set(0, 1, 0);
body.angularDamping = 0.5;
world.addBody(body);
bodies.push(body);
const geometry = new THREE.BoxGeometry(10, 10, 10);
const material = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });
const mesh = new THREE.Mesh(geometry, material);
meshes.push(mesh);
}
}
export const update = (delta) => {
world.step(TIMESTEP * delta);
}
我唯一能想到的是你不小心把
delta = 0
传递到了world.step
。使用 Cannon.js v0.6.2 进行重现:JSFiddle
尝试将代码更改为:
export const update = (delta) => {
if (delta > 0) {
world.step(TIMESTEP * delta);
}
}
你也可以帮我吗?这是我的代码
//Render loop
const clock = new THREE.Clock();
let oldElapsedTime = 0;
function animate() {
//Delta
const elapsedTime = clock.getElapsedTime();
const deltaTime = elapsedTime - oldElapsedTime;
oldElapsedTime = elapsedTime;
//update physics world
world.step(1 / 60, deltaTime, 3);
sphere.position.copy(sphereBody.position);
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();