Facepunch / sbox-issues

176 stars 12 forks source link

Capsule Trace reporting invalid results for corner intersection #2877

Open Phyksar opened 1 year ago

Phyksar commented 1 year ago

Describe the bug

Trace.Capsule returns invalid results when encountering a corner/vertex, the distance is less than expected and the normal is counter-directional to the direction of the trace. This happens only with corner intersection case, other surface and edge cases are working correctly. Also I'm suspecting that HitPosition is not what it should be, for Trace.Sphere it represents the contact point on sphere, while in my case it's just the center of the capsule which is a totally useless data.

To Reproduce

var forward = ViewAngles.Forward;
var dummySize = (Vector3)0.5f;
var startPosition = Eyes.Position + forward * 96.0f + Vector3.Up * 16.0f;
var capsule = new Capsule(Vector3.Zero, Vector3.Up * 24.0f, 12.0f);
var traceResult = Trace.Capsule(capsule, startPosition, startPosition + Vector3.Down * 80.0f).Run();
DebugOverlay.WireCapsule(capsule, traceResult.EndPosition, Color.Red);
DebugOverlay.Box(traceResult.StartPosition, -dummySize, dummySize, Color.Orange);
DebugOverlay.Text("StartPosition", traceResult.StartPosition, 1, Color.Orange);

DebugOverlay.Box(traceResult.EndPosition, -dummySize, dummySize, Color.Yellow);
DebugOverlay.Text("EndPosition", traceResult.EndPosition, 1, Color.Yellow);

DebugOverlay.Box(traceResult.HitPosition, -dummySize, dummySize, Color.Green);
DebugOverlay.Text("HitPosition", traceResult.HitPosition, 1, Color.Green);

var normalPoint = traceResult.EndPosition + traceResult.Normal * 4.0f;
DebugOverlay.Line(traceResult.StartPosition, traceResult.EndPosition, Color.White);
DebugOverlay.Line(traceResult.EndPosition, normalPoint, Color.Cyan);
DebugOverlay.Text("Normal", normalPoint, 0, Color.Cyan);

Expected behavior

The trace result shound not jump around the corners and normal should be computed correctly.

Media/Files

https://user-images.githubusercontent.com/13320416/217093600-35a08d18-72a1-45b6-89c6-9c10bf081ce6.mp4

https://user-images.githubusercontent.com/13320416/217095654-34f740c3-a9fa-4140-861a-446c33e78076.mp4

Additional context

No response

aylaylay commented 1 year ago

Updated trace solver, this should hopefully be fixed now.

Phyksar commented 1 year ago

@aylaylay The vertex/capsule intersection is now working, but the HitPosition is not reporting the actual hit position for the capsule trace and also the EndPosition has an extra offset of 0.0313 units, all this isn't a problem for a sphere trace.

https://github.com/sboxgame/issues/assets/13320416/353aa02d-9e5f-4f30-a45d-5255d07530e6