/**
* 获取小球碰撞瞄准线和预碰撞点
* @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;
},