Skip to content

Commit

Permalink
Optimize shape removal (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
notgiven688 authored May 10, 2024
1 parent 12641bd commit 0b8c7b2
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/Jitter2/Dynamics/RigidBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,22 +430,16 @@ public void RemoveShape(Shape shape, bool setMassInertia = true)
"Shape is not part of this body.");
}

Stack<Arbiter> toRemoveArbiter = new();

foreach (var contact in Contacts)
foreach (var arbiter in Contacts)
{
if (contact.Handle.Data.Key.Key1 == shape.ShapeId || contact.Handle.Data.Key.Key2 == shape.ShapeId)
if (arbiter.Handle.Data.Key.Key1 == shape.ShapeId || arbiter.Handle.Data.Key.Key2 == shape.ShapeId)
{
toRemoveArbiter.Push(contact);
// Removes the current element we are iterating over from Contacts, i.e. the HashSet
// we are iterating over is altered. This is allowed.
World.Remove(arbiter);
}
}

while (toRemoveArbiter.Count > 0)
{
var tr = toRemoveArbiter.Pop();
World.Remove(tr);
}

World.DynamicTree.RemoveProxy(shape);
shape.DetachRigidBody();
World.InternalRemoveShape(shape);
Expand Down

0 comments on commit 0b8c7b2

Please sign in to comment.