private void CollisionCapsuleCapsule() { CapsulePoints pointsA = GetCapsuleCollider(posA, rotA, halfSizeA.y * 2); CapsulePoints pointsB = GetCapsuleCollider(posB, rotB, halfSizeB.y * 2); CapsulePoints spheresA = GetCapsuleSpheres(pointsA.tipPoint, pointsA.basePoint, halfSizeA.x); CapsulePoints spheresB = GetCapsuleSpheres(pointsB.tipPoint, pointsB.basePoint, halfSizeB.x); float3 v0 = spheresB.basePoint - spheresA.basePoint; float3 v1 = spheresB.tipPoint - spheresA.basePoint; float3 v2 = spheresB.basePoint - spheresA.tipPoint; float3 v3 = spheresA.tipPoint - spheresA.tipPoint; float d0 = math.dot(v0, v0); float d1 = math.dot(v1, v1); float d2 = math.dot(v2, v2); float d3 = math.dot(v3, v3); float3 closestPointA = math.select(spheresA.basePoint, spheresA.tipPoint, d2 < d0 || d2 < d1 || d3 < d0 || d3 < d1); float3 closestPointB = ClosestPointOnLineSegement(spheresB.basePoint, spheresB.tipPoint, closestPointA); closestPointA = ClosestPointOnLineSegement(spheresA.basePoint, spheresA.tipPoint, closestPointB); float distance = math.distance(closestPointA, closestPointB); float penetrationDepth = halfSizeA.x + halfSizeB.x - distance; bool intersects = penetrationDepth > 0; if (intersects) { ResolveCapsuleCollision(ref posA, ref posB, closestPointA, closestPointB, halfSizeA.x, halfSizeB.x, distance); } }
private void CollisionSphereCapsule(ref float3 spherePos, float sphereRadius, ref float3 capsulePos, float3 capsuleRotation, float capsuleLength, float capsuleRadius) { CapsulePoints capsulePoints = GetCapsuleCollider(capsulePos, capsuleRotation, capsuleLength); CapsulePoints capsuleSpheres = GetCapsuleSpheres(capsulePoints.tipPoint, capsulePoints.basePoint, capsuleRadius); float3 closestPoint = ClosestPointOnLineSegement(capsuleSpheres.basePoint, capsuleSpheres.tipPoint, spherePos); float distance = math.distance(spherePos, closestPoint); float penetrationDepth = sphereRadius + capsuleRadius - distance; if (penetrationDepth > 0) { ResolveCapsuleCollision(ref spherePos, ref capsulePos, spherePos, closestPoint, sphereRadius, capsuleRadius, distance); } }