void OnDrawGizmos() { if (closeLine) { return; } Gizmos.color = lineColor; if (Application.isPlaying) { Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorToVector3(basePosition), ToolMethod.Config2Render(baseRadiusCon)); } else { Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorConToVector3(baseCenter), ToolMethod.Config2Render(baseRadiusCon)); } if (fixPosition) { int _posX = (int)ToolMethod.Render2Config(transform.position.x) + 25; int _posY = (int)ToolMethod.Render2Config(transform.position.z) + 25; _posX = _posX - _posX % 50; _posY = _posY - _posY % 50; baseCenter.x = _posX; baseCenter.y = _posY; transform.position = ToolGameVector.ChangeGameVectorConToVector3(baseCenter); fixPosition = false; } }
//圆和多边形碰撞 public static bool CollideCircleAndPolygon(GameVector2 _c1, int _radius1, GameVec2Con[] vectexs, int[] normalDir) { Vector3 _circleCenter = ToolGameVector.ChangeGameVectorToVector3(_c1); bool isAllIn = true;//是否都在多边形线段的右侧 for (int i = 0; i < vectexs.Length; i++) { Vector3 lineBegin = ToolGameVector.ChangeGameVectorConToVector3(vectexs [i]); int endIndex = (i + 1) % vectexs.Length; Vector3 lineEnd = ToolGameVector.ChangeGameVectorConToVector3(vectexs [endIndex]); Vector3 _circleVec = _circleCenter - lineBegin; Vector3 _line = lineEnd - lineBegin; Vector3 _cross = Vector3.Cross(_circleVec, _line); if (_cross.z < 0) { isAllIn = false; //在线段的左侧,即多边形的外侧 Vector3 vecProj = Vector3.Project(_circleVec, _line); //投影点 float disLine = _line.magnitude; float proj_begin = vecProj.magnitude; float proj_end = (vecProj - _line).magnitude; float projlengh = proj_begin + proj_end; if ((disLine + 1) >= projlengh) { //投影在线段上 int dis = (int)Mathf.Sqrt(_circleVec.sqrMagnitude - vecProj.sqrMagnitude); int disRadius = _radius1 / 100; if (dis < disRadius) { return(true); } else { return(false); } } else { //投影不在当前线段上 if (CollidePointAndCircle(ChangeGameVectorConToGameVector2(vectexs [i]), _c1, _radius1)) { return(true); } else { return(CollidePointAndCircle(ChangeGameVectorConToGameVector2(vectexs [endIndex]), _c1, _radius1)); } } } } if (isAllIn) { //全在内侧,圆心在多边形内 return(true); } return(false); }
public virtual void Logic_UpdateMoveDir(int _dir) { if (_dir > 120) { logicSpeed = GameVector2.zero; } else { roleDirection = _dir * 3; logicSpeed = moveSpeed * BattleData.Instance.GetSpeed(roleDirection); Vector3 _renderDir = ToolGameVector.ChangeGameVectorToVector3(logicSpeed); renderDir = Quaternion.LookRotation(_renderDir); } }
void OnDrawGizmos() { if (closeLine) { return; } Vector3 _center; if (Application.isPlaying) { _center = ToolGameVector.ChangeGameVectorToVector3(basePosition); } else { _center = ToolGameVector.ChangeGameVectorConToVector3(baseCenter); } if (!hideCircle) { Gizmos.color = Color.blue; Gizmos.DrawWireSphere(_center, ToolMethod.Config2Render(baseRadiusCon)); } Gizmos.color = lineColor; float _width = ToolMethod.Config2Render(halfWidthCon); float _heigh = ToolMethod.Config2Render(halfHeightCon); Vector3[] topVec = new Vector3[4]; topVec [0] = _center + new Vector3(-_width, 0, -_heigh); topVec [1] = _center + new Vector3(-_width, 0, _heigh); topVec [2] = _center + new Vector3(_width, 0, _heigh); topVec [3] = _center + new Vector3(_width, 0, -_heigh); Gizmos.DrawLine(topVec[0], topVec[1]); Gizmos.DrawLine(topVec[1], topVec[2]); Gizmos.DrawLine(topVec[2], topVec[3]); Gizmos.DrawLine(topVec[3], topVec[0]); if (updateRadius) { baseRadiusCon = (int)Mathf.Sqrt(halfWidthCon * halfWidthCon + halfHeightCon * halfHeightCon) + 1; updateRadius = false; } if (fixPosition) { int _posX = (int)ToolMethod.Render2Config(transform.position.x) + 25; int _posY = (int)ToolMethod.Render2Config(transform.position.z) + 25; _posX = _posX - _posX % 50; _posY = _posY - _posY % 50; baseCenter.x = _posX; baseCenter.y = _posY; transform.position = ToolGameVector.ChangeGameVectorConToVector3(baseCenter); fixPosition = false; } }
void OnDrawGizmos() { if (closeLine) { return; } if (!hideCircle) { Gizmos.color = Color.blue; if (Application.isPlaying) { Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorToVector3(basePosition), baseRadiusCon); Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorToVector3(basePosition), colliRadiusCon); } else { Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorConToVector3(baseCenter), baseRadiusCon); Gizmos.DrawWireSphere(ToolGameVector.ChangeGameVectorConToVector3(baseCenter), colliRadiusCon); } } if (updateVertexs || fixPosition) { vertexsCon = new GameVec2Con[vertexs_delta.Length]; normalDir = new int[vertexs_delta.Length]; float radius = 0; for (int i = 0; i < vertexs_delta.Length; i++) { vertexsCon [i].x = baseCenter.x + vertexs_delta [i].x; vertexsCon [i].y = baseCenter.y + vertexs_delta [i].y; Vector3 _dv = ToolGameVector.ChangeGameVectorConToVector3(vertexs_delta [i]); radius = Mathf.Max(radius, _dv.magnitude); //法向 Vector3 _dir; if (i == vertexs_delta.Length - 1) { _dir = ToolGameVector.ChangeGameVectorConToVector3(vertexs_delta [0]); } else { _dir = ToolGameVector.ChangeGameVectorConToVector3(vertexs_delta [i + 1]); } Vector3 _line = _dir - _dv; int _linedir = (int)Mathf.Sign(_line.y) * Mathf.RoundToInt(Vector3.Angle(_line, new Vector3(1f, 0f, 0f))); normalDir [i] = (int)Mathf.Repeat(_linedir + 90, 360); } baseRadiusCon = (int)radius + 1; } Gizmos.color = lineColor; for (int i = 0; i < vertexsCon.Length; i++) { Vector3 lineBegin; Vector3 lineEnd; if (i == vertexsCon.Length - 1) { lineBegin = ToolGameVector.ChangeGameVectorConToVector3(vertexsCon [i]); lineEnd = ToolGameVector.ChangeGameVectorConToVector3(vertexsCon [0]); } else { lineBegin = ToolGameVector.ChangeGameVectorConToVector3(vertexsCon [i]); lineEnd = ToolGameVector.ChangeGameVectorConToVector3(vertexsCon [i + 1]); } Gizmos.DrawLine(lineBegin, lineEnd); Vector3 _normalBegin = (lineBegin + lineEnd) * 0.5f; Vector3 _normalEnd = _normalBegin + new Vector3(100f * Mathf.Cos(Mathf.Deg2Rad * normalDir [i]), 100f * Mathf.Sin(Mathf.Deg2Rad * normalDir [i])); Gizmos.DrawLine(_normalBegin, _normalEnd); } if (fixPosition) { int _posX = (int)transform.position.x + 25; int _posY = (int)transform.position.y + 25; _posX = _posX - _posX % 50; _posY = _posY - _posY % 50; baseCenter.x = _posX; baseCenter.y = _posY; transform.position = new Vector3(_posX * 1.0f, _posY * 1.0f); fixPosition = false; } }
/// <summary> /// 圆和正圆扇面的碰撞,带位置修正 /// </summary> /// <returns><c>true</c>, if circle and arc was collided, <c>false</c> otherwise.</returns> /// <param name="_c1">圆心.</param> /// <param name="_radius1">圆半径.</param> /// <param name="_arcCen">扇面圆心.</param> /// <param name="_arcRadius">扇面所在圆半径.</param> /// <param name="_arcAngle">角度.</param> /// <param name="_arcAngleSize">角度范围.</param> /// <param name="_amend">修正值.</param> public static bool CollideCircleAndArcArea(GameVector2 _c1, int _radius1, GameVector2 _arcCen, int _arcRadius, int _arcAngle, int _arcAngleSize, out GameVector2 _amend) { _amend = GameVector2.zero; int radiusSum = _radius1 + _arcRadius; long sqrRadiusSum = radiusSum * radiusSum; long sqrDistance = _c1.sqrMagnitude(_arcCen); if (sqrDistance >= sqrRadiusSum) { return(false); } int _angle = (_c1 - _arcCen).Angle(); int _point1Angle = _arcAngle + _arcAngleSize; int _point2Angle = _arcAngle - _arcAngleSize; bool inArc; if (_point2Angle < 0) { int _angle1 = (int)Mathf.Repeat(_angle - _point2Angle, 360f); if (_angle1 >= 0 && _angle1 <= (_point1Angle - _point2Angle)) { inArc = true; } else { inArc = false; } } else if (_point1Angle > 360) { int _delA = _point1Angle - 360; int _angle1 = (int)Mathf.Repeat(_angle - _delA, 360f); if (_angle1 >= (_point2Angle - _delA) && _angle1 <= 360) { inArc = true; } else { inArc = false; } } else { if (_angle >= _point2Angle && _angle <= _point1Angle) { inArc = true; } else { inArc = false; } } if (inArc) { //在圆弧内,需要修正 int distance = (int)Mathf.Sqrt(sqrDistance); int amendDis = 1 + (int)(radiusSum - distance) / 100; _amend = amendDis * BattleData.Instance.GetSpeed(_angle); return(true); } //线段修正 Vector3 _circleCenter = ToolGameVector.ChangeGameVectorToVector3(_c1); Vector3 _arcAreaCenter = ToolGameVector.ChangeGameVectorToVector3(_arcCen); _point1Angle = (int)Mathf.Repeat(_point1Angle, 360); GameVector2 _point1 = _arcCen + (_arcRadius / 100) * BattleData.Instance.GetSpeed(_point1Angle); _point2Angle = (int)Mathf.Repeat(_point2Angle, 360); GameVector2 _point2 = _arcCen + (_arcRadius / 100) * BattleData.Instance.GetSpeed(_point2Angle); Vector3 _line1Point = ToolGameVector.ChangeGameVectorToVector3(_point1); Vector3 _line2Point = ToolGameVector.ChangeGameVectorToVector3(_point2); //线段1修正 bool amendline1 = false; GameVector2 amendLine1P = GameVector2.zero; Vector3 _circleVec = _circleCenter - _arcAreaCenter; Vector3 _line = _line1Point - _arcAreaCenter; Vector3 _cross = Vector3.Cross(_circleVec, _line); if (_cross.z <= 0) { //在线段的左侧,即多边形的外侧 Vector3 vecProj = Vector3.Project(_circleVec, _line); //投影点 float disLine = _line.magnitude; float proj_begin = vecProj.magnitude; float proj_end = (vecProj - _line).magnitude; float projlengh = proj_begin + proj_end; if ((disLine + 1) >= projlengh) { //投影在线段上 int dis = (int)Mathf.Sqrt(_circleVec.sqrMagnitude - vecProj.sqrMagnitude); int disRadius = _radius1 / 100; if (dis < disRadius) { //需要修正 amendline1 = true; int amendDis = 1 + (int)(_radius1 / 100 - dis); Vector3 angelVec = _circleVec - vecProj; float angle1 = Mathf.Atan2(angelVec.y, angelVec.x) * Mathf.Rad2Deg; int amendAngle = (int)Mathf.Repeat(angle1, 360f); amendLine1P = amendDis * BattleData.Instance.GetSpeed(amendAngle); } } else { //投影不在当前线段上 } } //线段2修正 bool amendline2 = false; GameVector2 amendLine2P = GameVector2.zero; Vector3 _circleVec2 = _circleCenter - _line2Point; Vector3 _line2 = _arcAreaCenter - _line2Point; Vector3 _cross2 = Vector3.Cross(_circleVec2, _line2); if (_cross2.z <= 0) { //在线段的左侧,即多边形的外侧 Vector3 vecProj = Vector3.Project(_circleVec2, _line2); //投影点 float disLine = _line2.magnitude; float proj_begin = vecProj.magnitude; float proj_end = (vecProj - _line2).magnitude; float projlengh = proj_begin + proj_end; if ((disLine + 1) >= projlengh) { //投影在线段上 int dis = (int)Mathf.Sqrt(_circleVec2.sqrMagnitude - vecProj.sqrMagnitude); int disRadius = _radius1 / 100; if (dis < disRadius) { //需要修正 amendline2 = true; int amendDis = 1 + (int)(_radius1 / 100 - dis); Vector3 angelVec = _circleVec2 - vecProj; float angle1 = Mathf.Atan2(angelVec.y, angelVec.x) * Mathf.Rad2Deg; int amendAngle = (int)Mathf.Repeat(angle1, 360f); amendLine2P = amendDis * BattleData.Instance.GetSpeed(amendAngle); } } else { //投影不在当前线段上 } } if (amendline1 || amendline2) { _amend = amendLine1P + amendLine2P; return(true); } //对点修正 GameVector2 _outAmend1; bool _result1 = ToolGameVector.CollidePointAndCircle(_c1, _arcCen, _radius1, out _outAmend1); if (_result1) { _amend = _outAmend1; return(true); } GameVector2 _outAmend2; bool _result2 = ToolGameVector.CollidePointAndCircle(_c1, _point1, _radius1, out _outAmend2); GameVector2 _outAmend3; bool _result3 = ToolGameVector.CollidePointAndCircle(_c1, _point2, _radius1, out _outAmend3); if (_result2 || _result3) { _amend = _outAmend2 + _outAmend3; return(true); } return(false); }
/// <summary> /// 圆和多边形的碰撞 /// </summary> /// <returns><c>true</c>, if circle and polygon was collided, <c>false</c> otherwise.</returns> /// <param name="_c1">圆心.</param> /// <param name="_radius1">圆半径.</param> /// <param name="vectexs">多边形顶点.</param> /// <param name="normalDir">多边形每条边的法向量.</param> /// <param name="_amend">位置修正.</param> public static bool CollideCircleAndPolygon(GameVector2 _c1, int _radius1, GameVec2Con[] vectexs, int[] normalDir, out GameVector2 _amend) { _amend = GameVector2.zero; Vector3 _circleCenter = ToolGameVector.ChangeGameVectorToVector3(_c1); for (int i = 0; i < vectexs.Length; i++) { Vector3 lineBegin = ToolGameVector.ChangeGameVectorConToVector3(vectexs [i]); int endIndex = (i + 1) % vectexs.Length; Vector3 lineEnd = ToolGameVector.ChangeGameVectorConToVector3(vectexs [endIndex]); Vector3 _circleVec = _circleCenter - lineBegin; Vector3 _line = lineEnd - lineBegin; Vector3 _cross = Vector3.Cross(_circleVec, _line); if (_cross.z < 0) { //在线段的左侧,即多边形的外侧 Vector3 vecProj = Vector3.Project(_circleVec, _line); //投影点 float disLine = _line.magnitude; float proj_begin = vecProj.magnitude; float proj_end = (vecProj - _line).magnitude; float projlengh = proj_begin + proj_end; if ((disLine + 1) >= projlengh) { //投影在线段上 int dis = (int)Mathf.Sqrt(_circleVec.sqrMagnitude - vecProj.sqrMagnitude); int disRadius = _radius1 / 100; if (dis < disRadius) { int amendDis = 1 + (int)(_radius1 / 100 - dis); _amend = amendDis * BattleData.Instance.GetSpeed(normalDir [i]); return(true); } else { return(false); } } else { Vector3 newLineBegin; Vector3 newLineEnd; //投影不在当前线段上 bool isSameDir = Vector3.Dot(vecProj, _line) > 0f ? true : false; //为0的时候是垂直,不会出现该情况 int normalIndex; int linePointIndex; //2条线段的交点 if (isSameDir) { //同向 newLineBegin = lineEnd; int newEndIndex = (i + 2) % vectexs.Length; newLineEnd = ToolGameVector.ChangeGameVectorConToVector3(vectexs [newEndIndex]); normalIndex = endIndex; linePointIndex = endIndex; } else { //反向 if (i == 0) { newLineBegin = ToolGameVector.ChangeGameVectorConToVector3(vectexs [vectexs.Length - 1]); } else { newLineBegin = ToolGameVector.ChangeGameVectorConToVector3(vectexs [i - 1]); } newLineEnd = lineBegin; normalIndex = vectexs.Length - 1; linePointIndex = i; } Vector3 newCircleVec = _circleCenter - newLineBegin; Vector3 _newline = newLineEnd - newLineBegin; Vector3 newVecProj = Vector3.Project(newCircleVec, _newline); //投影点 float newdisLine = _newline.magnitude; float newproj_begin = newVecProj.magnitude; float newproj_end = (newVecProj - _newline).magnitude; float newprojlengh = newproj_begin + newproj_end; if ((newdisLine + 1) >= newprojlengh) { //投影在线段上 int dis = (int)Mathf.Sqrt(newCircleVec.sqrMagnitude - newVecProj.sqrMagnitude); int disRadius = _radius1 / 100; if (dis < disRadius) { int amendDis = 1 + (int)(_radius1 / 100 - dis); _amend = amendDis * BattleData.Instance.GetSpeed(normalDir [normalIndex]); return(true); } else { return(false); } } else { bool isNewSameDir = Vector3.Dot(newVecProj, _newline) > 0f ? true : false; if (isNewSameDir != isSameDir) { //夹角处 GameVector2 _point = ToolGameVector.ChangeGameVectorConToGameVector2(vectexs [linePointIndex]); GameVector2 _outAmend; bool _result = ToolGameVector.CollidePointAndCircle(_c1, _point, _radius1, out _outAmend); _amend = _outAmend; return(_result); } } } } } return(false); }
//获取渲染坐标 public Vector3 GetPositionVec3(float _y = 0f) { return(ToolGameVector.ChangeGameVectorToVector3(basePosition, _y)); }