Attaching two actors (of the same type) using a physics constraint using c++

Just to preface, I’m very new to unreal so some of the terms may be used incorrectly and how things are supposed to be done in general.

What I’m attempting to do is basically have two actors of the same type I created (in this case two shopping carts that have wheels that roll them around and stuff), attach to each other whenever they bump into each other.

What I’m having issues with is when I run the code to attach them with a physics constraint, they go flying away really fast.

Here’s a cleaned up version of the code that’s attached to each carts tickcomponent script. I’m basically trying to have it so that, whenever a cart collides with another cart, it creates a physics constraint that attaches their bodies together. The If statement at the top is supposed to make sure it only creates one physics constraint and only runs when the collision is with another cart.

For a bit more clarity on what the carts are, they are blueprints with a body that is rectangular prism with physics enabled on top of four wheels that are attached to the body with physics constraints. The wheels roll with physics and carry the body around, basically like how a cart would in real life.

Apologies if the code isn’t very readable, I wasn’t sure of how the formatting on the forums worked exactly.

TArray<AActor*> Result;
GetOwner()->GetOverlappingActors(Result, USphereCartActor::StaticClass());
if (GEngine) {
	for (int i = 0; i < Result.Num(); i++) {

		if (Result[i]->GetClass() == GetOwner()->GetClass() && Result[i]->GetComponentsByTag(USceneComponent::StaticClass(), FName("AttachmentTag")).Num() == 0) {


>GetComponentsByTag(USceneComponent::StaticClass(), FName("BodyTag"))[0];

			UActorComponent* other_body = Result[i]->GetComponentsByTag(USceneComponent::StaticClass(), FName("BodyTag"))[0];

			USceneComponent* owner_body_scene = Cast<USceneComponent>(owner_body);
			USceneComponent* other_body_scene = Cast<USceneComponent>(other_body);

			UPhysicsConstraintComponent* NewPhysicsConstraint = NewObject<UPhysicsConstraintComponent>();
			NewPhysicsConstraint->ComponentTags.Add(FName("AttachmentTag"));
			NewPhysicsConstraint->SetVisibility(true);

			NewPhysicsConstraint->SetConstrainedComponents(Cast<UPrimitiveComponent>(owner_body_scene), NAME_None, Cast<UPrimitiveComponent>(other_body_scene), NAME_None);


		}


	}


}

I haven’t looked very carefully, but ‘flying away really fast’ when adding physics constraints is almost always an issue with collision. Object try to push against each other if they overlap when spawned. If physics constraints are added, this results in 2 forces (one pushing, one pulling), which due to floating point arithmetic errors results in undefined behavior (fly-away-epilepsy).

TLDR: Make sure your object are not colliding with each other when you’re adding constraints! Also note that ‘not colliding’ means not that they visually seem to not collide, but that their collision meshes are not intersecting. A super-quick way to check that this is the core issue is to spawn your actors far away from each other (removing any chance of collision) and see if the problem persists.