Fix physics recorder crash when bodies are disposed during capture
All checks were successful
Build / build (push) Successful in 1m31s
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:
parent
96ae033064
commit
e473e3d03e
@ -155,61 +155,71 @@ 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;
|
||||||
|
|
||||||
// Get position
|
// Double-check body still exists and has transformNode (can be disposed between filter and here)
|
||||||
const pos = body.transformNode.position;
|
if (!body || !body.transformNode) {
|
||||||
|
continue;
|
||||||
// Get rotation as quaternion
|
|
||||||
let quat = body.transformNode.rotationQuaternion;
|
|
||||||
if (!quat) {
|
|
||||||
// Convert Euler to Quaternion if needed
|
|
||||||
const rot = body.transformNode.rotation;
|
|
||||||
quat = Quaternion.FromEulerAngles(rot.x, rot.y, rot.z);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get velocities
|
try {
|
||||||
const linVel = body.getLinearVelocity();
|
// Get position
|
||||||
const angVel = body.getAngularVelocity();
|
const pos = body.transformNode.position;
|
||||||
|
|
||||||
// Get mass
|
// Get rotation as quaternion
|
||||||
const mass = body.getMassProperties().mass;
|
let quat = body.transformNode.rotationQuaternion;
|
||||||
|
if (!quat) {
|
||||||
|
// Convert Euler to Quaternion if needed
|
||||||
|
const rot = body.transformNode.rotation;
|
||||||
|
quat = Quaternion.FromEulerAngles(rot.x, rot.y, rot.z);
|
||||||
|
}
|
||||||
|
|
||||||
// Get restitution (from shape material if available)
|
// Get velocities
|
||||||
let restitution = 0;
|
const linVel = body.getLinearVelocity();
|
||||||
if (body.shape && (body.shape as any).material) {
|
const angVel = body.getAngularVelocity();
|
||||||
restitution = (body.shape as any).material.restitution || 0;
|
|
||||||
|
// Get mass
|
||||||
|
const mass = body.getMassProperties().mass;
|
||||||
|
|
||||||
|
// Get restitution (from shape material if available)
|
||||||
|
let restitution = 0;
|
||||||
|
if (body.shape && (body.shape as any).material) {
|
||||||
|
restitution = (body.shape as any).material.restitution || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
objects.push({
|
||||||
|
id: mesh.id,
|
||||||
|
position: [
|
||||||
|
parseFloat(pos.x.toFixed(3)),
|
||||||
|
parseFloat(pos.y.toFixed(3)),
|
||||||
|
parseFloat(pos.z.toFixed(3))
|
||||||
|
],
|
||||||
|
rotation: [
|
||||||
|
parseFloat(quat.x.toFixed(4)),
|
||||||
|
parseFloat(quat.y.toFixed(4)),
|
||||||
|
parseFloat(quat.z.toFixed(4)),
|
||||||
|
parseFloat(quat.w.toFixed(4))
|
||||||
|
],
|
||||||
|
linearVelocity: [
|
||||||
|
parseFloat(linVel.x.toFixed(3)),
|
||||||
|
parseFloat(linVel.y.toFixed(3)),
|
||||||
|
parseFloat(linVel.z.toFixed(3))
|
||||||
|
],
|
||||||
|
angularVelocity: [
|
||||||
|
parseFloat(angVel.x.toFixed(3)),
|
||||||
|
parseFloat(angVel.y.toFixed(3)),
|
||||||
|
parseFloat(angVel.z.toFixed(3))
|
||||||
|
],
|
||||||
|
mass: parseFloat(mass.toFixed(2)),
|
||||||
|
restitution: parseFloat(restitution.toFixed(2))
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// Physics body was disposed during capture, skip this object
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
objects.push({
|
|
||||||
id: mesh.id,
|
|
||||||
position: [
|
|
||||||
parseFloat(pos.x.toFixed(3)),
|
|
||||||
parseFloat(pos.y.toFixed(3)),
|
|
||||||
parseFloat(pos.z.toFixed(3))
|
|
||||||
],
|
|
||||||
rotation: [
|
|
||||||
parseFloat(quat.x.toFixed(4)),
|
|
||||||
parseFloat(quat.y.toFixed(4)),
|
|
||||||
parseFloat(quat.z.toFixed(4)),
|
|
||||||
parseFloat(quat.w.toFixed(4))
|
|
||||||
],
|
|
||||||
linearVelocity: [
|
|
||||||
parseFloat(linVel.x.toFixed(3)),
|
|
||||||
parseFloat(linVel.y.toFixed(3)),
|
|
||||||
parseFloat(linVel.z.toFixed(3))
|
|
||||||
],
|
|
||||||
angularVelocity: [
|
|
||||||
parseFloat(angVel.x.toFixed(3)),
|
|
||||||
parseFloat(angVel.y.toFixed(3)),
|
|
||||||
parseFloat(angVel.z.toFixed(3))
|
|
||||||
],
|
|
||||||
mass: parseFloat(mass.toFixed(2)),
|
|
||||||
restitution: parseFloat(restitution.toFixed(2))
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const snapshot: PhysicsSnapshot = {
|
const snapshot: PhysicsSnapshot = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user