46 lines
2.4 KiB
C#
46 lines
2.4 KiB
C#
using System.Numerics;
|
|
using GamePort.Assets;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>
|
|
/// Recovered source wall-grid selection and root-support phase, with the port's
|
|
/// provisional swept-sphere wall response. This is not the full original solver.
|
|
/// The caller supplies body radius/factor from the actual actor class.
|
|
/// </summary>
|
|
public sealed class OriginalScenePhysics
|
|
{
|
|
private readonly SceneCollision walls;
|
|
private readonly SupportTriangle[] support;
|
|
|
|
public OriginalScenePhysics(Mesh world)
|
|
{
|
|
if (world.CollisionPolygons.Length != world.Indices.Length / 3)
|
|
throw new InvalidDataException("Prepare source collision polygon metadata before original root support");
|
|
walls = new SceneCollision(world, wallsAndCeilingsOnly: true);
|
|
support = Enumerable.Range(0, world.Indices.Length / 3)
|
|
.Where(i => world.CollisionPolygons[i].TerrainCollision && !Checkpoint(world.CollisionPolygons[i].MaterialName))
|
|
.Select(i => new SupportTriangle(world.Vertices[world.Indices[i * 3]].Position,
|
|
world.Vertices[world.Indices[i * 3 + 1]].Position, world.Vertices[world.Indices[i * 3 + 2]].Position,
|
|
world.CollisionPolygons[i].ShapeId, world.CollisionPolygons[i].PolygonIndex)).ToArray();
|
|
}
|
|
|
|
public SupportResult Step(Vector3 position, Vector3 velocity, float radius, float factor, bool wasGrounded, float dt)
|
|
{
|
|
if (!float.IsFinite(dt) || dt < 0 || dt > 1) throw new ArgumentOutOfRangeException(nameof(dt));
|
|
var previous = position;
|
|
velocity += RecoveredMotion.Gravity * dt;
|
|
// A capsule with height=diameter is a sphere. Floor polygons are handled
|
|
// by the separate original support query, not by this wall response.
|
|
var moved = walls.Move(position - Vector3.UnitY * radius, velocity * dt, radius, radius * 2);
|
|
position = moved.Position + Vector3.UnitY * radius;
|
|
if (moved.HitCeiling && velocity.Y > 0) velocity.Y = 0;
|
|
return OriginalBodySupport.Resolve(new(position, previous, velocity, Vector3.Zero, radius, factor),
|
|
wasGrounded, (start, end, twoSided) => OriginalBodySupport.Trace(support, start, end, twoSided));
|
|
}
|
|
|
|
private static bool Checkpoint(string? name) => name != null
|
|
&& (name.Equals("cstart", StringComparison.OrdinalIgnoreCase)
|
|
|| name.Length == 7 && name.StartsWith("ccheck", StringComparison.OrdinalIgnoreCase) && name[6] is >= '1' and <= '9');
|
|
}
|