Cannon JS — 使用属性初始化 body 后的位置向量 NaN

Jac*_*ild 3 javascript physics three.js cannon.js

我正在使用简单的设置 Cannon.js,遵循在线示例,但是当我在构造函数中设置任何属性时,位置和角速度 x、y 和 z 都是 NaN。

这有效,但不会移动,因为身体没有质量。

const body = new CANNON.Body();
console.log(body.position.x, body.mass); //logs 0, 0
Run Code Online (Sandbox Code Playgroud)

然而,这并不...

const body = new CANNON.Body({
    mass: 1,
});
console.log(body.position.x, body.mass); //logs NaN, 1
Run Code Online (Sandbox Code Playgroud)

此外,如果我实例化身体,然后设置质量,它仍然不会移动。

更多的上下文代码(我在动画循环中调用更新函数,它正在发生)。

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);
}
Run Code Online (Sandbox Code Playgroud)

sch*_*ppe 5

我唯一能想到的就是你不小心传到delta = 0world.step。使用 Cannon.js v0.6.2 重现:JSFiddle

尝试将您的代码更改为:

export const update = (delta) => {
    if (delta > 0) {
        world.step(TIMESTEP * delta);
    }
}
Run Code Online (Sandbox Code Playgroud)