Add controllable lab walking checkpoint with collision and chase camera

This commit is contained in:
UnixNight committed 2026-10-23 23:29:40 -04:00
1 parent 37fa18094f
commit 58424120bb
26 files changed
+1848 -110

No files matched your search

+108 -20
View File
@@ -3,6 +3,7 @@ using System.Numerics;
using GamePort.Assets;
using GamePort.Application;
using GamePort.Audio;
using GamePort.Game;
using GamePort.Platform;
using GamePort.Rendering.Vulkan;
@@ -39,6 +40,9 @@ internal static class Program
--original-camera Stored GAM start camera and recovered projection
--animation PATH Prepared Jimmy animation.json; place at GAM startpoint
--with-player Use prepared jimstop animation in scene
--walk Controllable lab (default with prepared lab)
--inspect Pause simulation and inspect scene/animation
--smoke-walk Deterministic 240-step movement/jump check
--time SECONDS Fixed animation time for comparisons
--headless Render offscreen through Vulkan
--screenshot PATH Save actual GPU readback as PPM
@@ -48,9 +52,11 @@ internal static class Program
--verify-assets Decode all original PNG/ASE files
--smoke-window Test keyboard event/resize (requires --frames 6 or more)
--no-validation Disable development validation
Viewer: C toggles stored/orbit camera, arrows/WASD orbit, +/- zoom,
Walk: arrows move/turn; Shift, Insert or right mouse jump; R reset.
C pauses into inspection / resumes chase camera. F11 fullscreen, Escape quit.
Inspector: arrows/WASD orbit, +/- zoom,
F11 fullscreen, Escape quit. Animation preview: 1 idle, 2 run, 3 jump, 4 scratch.
Gameplay, original animation speed/state and cutscenes remain under reconstruction.
Collision/pivot/cadence and animation timing are provisional. Intro/interactions remain incomplete.
""");
return 0;
}
@@ -90,9 +96,17 @@ internal static class Program
var target = (bounds.Min + bounds.Max) / 2;
var radius = Math.Max(Vector3.Distance(bounds.Min, bounds.Max) * 1.15f, 1);
var animationPath = Option(args, "--animation");
if (args.Contains("--with-player") && scene != null)
var walkSmoke = args.Contains("--smoke-walk");
if (walkSmoke && args.Contains("--inspect")) throw new ArgumentException("Walk smoke cannot run in inspection mode.");
if (walkSmoke && args.Contains("--smoke-window")) throw new ArgumentException("Select one window smoke mode.");
var walking = !args.Contains("--inspect") && !args.Contains("--smoke-window") && (args.Contains("--walk") || walkSmoke
|| defaultScene && animationPath == null && !args.Contains("--time"));
if ((args.Contains("--with-player") || args.Contains("--walk") || defaultScene || walking) && scene != null)
animationPath ??= Path.Combine(Path.GetDirectoryName(scene)!, "../animations/jimstop/animation.json");
if (walking && (scene == null || originalStart == null))
throw new ArgumentException("Walking requires a scene and matching GAM startpoint.");
PreviewAnimationLibrary? animations = null;
LabSimulation? simulation = null;
var characterOffset = mesh.Vertices.Length;
var characterModel = Matrix4x4.Identity;
if (animationPath != null)
@@ -101,17 +115,21 @@ internal static class Program
throw new ArgumentException("A scene and matching GAM are required to place an animated player.");
animations = new PreviewAnimationLibrary(animationPath);
var standingPose = AseMesh.Load(AssetPath.Resolve(root, "ASE/jimstop.ase"));
if (walking) simulation = new LabSimulation(new SceneCollision(mesh), standingPose, originalStart.Position, originalStart.Rotation.Y);
var placement = PreviewCharacterPlacement.Create(mesh, standingPose, originalStart.Position, originalStart.Rotation.Y);
characterModel = placement.Model;
mesh = animations.Current.AppendTo(mesh);
Console.WriteLine($"Preview floor correction: +{placement.VerticalCorrection:F3} Y; original GAM position remains unchanged.");
Console.WriteLine("Scene preview: 1 idle (still pose), 2 run, 3 jump, 4 scratch. Playback loop/speed are preview policies; gameplay is incomplete.");
if (walking)
Console.WriteLine("Controllable lab: arrows move/turn; Shift/Insert/right mouse jump; R reset; C pause/inspect; F11 fullscreen; Escape quit. Collision, pivot and timing remain provisional.");
else
Console.WriteLine($"Preview floor correction: +{placement.VerticalCorrection:F3} Y. 1 idle, 2 run, 3 jump, 4 scratch.");
}
Console.WriteLine($"Loaded original geometry: {mesh.Vertices.Length} vertices, {mesh.Indices.Length / 3} triangles, {mesh.Textures.Count + (scene == null ? 1 : 0)} textures.");
var fixedTimeText = Option(args, "--time");
double? fixedTime = fixedTimeText == null ? null : double.Parse(fixedTimeText, System.Globalization.CultureInfo.InvariantCulture);
var headless = args.Contains("--headless");
var frames = int.Parse(Option(args, "--frames") ?? (headless ? "1" : "0"));
var frames = int.Parse(Option(args, "--frames") ?? (walkSmoke ? "240" : headless ? "1" : "0"));
if (walkSmoke && frames < 240) throw new ArgumentException("Walk smoke requires at least 240 frames.");
var smoke = args.Contains("--smoke-window");
if (frames < 0 || (smoke && (headless || frames < 6)))
throw new ArgumentException("Invalid frame count; window smoke requires at least six frames.");
@@ -119,7 +137,7 @@ internal static class Program
if (!headless)
{
if (!Sdl.SDL_Init(Sdl.InitVideo)) throw new InvalidOperationException(Sdl.Error);
window = Sdl.SDL_CreateWindow(animations?.WindowTitle ?? "Jimmy Neutron — scene preview | C camera", 960, 720,
window = Sdl.SDL_CreateWindow(walking ? WalkTitle : animations?.WindowTitle ?? "Jimmy Neutron — scene preview | C camera", 960, 720,
Sdl.WindowVulkan | Sdl.WindowResizable | Sdl.WindowHighPixelDensity);
if (window == 0) { Sdl.SDL_Quit(); throw new InvalidOperationException(Sdl.Error); }
}
@@ -136,29 +154,67 @@ internal static class Program
var clock = Stopwatch.StartNew();
var animationStarted = 0.0;
var animationSwitched = false;
var inspecting = !walking;
var active = true;
var mouseJump = false;
var stepClock = new FixedStepClock();
double poseTime = 0;
var injectedInput = default(PlayerInput);
var sawWalkKeys = 0;
double last = clock.Elapsed.TotalSeconds;
float angle = 0.6f, elevation = 0.32f;
var count = 0;
while (running)
{
if (walkSmoke && !headless)
{
var requested = LabSimulation.SmokeInput(simulation!.Steps);
foreach (var (scancode, down, previous) in new[] {
(82u, requested.Forward, injectedInput.Forward),
(79u, requested.Right, injectedInput.Right), (225u, requested.Jump, injectedInput.Jump) })
{
if (down == previous) continue;
var injected = new Sdl.Event { Type = down ? Sdl.KeyDown : Sdl.KeyUp, Scancode = scancode, Down = down ? (byte)1 : (byte)0 };
if (!Sdl.SDL_PushEvent(ref injected)) throw new InvalidOperationException(Sdl.Error);
}
injectedInput = requested;
}
while (!headless && Sdl.SDL_PollEvent(out var e))
{
if (e.Type is Sdl.Quit or 0x210) running = false;
if (e.Type is Sdl.WindowResized or Sdl.WindowPixelSizeChanged) renderer.Resize();
if (e.Type == Sdl.WindowFocusLost) keys.Clear();
if (e.Type is Sdl.WindowFocusLost or Sdl.WindowMinimized)
{ keys.Clear(); mouseJump = false; active = false; stepClock.Reset(); }
if (e.Type is Sdl.WindowFocusGained or Sdl.WindowRestored)
{ active = true; stepClock.Reset(); last = clock.Elapsed.TotalSeconds; }
if (e.Type == Sdl.MouseButtonDown && e.MouseButton == 3) mouseJump = true;
if (e.Type == Sdl.MouseButtonUp && e.MouseButton == 3) mouseJump = false;
if (e.Type == Sdl.KeyDown)
{
keys.Add(e.Scancode);
if (walkSmoke) sawWalkKeys |= e.Scancode == 82 ? 1 : e.Scancode == 79 ? 2 : e.Scancode == 225 ? 4 : 0;
if (smoke && e.Scancode == 79) sawKey = true;
if (e.Scancode == 41) running = false;
if (e.Repeat == 0 && animations?.Select(e.Scancode) == true)
if (inspecting && e.Repeat == 0 && animations?.Select(e.Scancode) == true)
{
animationStarted = clock.Elapsed.TotalSeconds;
animationSwitched = true;
if (!Sdl.SDL_SetWindowTitle(window, animations.WindowTitle)) throw new InvalidOperationException(Sdl.Error);
}
if (e.Scancode == 6 && e.Repeat == 0 && originalStart != null)
originalCamera = !originalCamera;
{
if (simulation != null)
{
inspecting = !inspecting;
keys.Clear(); mouseJump = false; stepClock.Reset();
originalCamera = false;
target = simulation.ActorRoot;
radius = 600;
if (!Sdl.SDL_SetWindowTitle(window, inspecting ? animations!.WindowTitle : WalkTitle)) throw new InvalidOperationException(Sdl.Error);
}
else originalCamera = !originalCamera;
}
if (!inspecting && e.Scancode == 21 && e.Repeat == 0) { simulation!.Reset(); poseTime = 0; }
if (e.Scancode == 68 && e.Repeat == 0)
{
fullscreen = !fullscreen;
@@ -168,33 +224,56 @@ internal static class Program
if (e.Type == Sdl.KeyUp) keys.Remove(e.Scancode);
}
var now = clock.Elapsed.TotalSeconds;
var dt = (float)Math.Min(now - last, 0.1);
var elapsed = now - last;
var dt = (float)Math.Min(elapsed, 0.1);
last = now;
if (keys.Contains(80) || keys.Contains(4)) angle -= dt;
if (keys.Contains(79) || keys.Contains(7)) angle += dt;
if (keys.Contains(82) || keys.Contains(26)) elevation = Math.Min(elevation + dt, 1.4f);
if (keys.Contains(81) || keys.Contains(22)) elevation = Math.Max(elevation - dt, -1.4f);
if (keys.Contains(46) || keys.Contains(87)) radius *= MathF.Exp(-dt);
if (keys.Contains(45) || keys.Contains(86)) radius *= MathF.Exp(dt);
if (simulation != null && !inspecting && (active || headless || walkSmoke))
{
var steps = headless || walkSmoke ? 1 : stepClock.Advance(elapsed);
for (var step = 0; step < steps; step++)
{
var input = walkSmoke && headless ? LabSimulation.SmokeInput(simulation.Steps) : new PlayerInput(
keys.Contains(82), keys.Contains(81), keys.Contains(80), keys.Contains(79),
mouseJump || keys.Contains(225) || keys.Contains(229) || keys.Contains(73) || keys.Contains(98));
simulation.Step(input);
if (animations!.SelectPose(simulation.Pose)) poseTime = 0;
poseTime += FixedStepClock.Step;
}
characterModel = simulation.Model;
}
if (inspecting)
{
if (keys.Contains(80) || keys.Contains(4)) angle -= dt;
if (keys.Contains(79) || keys.Contains(7)) angle += dt;
if (keys.Contains(82) || keys.Contains(26)) elevation = Math.Min(elevation + dt, 1.4f);
if (keys.Contains(81) || keys.Contains(22)) elevation = Math.Max(elevation - dt, -1.4f);
if (keys.Contains(46) || keys.Contains(87)) radius *= MathF.Exp(-dt);
if (keys.Contains(45) || keys.Contains(86)) radius *= MathF.Exp(dt);
}
radius = Math.Max(radius, 0.01f);
var eye = target + radius * new Vector3(MathF.Cos(elevation) * MathF.Sin(angle),
MathF.Sin(elevation), MathF.Cos(elevation) * MathF.Cos(angle));
var view = originalCamera
? Matrix4x4.CreateLookAtLeftHanded(originalStart!.CameraPosition, originalStart.Position + new Vector3(0, 80, 0), Vector3.UnitY)
: Matrix4x4.CreateLookAt(eye, target, Vector3.UnitY);
if (simulation != null && !inspecting)
view = Matrix4x4.CreateLookAtLeftHanded(simulation.CameraEye, simulation.CameraTarget, Vector3.UnitY);
var far = level?.Code == "LV1B" ? 10000 : 3200;
var projection = originalCamera
var projection = originalCamera || simulation != null && !inspecting
? Matrix4x4.CreatePerspectiveFieldOfViewLeftHanded(0xAAA / 16384f * MathF.Tau, (float)renderer.Width / renderer.Height, 20, far)
: Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 3, (float)renderer.Width / renderer.Height, Math.Max(radius / 10000, 0.01f), radius * 20);
projection.M22 *= -1;
if (animations != null)
{
animations.Current.Sample(fixedTime ?? (headless ? count / 60.0 : now - animationStarted), mesh.Vertices.AsSpan(characterOffset), characterModel);
animations.Current.Sample(simulation != null && !inspecting ? poseTime
: fixedTime ?? (headless ? count / 60.0 : now - animationStarted), mesh.Vertices.AsSpan(characterOffset), characterModel);
renderer.UpdateVertices(mesh.Vertices);
}
renderer.Render(view * projection);
count++;
audio?.Pump();
if (walkSmoke && !headless && count == 1 && !Sdl.SDL_SetWindowSize(window, 800, 600))
throw new InvalidOperationException(Sdl.Error);
if (smoke && count == 1)
{
var key = new Sdl.Event { Type = Sdl.KeyDown, Scancode = 79, Down = 1 };
@@ -206,7 +285,7 @@ internal static class Program
if (!Sdl.SDL_PushEvent(ref cycle)) throw new InvalidOperationException(Sdl.Error);
}
}
if (smoke && renderer.Width == 800 && renderer.Height == 600) didResize = true;
if ((smoke || walkSmoke) && renderer.Width == 800 && renderer.Height == 600) didResize = true;
if (frames > 0 && count >= frames) break;
}
if (Option(args, "--screenshot") is { } screenshot)
@@ -219,6 +298,13 @@ internal static class Program
for (var i = 0; i < pixels.Length; i += 4)
if (Math.Abs(pixels[i] - 31) + Math.Abs(pixels[i + 1] - 51) + Math.Abs(pixels[i + 2] - 76) > 6) changed++;
Console.WriteLine($"Rendered {count} frame(s), {changed} non-background pixels, {renderer.ValidationErrors} Vulkan validation error(s).");
if (simulation != null) Console.WriteLine(simulation.Report);
if (walkSmoke && !simulation!.SmokePassed) return 2;
if (walkSmoke && !headless)
{
Console.WriteLine($"Walk SDL keyboard events received: {sawWalkKeys == 7}; resize800x600: {didResize}.");
if (sawWalkKeys != 7 || !didResize) return 2;
}
if (smoke)
{
Console.WriteLine($"SDL3 injected keyboard event received: {sawKey}; swapchain resized to 800x600: {didResize}.");
@@ -237,6 +323,8 @@ internal static class Program
}
}
private const string WalkTitle = "Jimmy Neutron — lab checkpoint | Arrows move | Shift / Insert / right mouse jump | R reset | C inspect";
private static int VerifyAssets(string root)
{
var pngs = Directory.GetFiles(root, "*", SearchOption.AllDirectories)