在move移动的时候检测射线有没有碰撞到物体的时候就报错了
creator对3D支持非常差,当然对射线检测一样的差,现在是在搞cocos 3D去了,你检查一下是否使用正确物理系统-射线测试,在使用射线前提是开启物理和使用碰撞组件,如果只是简单的检查碰撞你可以使用简单的数学计算2个矩形是否相交就好了
我是要通过射线去检测没有碰撞到物体,因为我要做瞄准线,但是这个瞄准线有误差,去计算得到的路径,和实际物理自己反射的路径不一致,我就是把所有需要碰撞的物体弄成static了,我用的是2d的不是3d
// 剩余长度 const left_length = AIM_LINE_MAX_LENGTH - this._curLength; if (left_length <= 0) return; // 计算线的终点位置 const endLocation = startLocation.add(vector_dir.mul(left_length)); if (!this._physicsManager) { this.drawAimLine(startLocation, endLocation); return; } // 检测给定的线段穿过哪些碰撞体,可以获取到碰撞体在线段穿过碰撞体的那个点的法线向量和其他一些有用的信息。 this._results = this._physicsManager.rayCast(startLocation, endLocation, cc.RayCastType.Any); if (this._results && this._results.length > 0) { const result = this._results[0]; const comp: Part = result.collider.node.getComponent(Part); if (comp && comp.type === PART_TYPE.ELASTIC) { this.drawAimLine(startLocation, endLocation); } else { // 指定射线与穿过的碰撞体在哪一点相交。 const point = result.point; // 画入射线段 this.drawAimLine(startLocation, point); // 计算长度 const line_length = point.sub(startLocation).mag(); // 计算已画长度 this._curLength += line_length; // 指定碰撞体在相交点的表面的法线单位向量。 const vector_n = result.normal; // 入射单位向量 const vector_i = vector_dir; // 反射单位向量 const vector_r = vector_i.sub(vector_n.mul(2 * vector_i.dot(vector_n))); // 接着计算下一段 this.drawRayCast(point, vector_r); } } else { // 画剩余线段 this.drawAimLine(startLocation, endLocation); }
我是在touchmove的时候每次调用,去绘制路径,但是现在一检测到物体,这个射线就报错了,不知道为什么,是我代码的原因还是引擎的问题
private onTouchViewMove(evt: cc.Event.EventTouch) { if (!this._ball.active || this._isNextLevel) return; if (!LevelMgr.Ins.isStartLine) { const movePos = this.node.convertToNodeSpaceAR(evt.touch.getLocation()); LevelMgr.Ins.showLineBalls(this._startPos, movePos); } else { LevelMgr.Ins.hideLineBalls(); this._curLength = 0; const startLocation = evt.getStartLocation(); const location = evt.getLocation(); // 计算射线 this.drawRayCast(startLocation, location.subSelf(startLocation).normalizeSelf()); } }
