0

我正在尝试找到与墙壁碰撞的方向,使用 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

4

1 回答 1

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();
于 2014-04-24T09:32:37.810 回答