我正在尝试使用raycaster检测碰撞,如示例"碰撞检测" http://stemkoski.github.io/Three.js/的{{3}},但无法得到正确的结果。我的代码如下:
var material_blue = new THREE.MeshPhongMaterial({
color: 0x0000ff,
specular: 0x000000,
shininess: 10
});
var dest_pos = new THREE.Vector3(0, 0, 3);
cube = new THREE.Mesh(new THREE.CubeGeometry(2, 2, 2), material_blue);
cube.position.set(dest_pos.x, dest_pos.y, dest_pos.z);
scene.add(cube);
var collidableMeshList = []
collidableMeshList.push(cube)
var orig_pos = new THREE.Vector3(0.5, 0.5, -1);
let dir = dest_pos.clone();
dir.sub(orig_pos);
console.log("dir: " + dir.x + ", " + dir.y + ", " + dir.z)
let raycaster = new THREE.Raycaster(orig_pos, dir.clone().normalize());
let intersects = raycaster.intersectObjects(collidableMeshList);
console.log(intersects);
console.log("intersects.length = " + intersects.length);
if (intersects.length > 0) {
console.log("intersects[0].point: (" + intersects[0].point.x + ", " + intersects[0].point.y + ", " + intersects[0].point.z + ")")
}

在我的代码中,我在dest_pos(0,0,3)处放置了一个大小为(2,2,2)的立方体,并从orig_pos(0.5,0.5,-1)发送了一条光线。理论上,我应该得到(0.5,0.5,2),但最后,我得到的是(0.5,0.5,-1),这正是orig_pos。我尝试使用stackoverflow上建议的不同方法解决问题,但都未能获得正确的结果。 欢迎任何建议。 提前谢谢!