c++game-physicsunreal-engine5physics-engine

Unreal Engine 5 Geometry Collection get all bones and apply force


Using Unreal Engine 5.4.3, I've implemented a custom AGeometryCollectionActor and I want to apply force to the individual bones of this collection on tick. Following is a toy version of what I'm trying to do. I can read the position of all the bones via GetAllPhysicsObjects but can't seem to get the "bone name" to pass in to the GeometryCollectionComponent->AddForce method.

ACustomGeometryCollection.cpp

void ACustomGeometryCollection::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);

    FVector TestForce = FVector(0, 0, 100); 
    for (Chaos::FPhysicsObject* Obj : GeometryCollectionComponent->GetAllPhysicsObjects())
    {
        // this has no effect
        GeometryCollectionComponent->AddForce(TestForce, Obj->GetBodyName(), true);
    }
}

Solution

  • Here's the solution

    ACustomGeometryCollection.cpp

    #include "GeometryCollection/GeometryCollectionComponent.h"
    #include "Runtime/Experimental/Chaos/Private/Chaos/PhysicsObjectInternal.h"
    
    void ACustomGeometryCollection::Tick(float DeltaSeconds)
    {
        Super::Tick(DeltaSeconds);
    
        FVector TestForce = FVector(0, 0, 100); 
        for (int32 i = FirstRealParticleIndex; i < GeometryCollectionComponent->GetDynamicCollection()->GetNumTransforms(); ++i)
        {
            GeometryCollectionComponent->ApplyLinearVelocity(i, TestForce);
        }
    }
    

    See also: how to compute FirstRealParticleIndex how to tell real particles from clusters (and ignore the cluster union particles)

    Don't forget to add "GeometryCollectionEngine" to the dependency module names in the <Project>.Build.cs file.