我正在尝试找到与墙壁碰撞的方向,使用 Raycaster 射线来确定玩家与墙壁的碰撞。我的播放器由按钮 WASD 控制,并改变 Y 轴的方向,我找到了一个碰撞示例http://webmaestro.fr/blog/basic-collisions-detection-with-three-js-raycaster/,但只有轴 X 和 Z
/**player move**/
if(kerk.controller.moveRight && this.moveX) this.player.translateX( moveDistance2 );
if(kerk.controller.moveLeft && this.moveX) this.player.translateX( -moveDistance2 );
if(kerk.controller.moveUp && this.moveZ) this.player.translateZ( -moveDistance );
if(kerk.controller.moveBottom && this.moveZ) this.player.translateZ( moveDistance2 );
....
/**rotation player**/
this.player.rotation.y += mouse.x > scCenter ? -this.camRot : this.camRot;
....
this.rays = [
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(1, 0, 1),
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(1, 0, -1),
new THREE.Vector3(0, 0, -1),
new THREE.Vector3(-1, 0, -1),
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(-1, 0, 1)
];
this.caster = new THREE.Raycaster();
.....
for (i = 0; i < this.rays.length; i += 1) {
但是玩家改变了Y轴的方向
如何找到 Raycaster 的 8 个侧面中的每一个的方向?
this.caster.set(this.player.position, this.rays[i]);
collisions = this.caster.intersectObjects(obstacles);
if (collisions.length > 0 && collisions[0].distance <= distance) {
if ((i === 0 || i === 1 || i === 7) && kerk.controller.moveUp === 1) {
this.moveZ = 0;
} else if ((i === 3 || i === 4 || i === 5) && kerk.controller.moveBottom === -1) {
this.moveZ = 0;
}
if ((i === 1 || i === 2 || i === 3) && kerk.controller.moveRight === 1) {
this.moveX = 0;
} else if ((i === 5 || i === 6 || i === 7) && kerk.controller.moveLeft === -1) {
this.moveX = 0;
}
}
}
当玩家改变 Y 轴的方向时,我只是不知道如何找到 8 边的方向
也许这是一个正确的例子,但它以一种奇怪的方式为我工作了 25%,也许我放弃了它,或者你需要一些东西来更新 Raycaster