I’m trying to connect an Actor with a UPhysicsConstraintComponent
to a body of a ragdoll. The reason I do it this way, instead of just using sockets on the constraint, is bodies may have offsets from bones, and than they would not be aligned. I’d like to connect to the physics body instead of the bone.
The Physics Asset has a TArray<UPhysicsConstraintTemplate>
called ConstraintSetup, which I am adding my “Actor constraint” to. It also has a TArray<USkeletalBodySetup>
. These USkeletalBodySetups
contain the geometries I’d like to connect to,in this case capsules as FKSphylElems
. I am setting up bone and component names for the new constraint, but it does not connect objects, although the new UPhysicsConstraintTemplates and FConstraintInstances are added to the Phats ConstraintSetup(checked in the debugger).
void UBPLibrary::ConnectConstraintToSkeletalMeshActorPhysicsAsset(
UPhysicsConstraintComponent* Constraint, USkeletalMeshComponent* Mesh, FName BoneName)
{
if (!IsValid(Mesh) || !IsValid(Constraint))return;
UPhysicsAsset* Phat = Mesh->PhysicsAssetOverride;
if (!IsValid(Phat))return;
UPhysicsConstraintTemplate* constraintTemplate = NewObject<UPhysicsConstraintTemplate>();
FConstraintInstance * ConstraintInstance = &Constraint->ConstraintInstance;
ConstraintInstance->ConstraintBone1=BoneName;
ConstraintInstance->ConstraintBone2=FName(Constraint->GetName());
constraintTemplate->DefaultInstance = *ConstraintInstance;
if (!IsValid(constraintTemplate))return;
Phat->ConstraintSetup.Add(constraintTemplate);
UWorld* world = GetWorld(Constraint);
FPhysScene* PhysScene_PhysX = world->GetPhysicsScene();
Mesh->TermArticulated();
Mesh->InitArticulated(PhysScene_PhysX);
}