//New 已测 /// <summary> /// 计算点到经过两点的直线的距离 /// </summary> /// <param name="sPos"></param> /// <param name="sPos1"></param> /// <param name="sPos2"></param> /// <returns></returns> public static FP DistanceFromPointToLine(TSVector2 sPos, TSVector2 sPos1, TSVector2 sPos2) { TSVector2 line1 = sPos2 - sPos1; TSVector2 line2 = sPos - sPos1; if (line1 == TSVector2.zero) { return(line2.magnitude); } return(TSMath.Abs(line2.x * line1.y - line2.y * line1.x) / line1.magnitude); }
//New 已测 /// <summary> /// 检测圆与线段是否相交 /// </summary> /// <param name="sOrgPos"></param> /// <param name="sOffset"></param> /// <param name="sCenter"></param> /// <param name="nRadius"></param> /// <param name="sCrossPoint"></param> /// <returns></returns> public static bool CheckCicleAndLine(TSVector2 sOrgPos, TSVector2 sOffset, TSVector2 sCenter, FP nRadius, out TSVector2 sCrossPoint) { //推导过程 //http://blog.csdn.net/rabbit729/article/details/4285119 sCrossPoint = sCenter; FP nDis = sOffset.magnitude; TSVector2 d = sOffset.normalized; TSVector2 e = sCenter - sOrgPos; FP a = (e.x * d.x + e.y * d.y); FP f = a * a + nRadius * nRadius - e.LengthSquared(); if (f >= 0) { FP s = TSMath.Sqrt(f); FP t1 = a - s; FP t2 = a + s; if (TSMath.Abs(t1) > TSMath.Abs(t2)) { FP fTemp = t1; t1 = t2; t2 = fTemp; } //射线原点在圆外 if ((t1 >= 0) && (t1 - nDis) <= 0) { sCrossPoint.x = sOrgPos.x + t1 * d.x; sCrossPoint.y = sOrgPos.y + t1 * d.y; return(true); } //这里说明射线原点在圆内 if (t2 >= 0) { //如果与圆有碰撞 if ((t2 - nDis) <= 0) { sCrossPoint.x = sOrgPos.x + t2 * d.x; sCrossPoint.y = sOrgPos.y + t2 * d.y; return(true); } //如果两个点都在圆内 else { sCrossPoint = sOrgPos; return(true); } } } return(false); }
//NEW已测 /// <summary> /// 检测一个点的xz平面投影是否处于2DAabb内 /// </summary> /// <param name="sCenter"></param> /// <param name="nHalfWidth"></param> /// <param name="nHalfHeight"></param> /// <param name="sPos"></param> /// <returns></returns> public static bool CheckAabbAndPos(TSVector2 sCenter, FP nHalfWidth, FP nHalfHeight, TSVector2 sPos) { TSVector2 sOffset = sPos - sCenter; if (TSMath.Abs(sOffset.x) > nHalfWidth) { return(false); } if (TSMath.Abs(sOffset.y) > nHalfHeight) { return(false); } return(true); }