胶囊体边界点的计算及获取( 二 )

<= 180f; i += 9f){var point1 = up * remainHeightHalf + Quaternion.AngleAxis(i, -right) * forward * radius;var point2 = up * remainHeightHalf + Quaternion.AngleAxis(i, forward) * right * radius;Gizmos.DrawLine(center + lastPoint1.GetValueOrDefault(point1), center + point1);Gizmos.DrawLine(center + lastPoint2.GetValueOrDefault(point2), center + point2);lastPoint1 = point1;lastPoint2 = point2;}lastPoint1 = (Vector3?)null;for (var i = 0f; i <= 360f; i += 9f){var point1 = up * remainHeightHalf + Quaternion.AngleAxis(i, up) * forward * radius;Gizmos.DrawLine(center + lastPoint1.GetValueOrDefault(point1), center + point1);lastPoint1 = point1;}Gizmos.color = cacheColor;Gizmos.matrix = cacheMatrix;}bool BetweenLineAndSphere(Vector3 circleCenter, float circleRadius,Vector3 point1, Vector3 point2,out Vector3 intersection1, out Vector3 intersection2){float t;var dx = point2.x - point1.x;var dy = point2.y - point1.y;var dz = point2.z - point1.z;var a = dx * dx + dy * dy + dz * dz;var b = 2 * (dx * (point1.x - circleCenter.x) + dy * (point1.y - circleCenter.y) + dz * (point1.z - circleCenter.z));var c = (point1.x - circleCenter.x) * (point1.x - circleCenter.x)+ (point1.y - circleCenter.y) * (point1.y - circleCenter.y)+ (point1.z - circleCenter.z) * (point1.z - circleCenter.z) - circleRadius * circleRadius;var determinate = b * b - 4 * a * c;// Two solutions.t = (float)((-b + Mathf.Sqrt(determinate)) / (2 * a));intersection1 = new Vector3(point1.x + t * dx, point1.y + t * dy, point1.z + t * dz);t = (float)((-b - Mathf.Sqrt(determinate)) / (2 * a));intersection2 = new Vector3(point1.x + t * dx, point1.y + t * dy, point1.z + t * dz);if (intersection1.normalized == Vector3.zero && intersection2.normalized == Vector3.zero)return false;elsereturn true;}bool CylinderVSline(Vector3 start, Vector3 dir, Vector3 aVec, Vector3 bVec, float radius, out Vector3 intersectPoint){intersectPoint = Vector3.zero;// Solution : http://www.gamedev.net/community/forums/topic.asp?topic_id=467789var ab = bVec - aVec;var ao = start - aVec;var aoxAb = Vector3.Cross(ao, ab);var vxAb = Vector3.Cross(dir, ab);var ab2 = Vector3.Dot(ab, ab);var a = Vector3.Dot(vxAb, vxAb);var b = 2 * Vector3.Dot(vxAb, aoxAb);var c = Vector3.Dot(aoxAb, aoxAb) - (radius * radius * ab2);var d = b * b - 4 * a * c;if (d < 0) return false;float time = (-b - Mathf.Sqrt(d)) / (2 * a);if (time < 0) return false;intersectPoint = start + dir * time;var projectionPoint = aVec + Vector3.Project(intersectPoint - aVec, ab.normalized);var distance1 = Mathf.Max(Vector3.Distance(projectionPoint, aVec), Vector3.Distance(projectionPoint, bVec));var distance2 = Vector3.Distance(aVec, bVec);if (distance1 >= distance2)return false;return true;}}
View Code