feat: CollisionDetector CircleCircle Projection

This commit is contained in:
Syntriax 2024-01-26 12:45:26 +03:00
parent f980546d37
commit 271a9a244b
1 changed files with 23 additions and 6 deletions

View File

@ -49,17 +49,34 @@ public class CollisionDetector : ICollisionDetector
{
collisionInformation = default;
Vector2D leftToRightCenter = left.CircleWorld.Center.FromTo(right.CircleWorld.Center);
float distanceCircleCenter = leftToRightCenter.Magnitude;
float radiusSum = left.CircleWorld.Radius + right.CircleWorld.Radius;
Vector2D leftToRightCenterProjectionVector = left.CircleWorld.Center.FromTo(right.CircleWorld.Center).Normalized;
float circleSurfaceDistance = distanceCircleCenter - radiusSum;
Projection leftProjection = left.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
Projection rightProjection = right.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
bool collision = circleSurfaceDistance <= 0f;
bool collision = leftProjection.Overlaps(rightProjection, out float depth);
if (collision)
collisionInformation = new(left, right, leftToRightCenter.Normalized, -circleSurfaceDistance);
collisionInformation = new(left, right, leftToRightCenterProjectionVector, depth);
return collision;
}
// private static bool DetectCircleCircle(ICircleCollider2D left, ICircleCollider2D right, out CollisionDetectionInformation? collisionInformation)
// {
// collisionInformation = default;
// Vector2D leftToRightCenter = left.CircleWorld.Center.FromTo(right.CircleWorld.Center);
// float distanceCircleCenter = leftToRightCenter.Magnitude;
// float radiusSum = left.CircleWorld.Radius + right.CircleWorld.Radius;
// float circleSurfaceDistance = distanceCircleCenter - radiusSum;
// bool collision = circleSurfaceDistance <= 0f;
// if (collision)
// collisionInformation = new(left, right, leftToRightCenter.Normalized, -circleSurfaceDistance);
// return collision;
// }
}