fix: reset scene crash — remove physics bodies before entity destruct
Root cause: after Destruct(), physics world still references dead entities in its internal dictionary. SyncTransforms iterates dead entity IDs → Flecs assert ecs_is_alive. Fix: - Pause physics during reset - For each deleted entity: check Has<RigidBody>, call physicsWorld.RemoveBody to clean up Jolt body + dictionary entries BEFORE Destruct - Resume physics after CreateObjects - New objects get IsInitialized=false, physics bodies created next frame
This commit is contained in:
@@ -29,7 +29,7 @@ Size=507,263
|
|||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][Shadow & Light Parameters]
|
[Window][Shadow & Light Parameters]
|
||||||
Pos=2,200
|
Pos=22,191
|
||||||
Size=223,506
|
Size=223,506
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
|
|||||||
@@ -228,6 +228,9 @@ class Program
|
|||||||
ImGui.SameLine();
|
ImGui.SameLine();
|
||||||
if (ImGui.Button("Reset Scene"))
|
if (ImGui.Button("Reset Scene"))
|
||||||
{
|
{
|
||||||
|
// Pause physics during reset
|
||||||
|
physicsEnabled = false;
|
||||||
|
|
||||||
// Delete all entities except Camera, lights, Floor
|
// Delete all entities except Camera, lights, Floor
|
||||||
var toDelete = new List<Entity>();
|
var toDelete = new List<Entity>();
|
||||||
world.Each((Entity e, ref Transform t) =>
|
world.Each((Entity e, ref Transform t) =>
|
||||||
@@ -236,11 +239,20 @@ class Program
|
|||||||
if (name != "Camera" && name != "MainLight" && name != "SecondLight" && name != "ThirdLight" && name != "Floor")
|
if (name != "Camera" && name != "MainLight" && name != "SecondLight" && name != "ThirdLight" && name != "Floor")
|
||||||
toDelete.Add(e);
|
toDelete.Add(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Remove RigidBody from physics before destructing
|
||||||
foreach (var ent in toDelete)
|
foreach (var ent in toDelete)
|
||||||
{
|
{
|
||||||
if (ent.IsAlive()) ent.Destruct();
|
if (ent.IsAlive())
|
||||||
|
{
|
||||||
|
if (ent.Has<RigidBody>())
|
||||||
|
physicsWorld.RemoveBody(ent);
|
||||||
|
ent.Destruct();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CreateObjects(world);
|
CreateObjects(world);
|
||||||
|
physicsEnabled = true;
|
||||||
Console.WriteLine($"[App] Scene reset: {toDelete.Count} entities deleted");
|
Console.WriteLine($"[App] Scene reset: {toDelete.Count} entities deleted");
|
||||||
}
|
}
|
||||||
ImGui.End();
|
ImGui.End();
|
||||||
|
|||||||
Reference in New Issue
Block a user