c++unreal-engine4unreal-engine5unreal

Geometry collection particle's world location?


I've built a custom Geometry Collection that needs to get the absolute world location of each of the particles. But so far, I've only got the relative translation of the particle. So in the following code, before the geometry collection has broken apart from the original rest collection, all the particle translations are [0, 0, 0]:

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

    for (int32 i = 0; i < GeometryCollectionComponent->GetDynamicCollection()->GetNumTransforms(); ++i)
    {
        // This is the translation relative to the original resting location 
        UE::Math::TVector<float> T = GeometryCollectionComponent->GetDynamicCollection()->GetTransform(i).GetTranslation();
    }
}

I think I am on the right track with Particle->GetP() or Particle->GetX(). Also I've noticed that the particle at index 0 has some special behavior.


Solution

  • The answer is basically Particle->GetX() after getting a particle from the geometry collection physics proxy, but a major gotcha is that particles only update their location when they break off.

    This code comment from GeometryCollectionEngine -> GeometryCollectionComponent.cpp:2135 sheds some light on this:

    Clusters always fracture with one off pieces being removed. This means we only need to record the one offs that broke and we get the connected components for free. If the first child is disabled it means the properties apply to the parent (i.e. the cluster). If the first child is enabled it means it's a one off and the cluster IS the first child

    Ergo, we must check if each particle is disabled, and if it is, use its parent location.

    void ACustomGCActor::Tick(float DeltaTime)
    {
      Super::Tick(DeltaTime);
    
      const FGeometryCollectionPhysicsProxy* PhysicsProxy = GeometryCollectionComponent->GetPhysicsProxy();
      for (int32 i = FirstRealParticleIndex; i < GeometryCollectionComponent->GetDynamicCollection()->GetNumTransforms(); ++i)
      {
        if (Chaos::FPBDRigidClusteredParticleHandle* Particle = PhysicsProxy->GetParticle_Internal(i))
        {
          const Chaos::TVector<double, 3> Location =
            Particle->Disabled()
            ? GeometryCollectionComponent->GetPhysicsProxy()->GetParticleByIndex_External(
              GeometryCollectionComponent->GetParentArrayRest()[i + FirstRealParticleIndex])->GetX()
            : Particle->GetX();
        }
      }
    }
    

    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.