Fix physics recorder crash when bodies are disposed during capture
All checks were successful
Build / build (push) Successful in 1m31s

Added defensive checks and try-catch to handle race conditions where physics
bodies are disposed between the filter check and property access. This commonly
happens with projectiles and destroyed asteroids.

Changes:
- Added null/undefined check for physicsBody in filter
- Added transformNode existence check before access
- Wrapped capture logic in try-catch to skip disposed objects
- Continue loop instead of crashing when object is disposed

Fixes TypeError: Cannot read properties of undefined (reading 'transformNode')

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Michael Mainguy 2025-11-08 05:26:37 -06:00
parent 96ae033064
commit e473e3d03e

View File

@ -155,11 +155,17 @@ export class PhysicsRecorder {
const objects: PhysicsObjectState[] = []; const objects: PhysicsObjectState[] = [];
// Get all physics-enabled meshes // Get all physics-enabled meshes
const physicsMeshes = this._scene.meshes.filter(mesh => mesh.physicsBody !== null); const physicsMeshes = this._scene.meshes.filter(mesh => mesh.physicsBody !== null && mesh.physicsBody !== undefined);
for (const mesh of physicsMeshes) { for (const mesh of physicsMeshes) {
const body = mesh.physicsBody!; const body = mesh.physicsBody;
// Double-check body still exists and has transformNode (can be disposed between filter and here)
if (!body || !body.transformNode) {
continue;
}
try {
// Get position // Get position
const pos = body.transformNode.position; const pos = body.transformNode.position;
@ -210,6 +216,10 @@ export class PhysicsRecorder {
mass: parseFloat(mass.toFixed(2)), mass: parseFloat(mass.toFixed(2)),
restitution: parseFloat(restitution.toFixed(2)) restitution: parseFloat(restitution.toFixed(2))
}); });
} catch (error) {
// Physics body was disposed during capture, skip this object
continue;
}
} }
const snapshot: PhysicsSnapshot = { const snapshot: PhysicsSnapshot = {