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


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

文章插图
(注意 取的是中心点位置来计算边界点,而非投影点位置 。可以自行修改为投影点)
之前想做一些体积碰撞的效果但是遇到点问题,胶囊边界点检测这个涉及到球体的交点检测以及圆柱的交点检测
胶囊体边界点的计算及获取

文章插图
觉得可以放上来,相交检测的代码逻辑来源于网络 。
【胶囊体边界点的计算及获取】using System.Collections;using System.Collections.Generic;using UnityEngine;public class CapsuleTest : MonoBehaviour{public enum DirectionEnum { X, Y, Z }public Vector3 center;public float height = 1f;public float radius = 1f;public DirectionEnum direction = DirectionEnum.Y;public Transform testOutsidePoint;Vector3 Test(Vector3 outsidePoint){var localMassPoint = center;var localOutsidePoint = transform.worldToLocalMatrix.MultiplyPoint3x4(outsidePoint);var centerPoint = localMassPoint + center;var up = Vector3.up;var right = Vector3.right;var forward = Vector3.forward;switch (direction){case DirectionEnum.X:up = Vector3.right;right = Vector3.forward;forward = Vector3.up;break;case DirectionEnum.Z:up = Vector3.forward;right = Vector3.up;forward = Vector3.right;break;}var remainHeight = Mathf.Max(height - radius * 2f, 0) + 0.01f;var remainHeightHalf = remainHeight * 0.5f;var pillarOrigin = centerPoint + up * -remainHeightHalf;var pillarEnd = centerPoint + up * remainHeightHalf;var insectPoint = Vector3.zero;var isHitCylinder = CylinderVSline(localOutsidePoint, (centerPoint - localOutsidePoint).normalized, pillarOrigin, pillarEnd, radius, out insectPoint);if (isHitCylinder){return transform.localToWorldMatrix.MultiplyPoint3x4(insectPoint);}else{var spherePoint1 = centerPoint + up * remainHeightHalf;var spherePoint2 = centerPoint + (-up) * remainHeightHalf;var finalSpherePoint = spherePoint1;if (Vector3.Distance(localOutsidePoint, spherePoint2) < Vector3.Distance(localOutsidePoint, spherePoint1)){finalSpherePoint = spherePoint2;}BetweenLineAndSphere(finalSpherePoint, radius, centerPoint, localOutsidePoint, out spherePoint1, out spherePoint2);if (Vector3.Distance(spherePoint1, centerPoint) > Vector3.Distance(spherePoint2, centerPoint))return transform.localToWorldMatrix.MultiplyPoint3x4(spherePoint1);elsereturn transform.localToWorldMatrix.MultiplyPoint3x4(spherePoint2);}}void OnDrawGizmos(){var cacheColor = Gizmos.color;var cacheMatrix = Gizmos.matrix;if (testOutsidePoint != null){var edgePoint = Test(testOutsidePoint.position);Gizmos.DrawWireSphere(edgePoint, 0.2f);}Gizmos.color = Color.Lerp(Color.green, Color.gray, 0.5f);Gizmos.matrix = transform.localToWorldMatrix;var remainHeight = Mathf.Max(height - radius * 2f, 0);var remainHeightHalf = remainHeight * 0.5f;var up = Vector3.up;var right = Vector3.right;var forward = Vector3.forward;switch (direction){case DirectionEnum.X:up = Vector3.right;right = Vector3.forward;forward = Vector3.up;break;case DirectionEnum.Z:up = Vector3.forward;right = Vector3.up;forward = Vector3.right;break;}var lastPoint1 = (Vector3?)null;var lastPoint2 = (Vector3?)null;for (var i = 0f; i <= 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.DrawLine(center + forward * radius + up * -remainHeightHalf, center + forward * radius + up * remainHeightHalf);Gizmos.DrawLine(center + -forward * radius + up * -remainHeightHalf, center + -forward * radius + up * remainHeightHalf);Gizmos.DrawLine(center + right * radius + up * -remainHeightHalf, center + right * radius + up * remainHeightHalf);Gizmos.DrawLine(center + -right * radius + up * -remainHeightHalf, center + -right * radius + up * remainHeightHalf);lastPoint1 = (Vector3?)null;lastPoint2 = (Vector3?)null;for (var i = 0f; i