cocos 获取物理碰撞瞄准线和两个向量的夹角

/**
     * 获取小球碰撞瞄准线和预碰撞点
     * @param {*} cha_location 
     * @param {*} get_location 
     */
    rayTest (cha_location, get_location) {
        // 获取落地的世界坐标
        var louDi = this.layer_game.parent.convertToWorldSpaceAR(this.hero_luoDi_location);
        
        // 放大 物理碰撞点的瞄准线
        var zz = cc.v2(get_location.x - louDi.x, get_location.y - louDi.y);
        get_location.x = get_location.x + zz.x * this.speed;
        get_location.y = get_location.y + zz.y * this.speed;
        
        
        // 获取两个点直接的路径会碰撞到的物理组件以及碰撞点、法线
        // 这里的坐标必须是世界坐标
        var results = cc.director.getPhysicsManager().rayCast(louDi, get_location, cc.RayCastType.AllClosest);
        var luJin = 100000;
        var pointResult = null;
        // console.log('rayTest', results)
        for (var i = 0; i < results.length; i++) {
            //两点之间检测出来的点的数组
            var result = results[i];
            if(result.collider.node._name == 'block_12') {
                continue;
            }

            // 获取最短路径的碰撞点
            if(luJin > Math.abs(result.point.x-louDi.x) + Math.abs(result.point.y-louDi.y)) {
                luJin = Math.abs(result.point.x-louDi.x) + Math.abs(result.point.y-louDi.y)
                pointResult = result;
            }
        }

        if(pointResult) {
            // 计算夹角
            var jiao = this.vectorsToDegree(cha_location, pointResult.normal);

            var point = this.layer_game.convertToNodeSpaceAR(pointResult.point);
            // this.setLint(point, cc.v2(point.x + pointResult.normal.x*500, point.y + pointResult.normal.y*500), jiao)

            var cha_point = cc.v2(point.x - this.hero_luoDi_location.x, point.y - this.hero_luoDi_location.y);
            this.node_line.width = Math.sqrt(cha_point.x*cha_point.x + cha_point.y*cha_point.y);

            this.line_zhe.setPosition(this.layer_game.convertToNodeSpaceAR(pointResult.point))
            this.line_zhe.active = true;
            this.line_zhe.angle = this.node_line.angle-180 + jiao*2;
        } else {
            this.line_zhe.active = false;
        }
    },
    /**
     * 获取两个向量的夹角
     * @param {cc.v2} comVec 向量1
     * @param {cc.v2} dirVec 向量2
     * @returns 
     */
    vectorsToDegree(comVec, dirVec) {
        // 求方向向量与对比向量间的弧度
        let radian = dirVec.signAngle(comVec);
        // 将弧度转换为角度
        let degree = cc.misc.radiansToDegrees(radian);
        if(degree < -90) {
            degree = -90 - (90 + degree);
        }

        if(degree > 90) {
            degree = 180 - degree;
        }
        return degree;
    },

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值