| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasDiagnosticSink.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using Gravitas.Colliders; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using Gravitas.Constraints; |
| | | 12 | | using Gravitas.Queries; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using System; |
| | | 15 | | |
| | | 16 | | namespace Gravitas.Diagnostics; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Context-owned deterministic diagnostics buffer for physics events and debug draw commands. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed partial class GravitasDiagnosticSink |
| | | 22 | | { |
| | | 23 | | private readonly GravitasWorldContext _context; |
| | 5083 | 24 | | private readonly SwiftList<GravitasDiagnosticEvent> _events = new(); |
| | 5083 | 25 | | private readonly SwiftList<GravitasDebugDrawCommand> _drawCommands = new(); |
| | | 26 | | private int _eventSequence; |
| | | 27 | | private int _drawSequence; |
| | | 28 | | |
| | 5083 | 29 | | internal GravitasDiagnosticSink(GravitasWorldContext context) |
| | | 30 | | { |
| | 5083 | 31 | | SwiftThrowHelper.ThrowIfNull(context, nameof(context)); |
| | 5083 | 32 | | _context = context; |
| | 5083 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets whether this sink records events and draw commands. |
| | | 37 | | /// </summary> |
| | | 38 | | public bool Enabled { get; private set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets captured diagnostic events for this context. |
| | | 42 | | /// </summary> |
| | 58 | 43 | | public ReadOnlySpan<GravitasDiagnosticEvent> Events => _events.AsReadOnlySpan(); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets captured engine-agnostic draw commands for this context. |
| | | 47 | | /// </summary> |
| | 45 | 48 | | public ReadOnlySpan<GravitasDebugDrawCommand> DrawCommands => _drawCommands.AsReadOnlySpan(); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Dispatches captured events in buffer order to a typed diagnostic visitor. |
| | | 52 | | /// </summary> |
| | | 53 | | public void DispatchEventsTo(GravitasDiagnosticEventVisitor visitor) |
| | | 54 | | { |
| | 2 | 55 | | if (visitor == null) |
| | 1 | 56 | | throw new ArgumentNullException(nameof(visitor)); |
| | | 57 | | |
| | 1 | 58 | | ReadOnlySpan<GravitasDiagnosticEvent> events = Events; |
| | 6 | 59 | | for (int i = 0; i < events.Length; i++) |
| | 2 | 60 | | events[i].DispatchTo(visitor); |
| | 1 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Dispatches captured draw commands in buffer order to a typed debug draw visitor. |
| | | 65 | | /// </summary> |
| | | 66 | | public void DispatchDrawCommandsTo(GravitasDebugDrawCommandVisitor visitor) |
| | | 67 | | { |
| | 2 | 68 | | if (visitor == null) |
| | 1 | 69 | | throw new ArgumentNullException(nameof(visitor)); |
| | | 70 | | |
| | 1 | 71 | | ReadOnlySpan<GravitasDebugDrawCommand> commands = DrawCommands; |
| | 6 | 72 | | for (int i = 0; i < commands.Length; i++) |
| | 2 | 73 | | commands[i].DispatchTo(visitor); |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Gets the number of captured diagnostic events.</summary> |
| | 13 | 77 | | public int EventCount => _events.Count; |
| | | 78 | | |
| | | 79 | | /// <summary>Gets the number of captured debug draw commands.</summary> |
| | 8 | 80 | | public int DrawCommandCount => _drawCommands.Count; |
| | | 81 | | |
| | 3 | 82 | | internal int EventCapacity => _events.Capacity; |
| | | 83 | | |
| | 4 | 84 | | internal int DrawCommandCapacity => _drawCommands.Capacity; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Enables diagnostics and optionally reserves buffer capacity for hot-path capture. |
| | | 88 | | /// </summary> |
| | | 89 | | public void Enable(int eventCapacity = 128, int drawCommandCapacity = 128) |
| | | 90 | | { |
| | 76 | 91 | | Enabled = true; |
| | 76 | 92 | | if (eventCapacity > 0) |
| | 54 | 93 | | _events.EnsureCapacity(eventCapacity); |
| | 76 | 94 | | if (drawCommandCapacity > 0) |
| | 34 | 95 | | _drawCommands.EnsureCapacity(drawCommandCapacity); |
| | 76 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Disables diagnostics and clears captured data. |
| | | 100 | | /// </summary> |
| | | 101 | | public void Disable() |
| | | 102 | | { |
| | 1 | 103 | | Enabled = false; |
| | 1 | 104 | | Clear(); |
| | 1 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Clears captured data while retaining allocated buffer capacity. |
| | | 109 | | /// </summary> |
| | | 110 | | public void Clear() |
| | | 111 | | { |
| | 42 | 112 | | _events.FastClear(); |
| | 42 | 113 | | _drawCommands.FastClear(); |
| | 42 | 114 | | _eventSequence = 0; |
| | 42 | 115 | | _drawSequence = 0; |
| | 42 | 116 | | } |
| | | 117 | | |
| | 31 | 118 | | internal void Reset() => Clear(); |
| | | 119 | | |
| | | 120 | | internal void EmitForceDelta(SolidBody body, Vector3d force, Vector3d accelerationDelta) |
| | | 121 | | { |
| | 311 | 122 | | if (!Enabled) |
| | 307 | 123 | | return; |
| | | 124 | | |
| | 4 | 125 | | AddEvent( |
| | 4 | 126 | | GravitasDiagnosticEventKind.ForceDelta, |
| | 4 | 127 | | bodyId: body.DynamicId, |
| | 4 | 128 | | colliderAId: body.Collider.Id, |
| | 4 | 129 | | colliderAType: body.Collider.Shape, |
| | 4 | 130 | | vector: force, |
| | 4 | 131 | | pointA: accelerationDelta, |
| | 4 | 132 | | scalarA: force.Magnitude); |
| | 4 | 133 | | } |
| | | 134 | | |
| | | 135 | | internal void EmitTorqueDelta(SolidBody body, Vector3d torque) |
| | | 136 | | { |
| | 102 | 137 | | if (!Enabled) |
| | 98 | 138 | | return; |
| | | 139 | | |
| | 4 | 140 | | AddEvent( |
| | 4 | 141 | | GravitasDiagnosticEventKind.TorqueDelta, |
| | 4 | 142 | | bodyId: body.DynamicId, |
| | 4 | 143 | | colliderAId: body.Collider.Id, |
| | 4 | 144 | | colliderAType: body.Collider.Shape, |
| | 4 | 145 | | vector: torque, |
| | 4 | 146 | | scalarA: torque.Magnitude); |
| | 4 | 147 | | } |
| | | 148 | | |
| | | 149 | | internal void EmitLinearVelocityDelta(SolidBody body, Vector3d before, Vector3d after) |
| | | 150 | | { |
| | 247812 | 151 | | if (!Enabled) |
| | 247687 | 152 | | return; |
| | | 153 | | |
| | 125 | 154 | | AddEvent( |
| | 125 | 155 | | GravitasDiagnosticEventKind.LinearVelocityDelta, |
| | 125 | 156 | | bodyId: body.DynamicId, |
| | 125 | 157 | | colliderAId: body.Collider.Id, |
| | 125 | 158 | | colliderAType: body.Collider.Shape, |
| | 125 | 159 | | start: before, |
| | 125 | 160 | | end: after, |
| | 125 | 161 | | vector: after - before, |
| | 125 | 162 | | scalarA: after.Magnitude); |
| | 125 | 163 | | } |
| | | 164 | | |
| | | 165 | | internal void EmitAngularVelocityDelta(SolidBody body, Vector3d before, Vector3d after) |
| | | 166 | | { |
| | 240863 | 167 | | if (!Enabled) |
| | 240076 | 168 | | return; |
| | | 169 | | |
| | 787 | 170 | | AddEvent( |
| | 787 | 171 | | GravitasDiagnosticEventKind.AngularVelocityDelta, |
| | 787 | 172 | | bodyId: body.DynamicId, |
| | 787 | 173 | | colliderAId: body.Collider.Id, |
| | 787 | 174 | | colliderAType: body.Collider.Shape, |
| | 787 | 175 | | start: before, |
| | 787 | 176 | | end: after, |
| | 787 | 177 | | vector: after - before, |
| | 787 | 178 | | scalarA: after.Magnitude); |
| | 787 | 179 | | } |
| | | 180 | | |
| | | 181 | | internal void EmitGroundProbe( |
| | | 182 | | SolidBody body, |
| | | 183 | | GroundProbeMode mode, |
| | | 184 | | Vector3d origin, |
| | | 185 | | Vector3d end, |
| | | 186 | | Fixed64 radius, |
| | | 187 | | bool hit, |
| | | 188 | | Physics3DHit raycastHit) |
| | | 189 | | { |
| | 8760 | 190 | | if (!Enabled) |
| | 8728 | 191 | | return; |
| | | 192 | | |
| | 32 | 193 | | Vector3d point = default; |
| | 32 | 194 | | bool hasPoint = hit && raycastHit.TryGetPoint(out point); |
| | 32 | 195 | | AddEvent( |
| | 32 | 196 | | GravitasDiagnosticEventKind.GroundProbe, |
| | 32 | 197 | | bodyId: body.DynamicId, |
| | 32 | 198 | | colliderAId: body.Collider.Id, |
| | 32 | 199 | | colliderBId: raycastHit.Collider?.Id ?? -1, |
| | 32 | 200 | | colliderAType: body.Collider.Shape, |
| | 32 | 201 | | colliderBType: raycastHit.Collider?.Shape ?? ColliderType.None, |
| | 32 | 202 | | start: origin, |
| | 32 | 203 | | end: end, |
| | 32 | 204 | | pointA: point, |
| | 32 | 205 | | hasPointA: hasPoint, |
| | 32 | 206 | | vector: raycastHit.Normal, |
| | 32 | 207 | | scalarA: radius, |
| | 32 | 208 | | scalarB: raycastHit.Distance, |
| | 32 | 209 | | dataA: (int)mode, |
| | 32 | 210 | | hit: hit); |
| | 32 | 211 | | } |
| | | 212 | | |
| | | 213 | | internal void EmitGroundProbe( |
| | | 214 | | SolidBody2D body, |
| | | 215 | | GroundProbeMode2D mode, |
| | | 216 | | Vector2d start, |
| | | 217 | | Vector2d end, |
| | | 218 | | Fixed64 radius, |
| | | 219 | | bool hit, |
| | | 220 | | Physics2DHit raycastHit) |
| | | 221 | | { |
| | 5233 | 222 | | if (!Enabled) |
| | 5195 | 223 | | return; |
| | | 224 | | |
| | 38 | 225 | | AddEvent( |
| | 38 | 226 | | GravitasDiagnosticEventKind.GroundProbe, |
| | 38 | 227 | | bodyId: body.DynamicId, |
| | 38 | 228 | | colliderAId: body.Collider.Id, |
| | 38 | 229 | | colliderBId: raycastHit.Collider?.Id ?? -1, |
| | 38 | 230 | | colliderADimension: GravitasColliderDimension.TwoD, |
| | 38 | 231 | | colliderBDimension: raycastHit.Collider == null ? GravitasColliderDimension.None : GravitasColliderDimension |
| | 38 | 232 | | colliderA2DType: body.Collider.Shape, |
| | 38 | 233 | | colliderB2DType: raycastHit.Collider?.Shape ?? ColliderType2D.None, |
| | 38 | 234 | | start: ToDiagnosticVector(start), |
| | 38 | 235 | | end: ToDiagnosticVector(end), |
| | 38 | 236 | | pointA: ToDiagnosticVector(raycastHit.Point), |
| | 38 | 237 | | hasPointA: hit, |
| | 38 | 238 | | vector: ToDiagnosticVector(raycastHit.Normal), |
| | 38 | 239 | | scalarA: radius, |
| | 38 | 240 | | scalarB: raycastHit.Distance, |
| | 38 | 241 | | dataA: (int)mode, |
| | 38 | 242 | | dataB: (int)GravitasColliderDimension.TwoD, |
| | 38 | 243 | | hit: hit); |
| | 38 | 244 | | } |
| | | 245 | | |
| | | 246 | | internal void EmitRayQuery( |
| | | 247 | | Vector3d start, |
| | | 248 | | Vector3d end, |
| | | 249 | | Fixed64 radius, |
| | | 250 | | int layerMaskBits, |
| | | 251 | | bool hit, |
| | | 252 | | int hitCount, |
| | | 253 | | Physics3DHit raycastHit) |
| | | 254 | | { |
| | 15779 | 255 | | if (!Enabled) |
| | 15747 | 256 | | return; |
| | | 257 | | |
| | 32 | 258 | | Vector3d point = default; |
| | 32 | 259 | | bool hasPoint = hit && raycastHit.TryGetPoint(out point); |
| | 32 | 260 | | AddEvent( |
| | 32 | 261 | | GravitasDiagnosticEventKind.RayQuery, |
| | 32 | 262 | | colliderAId: raycastHit.Collider?.Id ?? -1, |
| | 32 | 263 | | colliderAType: raycastHit.Collider?.Shape ?? ColliderType.None, |
| | 32 | 264 | | start: start, |
| | 32 | 265 | | end: end, |
| | 32 | 266 | | pointA: point, |
| | 32 | 267 | | hasPointA: hasPoint, |
| | 32 | 268 | | vector: raycastHit.Normal, |
| | 32 | 269 | | scalarA: radius, |
| | 32 | 270 | | scalarB: raycastHit.Distance, |
| | 32 | 271 | | dataA: layerMaskBits, |
| | 32 | 272 | | dataB: hitCount, |
| | 32 | 273 | | hit: hit); |
| | 32 | 274 | | } |
| | | 275 | | |
| | | 276 | | internal void EmitCircleQuery( |
| | | 277 | | Vector3d center, |
| | | 278 | | Fixed64 radius, |
| | | 279 | | Vector3d direction, |
| | | 280 | | Fixed64 maxDistance, |
| | | 281 | | int layerMaskBits, |
| | | 282 | | bool hit, |
| | | 283 | | int hitCount, |
| | | 284 | | Physics3DHit raycastHit) |
| | | 285 | | { |
| | 35 | 286 | | if (!Enabled) |
| | 31 | 287 | | return; |
| | | 288 | | |
| | 4 | 289 | | Vector3d point = default; |
| | 4 | 290 | | bool hasPoint = hit && raycastHit.TryGetPoint(out point); |
| | 4 | 291 | | AddEvent( |
| | 4 | 292 | | GravitasDiagnosticEventKind.CircleQuery, |
| | 4 | 293 | | colliderAId: raycastHit.Collider?.Id ?? -1, |
| | 4 | 294 | | colliderAType: raycastHit.Collider?.Shape ?? ColliderType.None, |
| | 4 | 295 | | start: center, |
| | 4 | 296 | | end: center + direction * maxDistance, |
| | 4 | 297 | | pointA: point, |
| | 4 | 298 | | hasPointA: hasPoint, |
| | 4 | 299 | | vector: direction, |
| | 4 | 300 | | scalarA: radius, |
| | 4 | 301 | | scalarB: raycastHit.Distance, |
| | 4 | 302 | | dataA: layerMaskBits, |
| | 4 | 303 | | dataB: hitCount, |
| | 4 | 304 | | hit: hit); |
| | 4 | 305 | | } |
| | | 306 | | |
| | | 307 | | internal void EmitQuerySummary( |
| | | 308 | | GravitasColliderDimension sourceDimension, |
| | | 309 | | GravitasColliderDimension targetDimension, |
| | | 310 | | Vector3d start, |
| | | 311 | | Vector3d end, |
| | | 312 | | int exactReducerAttempts, |
| | | 313 | | int acceptedHits, |
| | | 314 | | int fallbackHits, |
| | | 315 | | int rejectedConservativeCandidates) |
| | | 316 | | { |
| | 6 | 317 | | AddEvent( |
| | 6 | 318 | | GravitasDiagnosticEventKind.QuerySummary, |
| | 6 | 319 | | colliderADimension: sourceDimension, |
| | 6 | 320 | | colliderBDimension: targetDimension, |
| | 6 | 321 | | start: start, |
| | 6 | 322 | | end: end, |
| | 6 | 323 | | scalarA: (Fixed64)fallbackHits, |
| | 6 | 324 | | scalarB: (Fixed64)rejectedConservativeCandidates, |
| | 6 | 325 | | dataA: exactReducerAttempts, |
| | 6 | 326 | | dataB: acceptedHits, |
| | 6 | 327 | | hit: fallbackHits > 0 || rejectedConservativeCandidates > 0); |
| | 6 | 328 | | } |
| | | 329 | | |
| | | 330 | | internal void EmitContact(CollisionPair pair, bool hit) |
| | | 331 | | { |
| | 2489 | 332 | | if (!Enabled) |
| | 2472 | 333 | | return; |
| | | 334 | | |
| | 17 | 335 | | bool hasContact = pair.Manifold.HasContact; |
| | 17 | 336 | | ManifoldContact contact = hasContact |
| | 17 | 337 | | ? pair.Manifold.PrimaryContact |
| | 17 | 338 | | : default; |
| | 17 | 339 | | Vector3d pointA = default; |
| | 17 | 340 | | Vector3d pointB = default; |
| | 17 | 341 | | bool hasPointA = hasContact && contact.TryGetPointA(out pointA); |
| | 17 | 342 | | bool hasPointB = hasContact && contact.TryGetPointB(out pointB); |
| | | 343 | | |
| | 17 | 344 | | AddEvent( |
| | 17 | 345 | | GravitasDiagnosticEventKind.Contact, |
| | 17 | 346 | | colliderAId: pair.ColliderA.Id, |
| | 17 | 347 | | colliderBId: pair.ColliderB.Id, |
| | 17 | 348 | | colliderAType: pair.ColliderA.Shape, |
| | 17 | 349 | | colliderBType: pair.ColliderB.Shape, |
| | 17 | 350 | | pointA: pointA, |
| | 17 | 351 | | pointB: pointB, |
| | 17 | 352 | | hasPointA: hasPointA, |
| | 17 | 353 | | hasPointB: hasPointB, |
| | 17 | 354 | | vector: contact.Normal, |
| | 17 | 355 | | scalarA: contact.Depth, |
| | 17 | 356 | | dataA: pair.Manifold.Count, |
| | 17 | 357 | | dataB: contact.DepthIsClamped ? 1 : 0, |
| | 17 | 358 | | hit: hit && hasContact); |
| | 17 | 359 | | } |
| | | 360 | | |
| | | 361 | | internal void EmitResponseImpulse(CollisionPair pair, Vector3d impulse, Fixed64 normalVelocity) |
| | | 362 | | { |
| | 6826 | 363 | | if (!Enabled) |
| | 6813 | 364 | | return; |
| | | 365 | | |
| | 13 | 366 | | ManifoldContact contact = pair.Manifold.PrimaryContact; |
| | 13 | 367 | | bool hasPointA = contact.TryGetPointA(out Vector3d pointA); |
| | 13 | 368 | | bool hasPointB = contact.TryGetPointB(out Vector3d pointB); |
| | | 369 | | |
| | 13 | 370 | | AddEvent( |
| | 13 | 371 | | GravitasDiagnosticEventKind.ResponseImpulse, |
| | 13 | 372 | | colliderAId: pair.ColliderA.Id, |
| | 13 | 373 | | colliderBId: pair.ColliderB.Id, |
| | 13 | 374 | | colliderAType: pair.ColliderA.Shape, |
| | 13 | 375 | | colliderBType: pair.ColliderB.Shape, |
| | 13 | 376 | | pointA: pointA, |
| | 13 | 377 | | pointB: pointB, |
| | 13 | 378 | | hasPointA: hasPointA, |
| | 13 | 379 | | hasPointB: hasPointB, |
| | 13 | 380 | | vector: impulse, |
| | 13 | 381 | | scalarA: impulse.Magnitude, |
| | 13 | 382 | | scalarB: normalVelocity, |
| | 13 | 383 | | hit: true); |
| | 13 | 384 | | } |
| | | 385 | | |
| | | 386 | | internal void EmitMixedQuery( |
| | | 387 | | Vector3d start, |
| | | 388 | | Vector3d end, |
| | | 389 | | Fixed64 radius, |
| | | 390 | | int layerMaskBits, |
| | | 391 | | bool hit, |
| | | 392 | | int hitCount, |
| | | 393 | | PhysicsMixedHit mixedHit) |
| | | 394 | | { |
| | 1336 | 395 | | if (!Enabled) |
| | 1331 | 396 | | return; |
| | | 397 | | |
| | 5 | 398 | | Vector3d point3D = default; |
| | 5 | 399 | | Vector3d point2D = default; |
| | 5 | 400 | | bool hasPoint3D = hit && mixedHit.TryGetPoint3D(out point3D); |
| | 5 | 401 | | bool hasPoint2D = hit && mixedHit.TryGetPoint2D(out point2D); |
| | 5 | 402 | | AddEvent( |
| | 5 | 403 | | GravitasDiagnosticEventKind.MixedQuery, |
| | 5 | 404 | | colliderAId: mixedHit.Collider3D?.Id ?? -1, |
| | 5 | 405 | | colliderBId: mixedHit.Collider2D?.Id ?? -1, |
| | 5 | 406 | | colliderADimension: mixedHit.Collider3D == null ? GravitasColliderDimension.None : GravitasColliderDimension |
| | 5 | 407 | | colliderBDimension: mixedHit.Collider2D == null ? GravitasColliderDimension.None : GravitasColliderDimension |
| | 5 | 408 | | colliderAType: mixedHit.Collider3D?.Shape ?? ColliderType.None, |
| | 5 | 409 | | colliderB2DType: mixedHit.Collider2D?.Shape ?? ColliderType2D.None, |
| | 5 | 410 | | start: start, |
| | 5 | 411 | | end: end, |
| | 5 | 412 | | pointA: point3D, |
| | 5 | 413 | | pointB: point2D, |
| | 5 | 414 | | hasPointA: hasPoint3D, |
| | 5 | 415 | | hasPointB: hasPoint2D, |
| | 5 | 416 | | vector: mixedHit.Normal3DTo2D, |
| | 5 | 417 | | scalarA: radius, |
| | 5 | 418 | | scalarB: mixedHit.Distance, |
| | 5 | 419 | | dataA: layerMaskBits, |
| | 5 | 420 | | dataB: hitCount, |
| | 5 | 421 | | hit: hit); |
| | 5 | 422 | | } |
| | | 423 | | |
| | | 424 | | internal void EmitMixedContact(CollisionPairMixed pair, MixedContact contact) |
| | | 425 | | { |
| | 184 | 426 | | if (!Enabled) |
| | 161 | 427 | | return; |
| | | 428 | | |
| | 23 | 429 | | bool hasPoint3D = contact.TryGetPoint3D(out Vector3d point3D); |
| | 23 | 430 | | bool hasPoint2D = contact.TryGetPoint2D(out Vector3d point2D); |
| | 23 | 431 | | AddEvent( |
| | 23 | 432 | | GravitasDiagnosticEventKind.MixedContact, |
| | 23 | 433 | | colliderAId: pair.Collider3DId, |
| | 23 | 434 | | colliderBId: pair.Collider2DId, |
| | 23 | 435 | | colliderADimension: GravitasColliderDimension.ThreeD, |
| | 23 | 436 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 23 | 437 | | colliderAType: pair.Collider3D.Shape, |
| | 23 | 438 | | colliderB2DType: pair.Collider2D.Shape, |
| | 23 | 439 | | pointA: point3D, |
| | 23 | 440 | | pointB: point2D, |
| | 23 | 441 | | hasPointA: hasPoint3D, |
| | 23 | 442 | | hasPointB: hasPoint2D, |
| | 23 | 443 | | vector: contact.Normal3DTo2D, |
| | 23 | 444 | | scalarA: contact.Depth, |
| | 23 | 445 | | dataA: contact.DepthIsClamped ? 1 : 0, |
| | 23 | 446 | | hit: true); |
| | 23 | 447 | | } |
| | | 448 | | |
| | | 449 | | internal void EmitMixedResponseImpulse( |
| | | 450 | | CollisionPairMixed pair, |
| | | 451 | | MixedContact contact, |
| | | 452 | | Vector3d impulse, |
| | | 453 | | Fixed64 normalVelocity, |
| | | 454 | | int iteration, |
| | | 455 | | int iterationLimit) |
| | | 456 | | { |
| | 110 | 457 | | if (!Enabled) |
| | 94 | 458 | | return; |
| | | 459 | | |
| | 16 | 460 | | bool hasPoint3D = contact.TryGetPoint3D(out Vector3d point3D); |
| | 16 | 461 | | bool hasPoint2D = contact.TryGetPoint2D(out Vector3d point2D); |
| | 16 | 462 | | AddEvent( |
| | 16 | 463 | | GravitasDiagnosticEventKind.MixedResponseImpulse, |
| | 16 | 464 | | colliderAId: pair.Collider3DId, |
| | 16 | 465 | | colliderBId: pair.Collider2DId, |
| | 16 | 466 | | colliderADimension: GravitasColliderDimension.ThreeD, |
| | 16 | 467 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 16 | 468 | | colliderAType: pair.Collider3D.Shape, |
| | 16 | 469 | | colliderB2DType: pair.Collider2D.Shape, |
| | 16 | 470 | | pointA: point3D, |
| | 16 | 471 | | pointB: point2D, |
| | 16 | 472 | | hasPointA: hasPoint3D, |
| | 16 | 473 | | hasPointB: hasPoint2D, |
| | 16 | 474 | | vector: impulse, |
| | 16 | 475 | | scalarA: impulse.Magnitude, |
| | 16 | 476 | | scalarB: normalVelocity, |
| | 16 | 477 | | dataA: iteration, |
| | 16 | 478 | | dataB: iterationLimit, |
| | 16 | 479 | | hit: true); |
| | 16 | 480 | | } |
| | | 481 | | |
| | | 482 | | internal void EmitMixedResponseIsland( |
| | | 483 | | int rootKey, |
| | | 484 | | int constraintCount, |
| | | 485 | | int iterationCount, |
| | | 486 | | bool reachedIterationLimit) |
| | | 487 | | { |
| | 13 | 488 | | if (!Enabled) |
| | 6 | 489 | | return; |
| | | 490 | | |
| | 7 | 491 | | AddEvent( |
| | 7 | 492 | | GravitasDiagnosticEventKind.MixedResponseIsland, |
| | 7 | 493 | | bodyId: rootKey, |
| | 7 | 494 | | dataA: constraintCount, |
| | 7 | 495 | | dataB: iterationCount, |
| | 7 | 496 | | hit: reachedIterationLimit); |
| | 7 | 497 | | } |
| | | 498 | | |
| | | 499 | | internal void EmitJointRegistered(Joint3D joint) |
| | | 500 | | { |
| | 312 | 501 | | if (!Enabled) |
| | 307 | 502 | | return; |
| | | 503 | | |
| | 5 | 504 | | AddEvent( |
| | 5 | 505 | | GravitasDiagnosticEventKind.JointRegistered, |
| | 5 | 506 | | jointId: joint.Id, |
| | 5 | 507 | | colliderAId: joint.BodyA.Collider.Id, |
| | 5 | 508 | | colliderBId: joint.BodyB.Collider.Id, |
| | 5 | 509 | | colliderAType: joint.BodyA.Collider.Shape, |
| | 5 | 510 | | colliderBType: joint.BodyB.Collider.Shape, |
| | 5 | 511 | | dataA: (int)joint.Type, |
| | 5 | 512 | | dataB: (int)joint.CollisionPolicy); |
| | 5 | 513 | | } |
| | | 514 | | |
| | | 515 | | internal void EmitJointRegistered(Joint2D joint) |
| | | 516 | | { |
| | 195 | 517 | | if (!Enabled) |
| | 190 | 518 | | return; |
| | | 519 | | |
| | 5 | 520 | | AddEvent( |
| | 5 | 521 | | GravitasDiagnosticEventKind.JointRegistered, |
| | 5 | 522 | | jointId: joint.Id, |
| | 5 | 523 | | colliderAId: joint.BodyA.Collider.Id, |
| | 5 | 524 | | colliderBId: joint.BodyB.Collider.Id, |
| | 5 | 525 | | colliderADimension: GravitasColliderDimension.TwoD, |
| | 5 | 526 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 5 | 527 | | colliderA2DType: joint.BodyA.Collider.Shape, |
| | 5 | 528 | | colliderB2DType: joint.BodyB.Collider.Shape, |
| | 5 | 529 | | dataA: (int)joint.Type, |
| | 5 | 530 | | dataB: (int)joint.CollisionPolicy); |
| | 5 | 531 | | } |
| | | 532 | | |
| | | 533 | | internal void EmitJointRemoved(Joint3D joint) |
| | | 534 | | { |
| | 29 | 535 | | if (!Enabled) |
| | 24 | 536 | | return; |
| | | 537 | | |
| | 5 | 538 | | AddEvent( |
| | 5 | 539 | | GravitasDiagnosticEventKind.JointRemoved, |
| | 5 | 540 | | jointId: joint.Id, |
| | 5 | 541 | | colliderAId: joint.BodyA.Collider.Id, |
| | 5 | 542 | | colliderBId: joint.BodyB.Collider.Id, |
| | 5 | 543 | | colliderAType: joint.BodyA.Collider.Shape, |
| | 5 | 544 | | colliderBType: joint.BodyB.Collider.Shape, |
| | 5 | 545 | | dataA: (int)joint.Type, |
| | 5 | 546 | | dataB: (int)joint.CollisionPolicy); |
| | 5 | 547 | | } |
| | | 548 | | |
| | | 549 | | internal void EmitJointRemoved(Joint2D joint) |
| | | 550 | | { |
| | 28 | 551 | | if (!Enabled) |
| | 23 | 552 | | return; |
| | | 553 | | |
| | 5 | 554 | | AddEvent( |
| | 5 | 555 | | GravitasDiagnosticEventKind.JointRemoved, |
| | 5 | 556 | | jointId: joint.Id, |
| | 5 | 557 | | colliderAId: joint.BodyA.Collider.Id, |
| | 5 | 558 | | colliderBId: joint.BodyB.Collider.Id, |
| | 5 | 559 | | colliderADimension: GravitasColliderDimension.TwoD, |
| | 5 | 560 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 5 | 561 | | colliderA2DType: joint.BodyA.Collider.Shape, |
| | 5 | 562 | | colliderB2DType: joint.BodyB.Collider.Shape, |
| | 5 | 563 | | dataA: (int)joint.Type, |
| | 5 | 564 | | dataB: (int)joint.CollisionPolicy); |
| | 5 | 565 | | } |
| | | 566 | | |
| | | 567 | | internal void EmitJointImpulse(Joint3D joint, JointSolveMetrics3D metrics) |
| | | 568 | | { |
| | 48069 | 569 | | if (!Enabled) |
| | 47679 | 570 | | return; |
| | | 571 | | |
| | 390 | 572 | | AddEvent( |
| | 390 | 573 | | GravitasDiagnosticEventKind.JointImpulse, |
| | 390 | 574 | | jointId: joint.Id, |
| | 390 | 575 | | colliderAId: joint.BodyA.Collider.Id, |
| | 390 | 576 | | colliderBId: joint.BodyB.Collider.Id, |
| | 390 | 577 | | colliderAType: joint.BodyA.Collider.Shape, |
| | 390 | 578 | | colliderBType: joint.BodyB.Collider.Shape, |
| | 390 | 579 | | vector: new Vector3d( |
| | 390 | 580 | | metrics.MotorImpulseMagnitude, |
| | 390 | 581 | | metrics.MotorErrorMagnitude, |
| | 390 | 582 | | metrics.AngularLimitErrorMagnitude), |
| | 390 | 583 | | scalarA: metrics.AccumulatedImpulseMagnitude, |
| | 390 | 584 | | scalarB: metrics.LinearAnchorErrorMagnitude, |
| | 390 | 585 | | dataA: metrics.PreparedRowCount, |
| | 390 | 586 | | dataB: metrics.ClampedRowCount, |
| | 390 | 587 | | hit: metrics.IncrementalImpulseMagnitude > Fixed64.Zero); |
| | 390 | 588 | | } |
| | | 589 | | |
| | | 590 | | internal void EmitJointImpulse(Joint2D joint, JointSolveMetrics2D metrics) |
| | | 591 | | { |
| | 576 | 592 | | if (!Enabled) |
| | 571 | 593 | | return; |
| | | 594 | | |
| | 5 | 595 | | AddEvent( |
| | 5 | 596 | | GravitasDiagnosticEventKind.JointImpulse, |
| | 5 | 597 | | jointId: joint.Id, |
| | 5 | 598 | | colliderAId: joint.BodyA.Collider.Id, |
| | 5 | 599 | | colliderBId: joint.BodyB.Collider.Id, |
| | 5 | 600 | | colliderADimension: GravitasColliderDimension.TwoD, |
| | 5 | 601 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 5 | 602 | | colliderA2DType: joint.BodyA.Collider.Shape, |
| | 5 | 603 | | colliderB2DType: joint.BodyB.Collider.Shape, |
| | 5 | 604 | | vector: new Vector3d( |
| | 5 | 605 | | metrics.MotorImpulseMagnitude, |
| | 5 | 606 | | metrics.MotorErrorMagnitude, |
| | 5 | 607 | | metrics.LimitErrorMagnitude), |
| | 5 | 608 | | scalarA: metrics.AccumulatedImpulseMagnitude, |
| | 5 | 609 | | scalarB: metrics.LinearAnchorErrorMagnitude, |
| | 5 | 610 | | dataA: metrics.PreparedRowCount, |
| | 5 | 611 | | dataB: metrics.ClampedRowCount, |
| | 5 | 612 | | hit: metrics.IncrementalImpulseMagnitude > Fixed64.Zero); |
| | 5 | 613 | | } |
| | | 614 | | |
| | | 615 | | internal void EmitJointLimitReached(Joint3D joint, Fixed64 limitError) |
| | | 616 | | { |
| | 89 | 617 | | if (!Enabled) |
| | 88 | 618 | | return; |
| | | 619 | | |
| | 1 | 620 | | AddEvent( |
| | 1 | 621 | | GravitasDiagnosticEventKind.JointLimitReached, |
| | 1 | 622 | | jointId: joint.Id, |
| | 1 | 623 | | colliderAId: joint.BodyA.Collider.Id, |
| | 1 | 624 | | colliderBId: joint.BodyB.Collider.Id, |
| | 1 | 625 | | colliderAType: joint.BodyA.Collider.Shape, |
| | 1 | 626 | | colliderBType: joint.BodyB.Collider.Shape, |
| | 1 | 627 | | scalarB: limitError, |
| | 1 | 628 | | dataA: (int)joint.Limits.Kind, |
| | 1 | 629 | | hit: limitError != Fixed64.Zero); |
| | 1 | 630 | | } |
| | | 631 | | |
| | | 632 | | internal void EmitJointLimitReached(Joint2D joint, Fixed64 limitError) |
| | | 633 | | { |
| | 177 | 634 | | if (!Enabled) |
| | 176 | 635 | | return; |
| | | 636 | | |
| | 1 | 637 | | AddEvent( |
| | 1 | 638 | | GravitasDiagnosticEventKind.JointLimitReached, |
| | 1 | 639 | | jointId: joint.Id, |
| | 1 | 640 | | colliderAId: joint.BodyA.Collider.Id, |
| | 1 | 641 | | colliderBId: joint.BodyB.Collider.Id, |
| | 1 | 642 | | colliderADimension: GravitasColliderDimension.TwoD, |
| | 1 | 643 | | colliderBDimension: GravitasColliderDimension.TwoD, |
| | 1 | 644 | | colliderA2DType: joint.BodyA.Collider.Shape, |
| | 1 | 645 | | colliderB2DType: joint.BodyB.Collider.Shape, |
| | 1 | 646 | | scalarB: limitError, |
| | 1 | 647 | | dataA: (int)joint.Limits.Kind, |
| | 1 | 648 | | hit: limitError != Fixed64.Zero); |
| | 1 | 649 | | } |
| | | 650 | | |
| | | 651 | | internal void EmitRagdollActivated(int ragdollId, int linkCount, int jointCount, bool isActive) |
| | | 652 | | { |
| | 16 | 653 | | if (!Enabled) |
| | 11 | 654 | | return; |
| | | 655 | | |
| | 5 | 656 | | AddEvent( |
| | 5 | 657 | | GravitasDiagnosticEventKind.RagdollActivated, |
| | 5 | 658 | | bodyId: ragdollId, |
| | 5 | 659 | | dataA: linkCount, |
| | 5 | 660 | | dataB: jointCount, |
| | 5 | 661 | | hit: isActive); |
| | 5 | 662 | | } |
| | | 663 | | |
| | | 664 | | private void AddEvent( |
| | | 665 | | GravitasDiagnosticEventKind kind, |
| | | 666 | | int bodyId = -1, |
| | | 667 | | int jointId = -1, |
| | | 668 | | int colliderAId = -1, |
| | | 669 | | int colliderBId = -1, |
| | | 670 | | GravitasColliderDimension colliderADimension = GravitasColliderDimension.None, |
| | | 671 | | GravitasColliderDimension colliderBDimension = GravitasColliderDimension.None, |
| | | 672 | | ColliderType colliderAType = ColliderType.None, |
| | | 673 | | ColliderType colliderBType = ColliderType.None, |
| | | 674 | | ColliderType2D colliderA2DType = ColliderType2D.None, |
| | | 675 | | ColliderType2D colliderB2DType = ColliderType2D.None, |
| | | 676 | | Vector3d start = default, |
| | | 677 | | Vector3d end = default, |
| | | 678 | | Vector3d pointA = default, |
| | | 679 | | Vector3d pointB = default, |
| | | 680 | | bool hasPointA = false, |
| | | 681 | | bool hasPointB = false, |
| | | 682 | | Vector3d vector = default, |
| | | 683 | | Fixed64 scalarA = default, |
| | | 684 | | Fixed64 scalarB = default, |
| | | 685 | | int dataA = 0, |
| | | 686 | | int dataB = 0, |
| | | 687 | | bool hit = false) |
| | | 688 | | { |
| | 1535 | 689 | | _events.Add(new GravitasDiagnosticEvent( |
| | 1535 | 690 | | _context.FrameCount, |
| | 1535 | 691 | | _eventSequence++, |
| | 1535 | 692 | | kind, |
| | 1535 | 693 | | bodyId, |
| | 1535 | 694 | | jointId, |
| | 1535 | 695 | | colliderAId, |
| | 1535 | 696 | | colliderBId, |
| | 1535 | 697 | | ResolveDimension(colliderADimension, colliderAType), |
| | 1535 | 698 | | ResolveDimension(colliderBDimension, colliderBType), |
| | 1535 | 699 | | colliderAType, |
| | 1535 | 700 | | colliderBType, |
| | 1535 | 701 | | colliderA2DType, |
| | 1535 | 702 | | colliderB2DType, |
| | 1535 | 703 | | start, |
| | 1535 | 704 | | end, |
| | 1535 | 705 | | pointA, |
| | 1535 | 706 | | pointB, |
| | 1535 | 707 | | hasPointA, |
| | 1535 | 708 | | hasPointB, |
| | 1535 | 709 | | vector, |
| | 1535 | 710 | | scalarA, |
| | 1535 | 711 | | scalarB, |
| | 1535 | 712 | | dataA, |
| | 1535 | 713 | | dataB, |
| | 1535 | 714 | | hit)); |
| | 1535 | 715 | | } |
| | | 716 | | |
| | | 717 | | private void AddDrawCommand( |
| | | 718 | | GravitasDebugDrawKind kind, |
| | | 719 | | int colliderId = -1, |
| | | 720 | | ColliderType colliderType = ColliderType.None, |
| | | 721 | | GravitasColliderDimension colliderDimension = GravitasColliderDimension.None, |
| | | 722 | | ColliderType2D collider2DType = ColliderType2D.None, |
| | | 723 | | Vector3d start = default, |
| | | 724 | | Vector3d end = default, |
| | | 725 | | Vector3d center = default, |
| | | 726 | | Vector3d halfExtents = default, |
| | | 727 | | Vector3d pointA = default, |
| | | 728 | | Vector3d pointB = default, |
| | | 729 | | Vector3d pointC = default, |
| | | 730 | | FixedQuaternion rotation = default, |
| | | 731 | | Fixed64 radius = default, |
| | | 732 | | Fixed64 axisLength = default, |
| | | 733 | | Fixed64 height = default, |
| | | 734 | | GravitasDiagnosticColor color = default) |
| | | 735 | | { |
| | 383 | 736 | | _drawCommands.Add(new GravitasDebugDrawCommand( |
| | 383 | 737 | | _context.FrameCount, |
| | 383 | 738 | | _drawSequence++, |
| | 383 | 739 | | kind, |
| | 383 | 740 | | colliderId, |
| | 383 | 741 | | ResolveDimension(colliderDimension, colliderType), |
| | 383 | 742 | | colliderType, |
| | 383 | 743 | | collider2DType, |
| | 383 | 744 | | start, |
| | 383 | 745 | | end, |
| | 383 | 746 | | center, |
| | 383 | 747 | | halfExtents, |
| | 383 | 748 | | pointA, |
| | 383 | 749 | | pointB, |
| | 383 | 750 | | pointC, |
| | 383 | 751 | | rotation, |
| | 383 | 752 | | radius, |
| | 383 | 753 | | axisLength, |
| | 383 | 754 | | height, |
| | 383 | 755 | | color)); |
| | 383 | 756 | | } |
| | | 757 | | |
| | | 758 | | private static GravitasColliderDimension ResolveDimension( |
| | | 759 | | GravitasColliderDimension dimension, |
| | | 760 | | ColliderType colliderType) |
| | | 761 | | { |
| | 3453 | 762 | | if (dimension != GravitasColliderDimension.None) |
| | 231 | 763 | | return dimension; |
| | 3222 | 764 | | if (colliderType != ColliderType.None) |
| | 2126 | 765 | | return GravitasColliderDimension.ThreeD; |
| | 1096 | 766 | | return GravitasColliderDimension.None; |
| | | 767 | | } |
| | | 768 | | |
| | 152 | 769 | | private static Vector3d ToDiagnosticVector(Vector2d value) => new(value.X, Fixed64.Zero, value.Y); |
| | | 770 | | } |