About Compound hierarchy question #221
-
Hi I have a need for a tree hierarchy with the preservation of the local position relative to the parent Example of a hierarchy: |PlanetA (KinematicBody)
\ Unit1 (DynamicBody)
\ Unit2 (DynamicBody)
\ ...
|PlanetB (KinematicBody)
\ Unit1 (DynamicBody)
\ ... Units with dynamic body must have a position relative to their parent Iam looking at Compound\BigCompound and the question arose, but how to add\delete childs without rebuilding Compound? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
A A static void AddChildToCompound(ref BigCompound compound, CompoundChild child, BufferPool pool, Shapes shapes)
{
pool.Resize(ref compound.Children, compound.Children.Length + 1, compound.Children.Length);
compound.Children[^1] = child;
shapes.UpdateBounds(child.LocalPose, child.ShapeIndex, out var bounds);
compound.Tree.Add(bounds, pool);
} static void RemoveChildFromCompoundAt(ref BigCompound compound, int childIndex, BufferPool pool)
{
var movedChildIndex = compound.Tree.RemoveAt(childIndex);
if (movedChildIndex >= 0)
{
compound.Children[childIndex] = compound.Children[movedChildIndex];
}
//Shrinking the buffer takes care of 'removing' the now-empty last slot.
pool.Resize(ref compound.Children, compound.Children.Length - 1, compound.Children.Length - 1);
}
It's worth noting that the innards are exposed specifically to let people mess with them in ways that are convenient for whatever they're doing- this isn't the only way to handle modifications. In particular, if you needed to make very large numbers of modifications, doing so one by one like this will very likely be slower than a batched process like https://github.com/bepu/bepuphysics2/blob/master/Demos/DemoMeshHelper.cs#L126. I'll probably add an equivalent to the functions above to the That said, the name "DynamicBody" implies that you might be expecting dynamic behavior (in the physics simulation sense of the word) in the compound children. Children of a compound can't be independently dynamic, and while the compound shapes can support children being moved around, it's not what they were built for. Depending on your intent, there might be a more direct approach. |
Beta Was this translation helpful? Give feedback.
A
Compound
is just a list of children with no acceleration structure, so adding to/removing from it amounts to just modifying the child buffer to be whatever you want.A
BigCompound
is built for larger numbers of children, and so has aTree
acceleration structure that will also need to be updated.