Triggering Physics Fields against a Geometry Collection in C++

As I wasn’t able to find much documentation on how trigger and apply physics fields against a UGeometryCollection in C++. So, I figured I would post a small example:

	// Generic Metadata
	TObjectPtr<UFieldSystemMetaDataFilter> MetaData = NewObject<UFieldSystemMetaDataFilter>();
	MetaData->SetMetaDataFilterType(
		EFieldFilterType::Field_Filter_All,
		EFieldObjectType::Field_Object_All,
		EFieldPositionType::Field_Position_CenterOfMass);

	// Apply uniform external strain
	TObjectPtr<UUniformScalar> UniformScalar = NewObject<UUniformScalar>();
	UniformScalar->Magnitude = 9999999999.0;
	GeometryCollection->ApplyPhysicsField(
		true, EGeometryCollectionPhysicsTypeEnum::Chaos_ExternalClusterStrain, MetaData.Get(), UniformScalar.Get());

	// Apply radial velocity
	TObjectPtr<URadialVector> RadialVector = NewObject<URadialVector>();
	RadialVector->Magnitude = 50.0;
	RadialVector->Position = GetActorLocation();
	GeometryCollection->ApplyPhysicsField(
		true, EGeometryCollectionPhysicsTypeEnum::Chaos_AngularVelocity, MetaData.Get(), RadialVector.Get());

	// Apply A Uniform up Vector
	TObjectPtr<UUniformVector> UniformVector = NewObject<UUniformVector>();
	UniformVector->Magnitude = 100;
	UniformVector->Direction = FVector(0, 0, 1);
	GeometryCollection->ApplyPhysicsField(true, EGeometryCollectionPhysicsTypeEnum::Chaos_LinearVelocity, MetaData.Get(), UniformVector.Get());

This is similar to some of the processing that happens via the FS_MasterField Field Node Blueprint from the Engine Content. I have not figured out how to apply a UNoiseField.

Perhaps, this is going to help someone.

4 Likes

My Geometry Collection exploeded!!! Thank you for posting this because I was having a hell of a time trying to read through docs and trying to figure this out!

Thanks, was trying with “GeometryCollectionComponent->ApplyKinematicField” and “GeometryCollectionComponent->ApplyExternalStrain” but not really getting the results I wanted.
This looks promising, got it to totally explode, now going to try and make it slowly fragment, and only after multiple hits totally fracture.

Some additional notes, you need to:

  • enable the “Chaos Fields” plugin in the .uproject (=FieldSystemPlugin).
  • have “FieldSystemEngine” (to avoid linker errors) in your .build.cs
  • include “Field/FieldSystemObjects.h” in your .cpp