From 0983f38d47ca9ac8ee8a57d6b3feb5c5dcdc3917 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Mar 2026 19:02:51 +0000 Subject: [PATCH 1/2] Fix: use instanceof instead of constructor.name for physics shape detection The physics shape update (updatePhysics) was silently failing on the GitHub Pages production build because the Babylon.js classes are minified: constructor.name is mangled (e.g. "a") so the switch on "_PhysicsShapeCapsule" and "_PhysicsShapeMesh" never matched, causing updatePhysics to return early and the physics shape to stay at its original size after scaling. - Replace getShapeTypeFromPhysics to use instanceof checks instead of constructor.name, which works correctly in both dev and minified builds - Add explicit instanceof branches for PhysicsShapeCapsule and PhysicsShapeMesh in updatePhysics, bypassing the string-based detection - Add metadata fallback in the remaining else branch as extra defence - Fix the same constructor.name pattern in animate.js and mesh.js https://claude.ai/code/session_017rYZBuYo2r36GLPJirf6ir --- api/animate.js | 2 +- api/mesh.js | 2 +- api/physics.js | 28 ++++++++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/api/animate.js b/api/animate.js index 91ed8537..18972ff5 100644 --- a/api/animate.js +++ b/api/animate.js @@ -24,7 +24,7 @@ const updateCapsuleShapeForAnimation = ( !physicsMesh || !physicsMesh.physics || !physicsMesh.physics.shape || - physicsMesh.physics.shape.constructor.name !== "_PhysicsShapeCapsule" + !(physicsMesh.physics.shape instanceof flock.BABYLON.PhysicsShapeCapsule) ) { return; } diff --git a/api/mesh.js b/api/mesh.js index 42d92f71..70ad33ed 100644 --- a/api/mesh.js +++ b/api/mesh.js @@ -72,7 +72,7 @@ export const flockMesh = { : null; if ( - physicsMesh?.physics?.shape?.constructor.name === "_PhysicsShapeCapsule" + physicsMesh?.physics?.shape instanceof flock.BABYLON.PhysicsShapeCapsule ) { const currentShape = physicsMesh.physics.shape; if ( diff --git a/api/physics.js b/api/physics.js index 9b460c21..6ea2d132 100644 --- a/api/physics.js +++ b/api/physics.js @@ -1,15 +1,13 @@ let flock; const getShapeTypeFromPhysics = (physics) => { - const shapeName = physics?.shape?.constructor?.name; - switch (shapeName) { - case "_PhysicsShapeCapsule": - return "CAPSULE"; - case "_PhysicsShapeMesh": - return "MESH"; - default: - return null; - } + if (!physics?.shape) return null; + const shape = physics.shape; + if (flock?.BABYLON?.PhysicsShapeCapsule && shape instanceof flock.BABYLON.PhysicsShapeCapsule) + return "CAPSULE"; + if (flock?.BABYLON?.PhysicsShapeMesh && shape instanceof flock.BABYLON.PhysicsShapeMesh) + return "MESH"; + return null; }; const capturePhysicsState = (targetMesh) => ({ @@ -168,8 +166,18 @@ export const flockPhysics = { Math.max(width, depth) / 2, flock.scene, ); + } else if (physicsShape instanceof flock.BABYLON.PhysicsShapeCapsule) { + detectedShapeType = "CAPSULE"; + newShape = createPhysicsShape(mesh, "CAPSULE"); + if (!newShape) return; + } else if (physicsShape instanceof flock.BABYLON.PhysicsShapeMesh) { + detectedShapeType = "MESH"; + newShape = createPhysicsShape(mesh, "MESH"); + if (!newShape) return; } else { - detectedShapeType = getShapeTypeFromPhysics(parent.physics); + detectedShapeType = + getShapeTypeFromPhysics(parent.physics) || + parent.metadata?.physicsShapeType; if (!detectedShapeType) return; newShape = createPhysicsShape(mesh, detectedShapeType); if (!newShape) return; From ae28c0a55488799a92f428d59d6f31fdf63492da Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Mar 2026 19:07:47 +0000 Subject: [PATCH 2/2] Fix: add null-safety guards to instanceof checks (CodeRabbit nitpicks) Add flock?.BABYLON?.PhysicsShapeCapsule && guards before instanceof in animate.js and the new CAPSULE/MESH branches in physics.js, consistent with the pattern already used in getShapeTypeFromPhysics. https://claude.ai/code/session_017rYZBuYo2r36GLPJirf6ir --- api/animate.js | 2 +- api/physics.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/animate.js b/api/animate.js index 18972ff5..9dfbac84 100644 --- a/api/animate.js +++ b/api/animate.js @@ -24,7 +24,7 @@ const updateCapsuleShapeForAnimation = ( !physicsMesh || !physicsMesh.physics || !physicsMesh.physics.shape || - !(physicsMesh.physics.shape instanceof flock.BABYLON.PhysicsShapeCapsule) + !(flock?.BABYLON?.PhysicsShapeCapsule && physicsMesh.physics.shape instanceof flock.BABYLON.PhysicsShapeCapsule) ) { return; } diff --git a/api/physics.js b/api/physics.js index 6ea2d132..932409cf 100644 --- a/api/physics.js +++ b/api/physics.js @@ -166,11 +166,11 @@ export const flockPhysics = { Math.max(width, depth) / 2, flock.scene, ); - } else if (physicsShape instanceof flock.BABYLON.PhysicsShapeCapsule) { + } else if (flock?.BABYLON?.PhysicsShapeCapsule && physicsShape instanceof flock.BABYLON.PhysicsShapeCapsule) { detectedShapeType = "CAPSULE"; newShape = createPhysicsShape(mesh, "CAPSULE"); if (!newShape) return; - } else if (physicsShape instanceof flock.BABYLON.PhysicsShapeMesh) { + } else if (flock?.BABYLON?.PhysicsShapeMesh && physicsShape instanceof flock.BABYLON.PhysicsShapeMesh) { detectedShapeType = "MESH"; newShape = createPhysicsShape(mesh, "MESH"); if (!newShape) return;