Recover and verify static collision traversal and body contracts
This commit is contained in:
1 parent
7c0d1a4b0c
commit
df0263b26e
34 files changed
+1962
-17
No files matched your search
@@ -325,8 +325,9 @@ public sealed class LabSimulation
|
||||
var wasGrounded = Grounded;
|
||||
var velocity = Velocity;
|
||||
// Keep the existing capsule/gravity adapter until C4 recovers Jimmy's
|
||||
// pre-integration support/response. Source479DF7..479E54 can cancel
|
||||
// gravity before a supported impulse; this adapter does not model that.
|
||||
// support/response. Source479DF7..479E54 cancels gravity and vertical
|
||||
// velocity when global geometry4F69E5 is disabled, not when grounded.
|
||||
// This adapter does not model that separate source debug/global gate.
|
||||
var displacement = RecoveredMotion.Integrate(ref velocity, ref pendingImpulse,
|
||||
Vector3.UnitY * extraAirAcceleration, RecoveredMotion.Gravity, dt);
|
||||
var before = Position;
|
||||
|
||||
@@ -4,7 +4,7 @@ using GamePort.Assets;
|
||||
namespace GamePort.Game;
|
||||
|
||||
public readonly record struct CameraRayPolygon(SupportTriangle Triangle, Vector3 StoredNormal,
|
||||
string? ShapeName = null, string? MaterialName = null);
|
||||
string? ShapeName = null, string? MaterialName = null, int? MaterialId = null);
|
||||
public readonly record struct CameraRayResult(Vector3 Eye, float Gain, SupportHit? Hit);
|
||||
|
||||
/// <summary>
|
||||
@@ -77,7 +77,7 @@ public sealed class OriginalCameraRay
|
||||
if (a >= mesh.Vertices.Length || b >= mesh.Vertices.Length || c >= mesh.Vertices.Length)
|
||||
throw new InvalidDataException("Camera triangle index outside vertex table");
|
||||
polygons[i] = new(new(mesh.Vertices[a].Position, mesh.Vertices[b].Position, mesh.Vertices[c].Position,
|
||||
source.ShapeId, source.PolygonIndex), source.Normal, source.ShapeName, source.MaterialName);
|
||||
source.ShapeId, source.PolygonIndex), source.Normal, source.ShapeName, source.MaterialName, source.MaterialId);
|
||||
}
|
||||
return new(polygons);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
|
||||
namespace GamePort.Game;
|
||||
|
||||
public sealed class StaticCollisionContext(float radius, float faceDistance = 130000)
|
||||
{
|
||||
public SphereResponseState Nearest { get; internal set; } = new(faceDistance, radius, null);
|
||||
public CameraRayPolygon? LastPolygon { get; internal set; }
|
||||
public int Passes { get; internal set; }
|
||||
public int TriangleTests { get; internal set; }
|
||||
}
|
||||
|
||||
public readonly record struct StaticCollisionMove(Vector3 Position, CameraRayPolygon? CallbackPolygon, bool Published);
|
||||
public readonly record struct CollisionCells(int MinX, int MaxX, int MinZ, int MaxZ);
|
||||
|
||||
/// <summary>
|
||||
/// Source47B4B0/47BBB0 static dispatcher. Preserves cell/FIFO duplicates, mutable
|
||||
/// candidate, shared nearest state and retries. Callers own normal callbacks,
|
||||
/// integration, support and scene-wide state. See collision-grid-traversal.md.
|
||||
/// </summary>
|
||||
public sealed class OriginalStaticCollision
|
||||
{
|
||||
private readonly Dictionary<int, List<CameraRayPolygon>> walls = [];
|
||||
private readonly Dictionary<int, List<SupportTriangle>> all = [];
|
||||
public int PolygonCount { get; }
|
||||
public int WallPolygonCount { get; }
|
||||
public bool HasSpecialPlayerContacts { get; }
|
||||
|
||||
public OriginalStaticCollision(IEnumerable<CameraRayPolygon> polygons)
|
||||
{
|
||||
var identities = new HashSet<(int, int)>();
|
||||
foreach (var polygon in polygons.OrderBy(p => p.Triangle.ShapeId).ThenBy(p => p.Triangle.PolygonId))
|
||||
{
|
||||
if (polygon.ShapeName?.StartsWith("nc", StringComparison.OrdinalIgnoreCase) == true
|
||||
|| Checkpoint(polygon.MaterialName)) continue;
|
||||
var t = polygon.Triangle;
|
||||
Finite(t.A); Finite(t.B); Finite(t.C); Finite(polygon.StoredNormal);
|
||||
if (!identities.Add((t.ShapeId, t.PolygonId)))
|
||||
throw new InvalidDataException("Source static collision needs one triangle per polygon");
|
||||
var min = Vector3.Min(t.A, Vector3.Min(t.B, t.C));
|
||||
var max = Vector3.Max(t.A, Vector3.Max(t.B, t.C));
|
||||
foreach (var cell in Rectangle(new(Cell(min.X), Cell(max.X), Cell(min.Z), Cell(max.Z))))
|
||||
{
|
||||
if (!all.TryGetValue(cell, out var support)) all.Add(cell, support = []);
|
||||
support.Add(t);
|
||||
if (polygon.StoredNormal.Y >= .7f) continue;
|
||||
if (!walls.TryGetValue(cell, out var list)) walls.Add(cell, list = []);
|
||||
list.Add(polygon);
|
||||
}
|
||||
PolygonCount++;
|
||||
if (polygon.StoredNormal.Y < .7f) WallPolygonCount++;
|
||||
if (polygon.StoredNormal.Y > -.7f && polygon.StoredNormal.Y < .7f
|
||||
&& polygon.MaterialId is >= 60 and <= 69 or >= 110 and <= 129 or 180)
|
||||
HasSpecialPlayerContacts = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static OriginalStaticCollision FromRouteScene(Mesh mesh, string level)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(mesh);
|
||||
if (level is not ("LV1B" or "VR04" or "LEV1"))
|
||||
throw new NotSupportedException("Original static game-filter recovery covers only the three route scenes");
|
||||
if (mesh.Indices.Length % 3 != 0 || mesh.CollisionPolygons.Length != mesh.Indices.Length / 3)
|
||||
throw new InvalidDataException("Source static collision requires original polygon metadata");
|
||||
if (!mesh.HasCollisionMaterialIds)
|
||||
throw new InvalidDataException("Source static collision requires material identities; prepare the scene again");
|
||||
return new(Enumerable.Range(0, mesh.CollisionPolygons.Length).Select(i => {
|
||||
var p = mesh.CollisionPolygons[i];
|
||||
if (mesh.Indices[i * 3] >= mesh.Vertices.Length || mesh.Indices[i * 3 + 1] >= mesh.Vertices.Length
|
||||
|| mesh.Indices[i * 3 + 2] >= mesh.Vertices.Length)
|
||||
throw new InvalidDataException("Collision triangle index outside vertex table");
|
||||
return new CameraRayPolygon(new(mesh.Vertices[mesh.Indices[i * 3]].Position,
|
||||
mesh.Vertices[mesh.Indices[i * 3 + 1]].Position, mesh.Vertices[mesh.Indices[i * 3 + 2]].Position,
|
||||
p.ShapeId, p.PolygonIndex), p.Normal, p.ShapeName, p.MaterialName, p.MaterialId);
|
||||
}));
|
||||
}
|
||||
|
||||
public StaticCollisionMove Move(Vector3 previous, Vector3 delta, float radius,
|
||||
StaticCollisionContext context, bool edges = true, bool lowerEdges = false, bool geometryEnabled = true)
|
||||
{
|
||||
Finite(previous); Finite(delta);
|
||||
if (!float.IsFinite(radius) || radius <= 0) throw new ArgumentOutOfRangeException(nameof(radius));
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
// The source recurses with no explicit depth limit. This iterative form
|
||||
// keeps the same state, and rejects paths outside the bounded contract
|
||||
// instead of silently inventing a final collision position.
|
||||
for (var retry = 0; retry < 256; retry++)
|
||||
{
|
||||
var length = OriginalBodySupport.QuickMagnitude(delta);
|
||||
if (!lowerEdges && length < 1.1920928955078125e-7f) return new(previous, null, false);
|
||||
context.Passes++;
|
||||
var candidate = previous + delta;
|
||||
var accepted = false;
|
||||
foreach (var cell in Rectangle(Bounds(previous, candidate, radius + length)))
|
||||
if (walls.TryGetValue(cell, out var polygons))
|
||||
foreach (var polygon in polygons)
|
||||
{
|
||||
if (!geometryEnabled) continue;
|
||||
context.TriangleTests++;
|
||||
var result = OriginalSphereResponse.Resolve(polygon.Triangle, polygon.StoredNormal,
|
||||
previous, candidate, radius, context.Nearest, edges, lowerEdges);
|
||||
if (result.Contact == SphereContact.None) continue;
|
||||
candidate = result.Position; context.Nearest = result.State;
|
||||
context.LastPolygon = polygon; accepted = true;
|
||||
}
|
||||
if (!accepted) return new(candidate, context.LastPolygon, true);
|
||||
delta = candidate - previous;
|
||||
}
|
||||
throw new InvalidOperationException("Source collision retry exceeds the verified finite-motion contract");
|
||||
}
|
||||
|
||||
/// <summary>47C6E0 nearest reduction; equal hits retain cell/FIFO order.</summary>
|
||||
public SupportHit? TraceSupport(Vector3 start, Vector3 end, bool twoSided = false)
|
||||
{
|
||||
Finite(start); Finite(end);
|
||||
SupportHit? nearest = null;
|
||||
var nearestDistance = OriginalBodySupport.QuickMagnitude(end - start);
|
||||
foreach (var cell in Rectangle(new(Math.Min(Cell(start.X), Cell(end.X)), Math.Max(Cell(start.X), Cell(end.X)),
|
||||
Math.Min(Cell(start.Z), Cell(end.Z)), Math.Max(Cell(start.Z), Cell(end.Z)))))
|
||||
if (all.TryGetValue(cell, out var triangles))
|
||||
foreach (var triangle in triangles)
|
||||
if (OriginalBodySupport.IntersectSegment(triangle, start, end, twoSided) is { } hit
|
||||
&& hit.Distance < nearestDistance)
|
||||
{
|
||||
nearest = hit;
|
||||
nearestDistance = hit.Distance;
|
||||
}
|
||||
return nearest;
|
||||
}
|
||||
|
||||
/// <summary>47B53A..47B62A: the Z upper bound intentionally uses MIN.</summary>
|
||||
public static CollisionCells Bounds(Vector3 previous, Vector3 candidate, float expansion) => new(
|
||||
Math.Min(Cell(previous.X - expansion), Cell(candidate.X - expansion)),
|
||||
Math.Max(Cell(previous.X + expansion), Cell(candidate.X + expansion)),
|
||||
Math.Min(Cell(previous.Z - expansion), Cell(candidate.Z - expansion)),
|
||||
Math.Min(Cell(previous.Z + expansion), Cell(candidate.Z + expansion)));
|
||||
|
||||
private static int Cell(float value) => OriginalCameraRay.CellCoordinate(value);
|
||||
private static IEnumerable<int> Rectangle(CollisionCells bounds)
|
||||
{
|
||||
for (var x = Math.Max(0, bounds.MinX); x <= Math.Min(259, bounds.MaxX); x++)
|
||||
for (var z = Math.Max(0, bounds.MinZ); z <= Math.Min(259, bounds.MaxZ); z++) yield return 260 * x + z;
|
||||
}
|
||||
private static bool Checkpoint(string? value) => value != null
|
||||
&& (value.Equals("cstart", StringComparison.OrdinalIgnoreCase)
|
||||
|| value.Length == 7 && value.StartsWith("ccheck", StringComparison.OrdinalIgnoreCase)
|
||||
&& value[6] is >= '1' and <= '9');
|
||||
private static void Finite(Vector3 value)
|
||||
{
|
||||
if (!float.IsFinite(value.X) || !float.IsFinite(value.Y) || !float.IsFinite(value.Z))
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user