我正在尝试找到与墙碰撞的方向,使用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
答案 0 :(得分:0)
我不确定这是否有帮助,但以防小数学刷新:
要计算方向,需要两个点,(与维数无关),例如p1和p2。因此,创建了一个矢量v1,这是一个矢量,而不是一个方向,可以将其转换为您需要将其标准化的方向。
在three.js中你会做类似的事情:
var p1 = new THREE.Vector3(x1, y1, z1);
var p2 = new THREE.Vector3(x2, y2, z2);
var v = new THREE.Vector3();
v.subVectors(p2, p1);
v.normalize();