From c7908394eb3ec3aa0f54bf26a98a683c1950ac15 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 25 Nov 2026 22:16:01 +0100 Subject: [PATCH] Broad-phase: rebuild the trees when the move buffer is empty b3BroadPhase_DestroyProxy unbuffers the proxy's move, but its ancestors in the dynamic tree keep the enlarged flags set when it was enlarged in the last step. If it was the only buffered proxy, b3UpdateBroadPhasePairs returned before the tree rebuild that clears them, and the next solve tripped ValidateNoEnlarged (dynamic_tree.c:1081) with validation on. In release the stale flags only deferred the rebuild. The early return now runs the rebuild; on a tree with no enlarged node it is a walk of the root. Upstream replaced this move buffer with moved flags in the tree (#159). DestroyEnlargedProxyTest: aborts before, passes after. Co-Authored-By: Claude Opus 5.5 --- src/broad_phase.c | 3 +++ test/test_world.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/broad_phase.c b/src/broad_phase.c index a14a1a2..058aa79 100644 --- a/src/broad_phase.c +++ b/src/broad_phase.c @@ -403,6 +403,9 @@ void b3UpdateBroadPhasePairs( b3World* world ) if ( moveCount == 0 ) { + // GameCult fork: destroying a proxy unbuffers its move but leaves its ancestors flagged enlarged, so an empty + // move buffer does not mean the trees are clean. The rebuild clears the flags; on a clean tree it is a no-op. + b3UpdateTreesTask( world ); return; } diff --git a/test/test_world.c b/test/test_world.c index 714430f..0517671 100644 --- a/test/test_world.c +++ b/test/test_world.c @@ -1122,6 +1122,47 @@ int ContactFrictionRecycledTest( void ) return 0; } +// GameCult fork. A body whose proxy was enlarged in the last step is destroyed before the next step, and it was +// the only proxy in the move buffer. Its ancestors in the dynamic tree are still flagged enlarged, so the next +// step must still rebuild the tree: with validation on, a skipped rebuild trips ValidateNoEnlarged in the solver. +int DestroyEnlargedProxyTest( void ) +{ + b3WorldDef worldDef = b3DefaultWorldDef(); + worldDef.gravity = b3Vec3_zero; + b3WorldId worldId = b3CreateWorld( &worldDef ); + + b3BodyDef bodyDef = b3DefaultBodyDef(); + bodyDef.type = b3_dynamicBody; + b3ShapeDef shapeDef = b3DefaultShapeDef(); + b3Sphere sphere = { b3Vec3_zero, 0.25f }; + + // Resting bodies far apart: they never leave their fat AABBs, so after the first step only the mover is buffered. + enum + { + e_count = 8 + }; + b3BodyId bodyIds[e_count]; + for ( int i = 0; i < e_count; ++i ) + { + bodyDef.position = (b3Pos){ 4.0f * i, 0.0f, 0.0f }; + bodyIds[i] = b3CreateBody( worldId, &bodyDef ); + b3CreateSphereShape( bodyIds[i], &shapeDef, &sphere ); + } + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + + // Each body in turn is the mover; some lie deep enough that an enlarged ancestor outlives their removal. + for ( int i = 0; i < e_count; ++i ) + { + b3Body_SetLinearVelocity( bodyIds[i], (b3Vec3){ 0.0f, 30.0f, 0.0f } ); + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + b3DestroyBody( bodyIds[i] ); + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + } + + b3DestroyWorld( worldId ); + return 0; +} + int WorldTest( void ) { RUN_SUBTEST( HelloWorld ); @@ -1145,6 +1186,7 @@ int WorldTest( void ) RUN_SUBTEST( TestHullDatabase ); RUN_SUBTEST( ContactFrictionHookTest ); RUN_SUBTEST( ContactFrictionRecycledTest ); + RUN_SUBTEST( DestroyEnlargedProxyTest ); return 0; }