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); }
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; } Gizmos.color = lineColor; Vector3 baseVec3 = ToolGameVector.ChangeGameVectorConToVector3(baseCenter); Gizmos.DrawWireSphere(baseVec3, baseRadiusCon); Vector3 arcCenter = baseVec3 + ToolGameVector.ChangeGameVectorConToVector3(arcCenterCon); if (showArcCircle) { Gizmos.DrawWireSphere(arcCenter, arcRadiusCon); } int vectNumber = arcAngleSize / 3; Vector3 firstVect1 = arcCenter + new Vector3(Mathf.Cos(Mathf.Deg2Rad * arcAngle) * arcRadiusCon, Mathf.Sin(Mathf.Deg2Rad * arcAngle) * arcRadiusCon, 0); Vector3 firstVect2 = firstVect1; // Gizmos.color = Color.blue; for (int i = 1; i <= vectNumber; i++) { int angle1 = arcAngle + 3 * i; int angle2 = arcAngle - 3 * i; if (i == vectNumber) { angle1 = arcAngle + arcAngleSize; angle2 = arcAngle - arcAngleSize; } float fAngle1 = Mathf.Deg2Rad * angle1; float fAngle2 = Mathf.Deg2Rad * angle2; Vector3 vectPos1 = arcCenter + new Vector3(Mathf.Cos(fAngle1) * arcRadiusCon, Mathf.Sin(fAngle1) * arcRadiusCon, 0); Gizmos.DrawLine(firstVect1, vectPos1); firstVect1 = vectPos1; Vector3 vectPos2 = arcCenter + new Vector3(Mathf.Cos(fAngle2) * arcRadiusCon, Mathf.Sin(fAngle2) * arcRadiusCon, 0); Gizmos.DrawLine(firstVect2, vectPos2); firstVect2 = vectPos2; } 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; } }
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 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); }