29 lines
1.3 KiB
C#
29 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace MagnetismHeroes.UnityRuntime.Presentation
|
|
{
|
|
public static class ContactPlacement
|
|
{
|
|
// Leave the union of unit-sphere overlap intervals along the chosen contact direction.
|
|
// Translating the whole friend preserves its shape, including two identical rows.
|
|
public static float Clearance(IReadOnlyList<Vector3> moving, IReadOnlyList<Vector3> stationary, Vector3 direction)
|
|
{
|
|
var intervals = new List<(float start, float end)>();
|
|
foreach (var source in moving)
|
|
foreach (var target in stationary)
|
|
{
|
|
var offset = source - target; float along = Vector3.Dot(offset, direction);
|
|
float perpendicularSquared = offset.sqrMagnitude - along * along;
|
|
if (perpendicularSquared >= 1) continue;
|
|
float half = Mathf.Sqrt(Mathf.Max(0, 1 - perpendicularSquared));
|
|
intervals.Add((-along - half, -along + half));
|
|
}
|
|
intervals.Sort((a, b) => a.start.CompareTo(b.start)); float distance = 0;
|
|
foreach (var interval in intervals)
|
|
if (interval.start < distance + .0001f && interval.end > distance) distance = interval.end;
|
|
return distance;
|
|
}
|
|
}
|
|
}
|