Removing elements from a Chaos Geometry Collection at runtime (destroy active transforms)

Hey guys, I am trying to do seemingly simple task - destroy active parts of a live geometry collection. I want to destroy the broken pieces after some time, so only the hole in the wall remains (see image).

EDIT: I found the bRemoveOnMaxSleep and EnableRemovePiecesOnFracture switches, but I still need to serialize the GC with the “missing” pieces and de-serialize it on game load. How do I do that?

I am ofc using sleep fields, but my end goal is to save the collection like this and then load it without the debris pieces. Yesterday I was trying to find a way how to serialize and de-serialize the positions, but I had to dig deep into the Chaos core code, and that’s above my skill level. So I decided an easier approach - just delete the parts after some time.

My code right now does have some effect, it deletes the parts, but with a lot of exceptions in the core code and on certain GC objects it crashes entirely.

I must be missing some important step, like updating the chaos solver before getting the dynamic GC or something (I don’t know how to work with the dynamic and rest GC at all). I just want to delete the active pieces, but can’t…

// get the geometry collection on my GeometryCollectionActor
UGeometryCollectionComponent* gc_comp = GetGeometryCollectionComponent();
auto dynamic_collection = gc_comp->GetDynamicCollection();
const TManagedArray<FTransform>& gc_transforms = dynamic_collection->GetAttribute<FTransform>("Transform", FGeometryCollection::TransformGroup);
const TManagedArray<bool>& active_pieces = dynamic_collection->GetAttribute<bool>("Active", FGeometryCollection::TransformGroup);
	
// I was getting an exception "GlobalMatrices (Num=%d) cached on GeometryCollectionComponent are not in sync with ExemplarIndexArray (Num=%d) on underlying GeometryCollection; likely missed a dynamic data update" so I added this
auto GeometryCollectionEdit = GetGeometryCollectionComponent()->EditRestCollection(GeometryCollection::EEditUpdate::Dynamic);
auto result = GeometryCollectionEdit.GetRestCollection();
	
// here I get the active pieces ("Active" TManagedArray in the collection)
TArray<int32> to_delete;
for (int i = 0; i < gc_transforms.Num(); ++i)
{
	if (active_pieces[i])
	{
		to_delete.Add(i);
		UE_LOG(LogTemp, Warning, TEXT("Active piece: %s"), *(gc_transforms[i].GetLocation().ToString()));
	}
}

// and here I try to remove it.
UE_LOG(LogTemp, Warning, TEXT("Running delete chaos"));
// result->RemoveElements("Geometry", to_delete);
result->RemoveElements("Transform", to_delete);

1 Like

Hi

Geometry collections have already a removal on max sleep feature in version 5.0 that will shrink and remove broken pieces from the simulation after they go to sleep for a configurable duration ( sleeping in a physics point of view )

You can enable this feature in the geometry collection asset

Version 5.1 will introduce a number of improvement on this feature as well as add a remove on break feature where pieces will be removed after a certain time after they broke off

Hope this helps

1 Like

@CedUE Thank you, I found the variables and used them successfully (I am on 5.0.3).

Can you also tell me how do I correctly serialize and deserialize a chaos collection from disk? I can save the GeometryCollection with Serialize into FArchive, but I don’t know how to deserialize it, there is no function for that. I want to put the geometry collection in identical state after loading it as it was when saved to disk.

If I can’t deserialize it directly, is there a way I can destroy certain pieces of the geometry collection (I have a list of active pieces as seen in the code chunk), with skipping the physics?
On game load, I simply want to spawn the GC and remove the pieces that correspond to created holes.

Serializing the broken state is quite tricky as this is driven by the simulation code
Since the breaking is hierarchical ( clusters can hold clusters for example ) you would need a way to capture the history of breaking and replay that when reloading the asset
In 5.1 this may be a bit simpler as we normally return the item index as part of the break event

Another option may be to set the simulatable attribute of the broken pieces to false ( see Geometry Collection Physics Proxy code ) and to hide them somehow that would force the geometry collection to not create a physics object for them

Hope this helps

2 Likes

Did the remove on break feature make it into 5.1?
Also will there be a remove on impact setting added as well?

Edit: Nevermind, found that remove on break was added, works great.

Sry to be a dufus but I can’t find this option in 5.1.1. On the instance of the geometry collection there’s Allow Removal on Sleep/ on Break, but then in the actual Geometry Collection Asset the only Removal options I see are related to Sleep.

@Sweatymoose2
These options are in the Fracture mode “toolbar”. Check the buttons there (if I remember correctly the button was somewhere at the bottom of the toolbar).

Also took me a bit of time to find.

1 Like