Threejs 通过 Raycaster 进行物体碰撞检测

参考:http://www.wukai.me/2015/12/28/threejs-collision/

1: 

创建Raycaster, 设置摄像机。

this.rayCaster = new THREE.Raycaster();

this.rayCaster.camera = camera;

 

2:

 this.spaceShip 是玩家操作的类,我创建了一个专门用于检测碰撞的THREE.BoxGeometry

 this.stoneList 是 需要被检测碰撞的物体 从屏幕里向外飞出,通过 new THREE.Sprite(new THREE.SpriteMaterial({ map: texture })); 创建

下面是检测碰撞的逻辑, 在update中每帧进行调用
private isCollision () {
    if(this.spaceShip && this.stoneList.length) {
        // 获取到用于检测碰撞的BoxGeometry的geometry;
        let geometry = this.spaceShip.crashGeometry;
        // 获取当前操作物体的坐标作为原点
        let originPoint = this.spaceShip.position.clone();

        // 遍历Geometry上每个顶点,用射线对其进行碰撞检测
        // @ts-ignore
        for (let vertexIndex = 0; vertexIndex < geometry.vertices.length; vertexIndex++) {
            // 顶点原始坐标
            // @ts-ignore
            let localVertex = geometry.vertices[vertexIndex].clone();
            // 顶点经过变换后的坐标
            let globalVertex = localVertex.applyMatrix4(this.spaceShip.matrix);
            let directionVector = globalVertex.sub(this.spaceShip.position);
            this.rayCaster.set(originPoint, directionVector.clone().normalize())

            let collisionResults = this.rayCaster.intersectObjects(this.stoneList);
            if (collisionResults.length > 0) {
                let dis = collisionResults[0].distance;
                let length = directionVector.length();
                // 发生碰撞
                if(dis < length) {
                    // @ts-ignore
                    let gameObjectId = collisionResults[0].object.parent.gameObjectID;
                    this.removeStone(gameObjectId);
                    let chance = UserData.i.chance - 1;
                    if(chance < 0) {
                        CommandManager.i.pushCommandInfo({
                            cmdName: CommandName.ReplaceSceneCmd,
                            isUpgrade: false
                        })
                    } else {
                        UserData.i.chance--;
                    }
                    break;
                }
            }
        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值