Unable to store PhysicsObject in FChaosUserData

Hi there physics gang!

I’ve been working on a new component which stores physics objects, but I’m creating the physics myself and as an experiment I thought I’d try forgo using BodyInstances and talk to Chaos directly in terms of setup and communication of the physics - just to keep things as lightweight as possible. Everything seems to be working fine apart from Queries due to needing to convert the results and needing to work out the actor, bone name, etc as that goes through the BodyInstance. However I saw the the PhysicsObject has this information, so I thought I’d change some code to use that and after a some bug fixes, everything seems to be working fine!

There are something that I’ve ran into and fixed:

  • Storing the FPhysicsObject in FChaosUserData, this doesn’t work due to the functions of storing the FPhysisObject in the user data is done in a header file (PhysicsInterfaceTypeCore.h) and FPhysicsObject is forward declared - however it’s not declared in the Chaos namespace, when you try and use the function to save the data, the code doesn’t compile as it doesn’t understand the Chaos::FPhysicsObject type
  • Added a fallback to CollisionConversion which will check to see if FPhysicsObject is part of the user data and will then grab the item index and bone name from here. Initially I want to use in the overriden LineTraceComponent function in my own component the Chaos::FPhysicsObjectCollisionInterface_External interface to do the query however ConvertQueryImpactHit is internal to the engine, so there is no external API that allows you to do a query directly and then convert the results if you need without having to roll your own.
  • Usage of GetBodyInstance() which returns a pointer to FBodyInstance - as the FBodyInstance struct is created as part of the primitive component, the poiter than GetBodyInstance returns always valids however this doesn’t mean that the body instance is valid as the PhysicsActor is not set (as in my case), so this means for a lot of the function we still try go through the BodyInstance - like in the LineTraceComponent case that I was using. This is obviously a big can of worms but ideally GetBodyInstance should also be checking IsValidBodyInstance and return a nullptr if it’s not but given that this API is used quite extensively, I didn’t try that and just changed the LineTraceComponent function so that it goes to the fallback of using the FPhysicsObjectCollisionInterface_External interface which uses the PhysicsObjects, which are valid in my case.

All my changes are in pull request for you to see: Support FPhysiscObject stored in FChaosUserData and support PrimitiveComponents to not use BodyInstances but fallback to using FPhysicsObjects in when doing query result conversion by Jal1338 · Pull Request #13551 · EpicGames/UnrealEngine

Apologies Jalpesh,

We’ll take a look into this next week for you.

Best

Geoff Stacey

Developer Relations

EPIC Games

Hi Jalpesh,

I can see this is in motion now on the slack channel, so I’ll go ahead and get this closed off :slight_smile:

All the best

Geoff