88 lines
3.6 KiB
C#
88 lines
3.6 KiB
C#
using System.Numerics;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>Live callbacks of an original physical-list member, not render visibility or raw control.</summary>
|
|
public interface IOriginalPairBody
|
|
{
|
|
bool PairCollisionEnabled { get; }
|
|
int CollisionMode { get; }
|
|
Vector3 Root { get; }
|
|
float Radius { get; }
|
|
bool AllowsPair(IOriginalPairBody other);
|
|
bool Excludes(IOriginalPairBody other);
|
|
bool HasContact(IOriginalPairBody other);
|
|
void SphereOverlap(IOriginalPairBody other, float distance);
|
|
void Enter(IOriginalPairBody other);
|
|
void Stay(IOriginalPairBody other);
|
|
void Leave(IOriginalPairBody other);
|
|
}
|
|
|
|
/// <summary>
|
|
/// EXE47A430/47B3E0/47CA90 ordered pair and notification contract. The caller
|
|
/// supplies physical registration order and real actor callbacks. Mode2 geometry
|
|
/// remains a separate injected solver. Not yet admitted to the live scene loop.
|
|
/// </summary>
|
|
public static class OriginalPairCollision
|
|
{
|
|
/// <remarks>
|
|
/// This bounded component requires a stable list during the pass. Source
|
|
/// linked-list insertion/deletion during callbacks is not reconstructed here.
|
|
/// Eligibility gates retain existing contacts; they do not synthesize leaves.
|
|
/// </remarks>
|
|
public static void Step(IReadOnlyList<IOriginalPairBody> bodies,
|
|
Func<IOriginalPairBody, IOriginalPairBody, bool>? boxContact = null, bool enabled = true)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(bodies);
|
|
if (!enabled) return;
|
|
for (var i = 0; i < bodies.Count; i++)
|
|
{
|
|
var first = bodies[i];
|
|
//47A46E: sampled once for this outer traversal, before its callbacks.
|
|
if (!first.PairCollisionEnabled) continue;
|
|
for (var j = i; j < bodies.Count; j++)
|
|
{
|
|
var second = bodies[j];
|
|
if (!second.PairCollisionEnabled || ReferenceEquals(first, second)
|
|
|| !first.AllowsPair(second) || !second.AllowsPair(first)) continue;
|
|
bool contact;
|
|
if (first.CollisionMode == 2) contact = Box(first, second);
|
|
else if (second.CollisionMode == 2) contact = Box(second, first);
|
|
else contact = SphereContact(first, second);
|
|
|
|
Notify(first, second, contact);
|
|
//The first callback can alter the second actor's membership.
|
|
Notify(second, first, contact);
|
|
}
|
|
}
|
|
|
|
bool Box(IOriginalPairBody box, IOriginalPairBody other) =>
|
|
(boxContact ?? throw new InvalidOperationException("Mode2 requires an explicit source box solver"))(box, other);
|
|
}
|
|
|
|
public static bool SphereContact(IOriginalPairBody first, IOriginalPairBody second)
|
|
{
|
|
//Source queries second root first, then first root and both radii.
|
|
var secondRoot = second.Root;
|
|
var firstRoot = first.Root;
|
|
var firstRadius = first.Radius;
|
|
var radius = second.Radius + firstRadius;
|
|
var distance = OriginalBodySupport.QuickMagnitude(secondRoot - firstRoot);
|
|
//47CA90 checks only the earlier body's exclusions, after strict overlap.
|
|
if (!(distance < radius) || first.Excludes(second)) return false;
|
|
first.SphereOverlap(second, distance);
|
|
second.SphereOverlap(first, distance);
|
|
return true;
|
|
}
|
|
|
|
private static void Notify(IOriginalPairBody self, IOriginalPairBody other, bool contact)
|
|
{
|
|
if (!contact)
|
|
{
|
|
if (self.HasContact(other)) self.Leave(other);
|
|
}
|
|
else if (self.HasContact(other)) self.Stay(other);
|
|
else self.Enter(other);
|
|
}
|
|
}
|