我正在做一个 3d 虚拟家庭项目。除了碰撞检测,一切都很好。我将 PointerLockControls 用于我的相机和移动。但我不确定如何检测每个可能方向的碰撞。为简单起见,我从 (0,0,0) 上的简单立方体的前向和后向碰撞开始:
rays = [
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0, 0, -1)
];
然后:
function detectCollision() {
var vector;
var projector = new THREE.Projector();
for (var i = 0; i < rays.length; i++) {
var vector = rays[i].clone();
projector.unprojectVector(vector, camera);
var rayCaster = new THREE.Raycaster(controls.getObject().position, vector.sub(controls.getObject().position).normalize());
var intersects = rayCaster.intersectObject(cube, true);
if (intersects.length > 0 && intersects[0].distance < 50) {
console.log(vector);
console.log(i, intersects);
$("#status").text("Collision detected @ " + rays[i].x + "," + rays[i].z +
"<br \>" + intersects[0].distance);
}
}
但是当我离我的立方体足够近时,控制台会显示两条光线都击中了立方体!所以为什么?我的光线有问题吗?vector(0,0,1) 应该是向后的,(0,0,-1) 应该是向前的,对吧?请帮助我,在我迷失在 3d 中之前!谢谢。