Mejora en la normalización de ángulos en la clase Aether: se implementó un manejo más robusto de los ángulos para casos donde el arco cruza el límite 0/2π, asegurando un cálculo preciso de la superposición y la distancia a los bordes.

This commit is contained in:
Miguel 2025-06-13 20:59:09 +02:00
parent 380bc14b69
commit 9c4e87be9b
1 changed files with 45 additions and 10 deletions

View File

@ -138,23 +138,58 @@ namespace CtrEditor.Simulacion
Vector2 centerToBottle = bottle.Body.Position - Body.Position;
// Calculate angle of bottle relative to curve center (in radians)
float bottleAngle = (float)Math.Atan2(centerToBottle.Y, centerToBottle.X);
// Normalize angle to be positive
if (bottleAngle < 0)
bottleAngle += 2 * (float)Math.PI;
// If bottle is outside the angle range, return 0
if (bottleAngle < _startAngle || bottleAngle > _endAngle)
return 0;
// Normalize all angles to the same range [0, 2π]
float normalizedBottleAngle = bottleAngle < 0 ? bottleAngle + 2 * (float)Math.PI : bottleAngle;
float normalizedStartAngle = _startAngle < 0 ? _startAngle + 2 * (float)Math.PI : _startAngle;
float normalizedEndAngle = _endAngle < 0 ? _endAngle + 2 * (float)Math.PI : _endAngle;
// Handle case where the arc crosses the 0/2π boundary
if (normalizedStartAngle > normalizedEndAngle)
{
// Arc crosses 0/2π boundary - check if bottle is in either part of the arc
if (!(normalizedBottleAngle >= normalizedStartAngle || normalizedBottleAngle <= normalizedEndAngle))
return 0;
}
else
{
// Normal case - arc doesn't cross boundary
if (normalizedBottleAngle < normalizedStartAngle || normalizedBottleAngle > normalizedEndAngle)
return 0;
}
// Calculate distance from center
float distanceToCenter = centerToBottle.Length();
// If bottle is outside radius range, return 0
if (distanceToCenter < _innerRadius || distanceToCenter > _outerRadius)
return 0;
// Calculate how far the bottle is from the edges
float angleFromStart = bottleAngle - _startAngle;
float angleToEnd = _endAngle - bottleAngle;
// Calculate how far the bottle is from the edges using normalized angles
float angleFromStart, angleToEnd, totalAngle;
if (normalizedStartAngle > normalizedEndAngle)
{
// Arc crosses 0/2π boundary
totalAngle = (2 * (float)Math.PI - normalizedStartAngle) + normalizedEndAngle;
if (normalizedBottleAngle >= normalizedStartAngle)
{
angleFromStart = normalizedBottleAngle - normalizedStartAngle;
angleToEnd = (2 * (float)Math.PI - normalizedBottleAngle) + normalizedEndAngle;
}
else
{
angleFromStart = (2 * (float)Math.PI - normalizedStartAngle) + normalizedBottleAngle;
angleToEnd = normalizedEndAngle - normalizedBottleAngle;
}
}
else
{
// Normal case
angleFromStart = normalizedBottleAngle - normalizedStartAngle;
angleToEnd = normalizedEndAngle - normalizedBottleAngle;
totalAngle = normalizedEndAngle - normalizedStartAngle;
}
// Use the minimum distance to either edge to calculate overlap
float minAngleDistance = Math.Min(angleFromStart, angleToEnd);
float totalAngle = _endAngle - _startAngle;
// Calculate overlap percentage based on angle proximity to edges
float overlapPercentage = Math.Min(minAngleDistance / (totalAngle * 0.1f), 1.0f);
return overlapPercentage;