In UE4 C++, how do I snap two object faces together during runtime so they are flush with one another?,How do I create a constraint in UE4 such that the bodies are projected together?

Okay, so I am trying to create a simple self-reconfigurable robotic simulator in UE4. I have the distributed control, message passing, etc. working, but I am somewhat stuck on the dynamic constraints. I have written a self-reconfigurable simulator using OpenGL, PhysX, and OpenFrameworks, but I would love to implement this in UE4 to take advantage of its vast array of features (drag and drop, easy environment creation, etc.).

I have a lot of experience with using PhysX directly, and I can use PhysX directly in my other simulator to create fixed joints that “pull” the constrained components together automatically. PhysX does this automatically using low level flags and appropriate relative positions/orientations of the bodies to be constrained. I am trying to achieve something similar in UE4.

I am attaching a picture of the situation I have and what I would like to have after the correction. I have two robotic modules (in the simplest case) that need to dock together during runtime

. These are autonomous robots, so it is impossible that they will ever line their docks up exactly such that there is zero error. I am able to spawn a physics constraint (fixed joint) to hold them together and weld them together, but the difference in position and orientation remains, even when the constraint is applied. They are simply stuck together as shown in the picture. (Note that the error in this picture is exaggerated. It is not usually this much.) This is problematic, because we have physical mechanisms on the real hardware that guide the docks into the correct place. So, I need to fix the position and orientation error during runtime to “dock” the modules together flush.

My question is whether or not anyone knows if something similar can be achieved in UE4? I read about collision bounds and overlaps and didn’t know if maybe I could detect an overlap at a certain tolerance and use that somehow. I could manually move the modules, but, in general, each side could have more modules connected, meaning all of them would have to be relocated and reoriented, which would be quite tedious. I didn’t know if anyone else knew of a simpler way to handle this?

Any help would be appreciated.

Best,

TJ

If the pieces are supposed to rigidly attach to each other and then act as one, I would look into “AttachTo” rather than using a physics constraint. Example using blueprints: Attach Actor to another Actor based on Socket points - Blueprint - Epic Developer Community Forums. Docs on C++: USceneComponent::AttachTo | Unreal Engine Documentation.

Ok, so based on rantrod’s help, the following code worked. In summary, I had to use AttachRootComponentTo in order to line up the docks, then I used a physics constraint actor to actually make the connection. It should be enough to weld the elements together once they are lined up, but that part I still can’t get to work. Thanks!:

        connections[modules[0]->GetIDNumber()][BACK_DOCK] = modules[1]->GetDocks()[FRONT_DOCK];
        connections[modules[1]->GetIDNumber()][FRONT_DOCK] = modules[0]->GetDocks()[BACK_DOCK];
        modules[0]->SetDockEngaged(BACK_DOCK, true);
        modules[1]->SetDockEngaged(FRONT_DOCK, true);
        modules[0]->AttachRootComponentTo(modules[1]->GetRootComponent(),"",EAttachLocation::SnapToTarget, true);
        modules[0]->GetRootComponent()->SetRelativeRotation(FRotator(0,0,0));
        modules[0]->GetRootComponent()->SetRelativeLocation(FVector(0,0,190));
        modules[0]->WeldDockToComponent(BACK_DOCK, modules[1]->GetDockComponent(FRONT_DOCK));
        APhysicsConstraintActor *a = GetWorld()->SpawnActor<APhysicsConstraintActor>();
         a->GetConstraintComp()->SetConstrainedComponents(modules[0]->GetSlaveOuterMesh(), "BackDock", modules[1]->GetMasterOuterMesh(), "FrontDock");
         a->GetConstraintComp()->SetLinearXLimit(ELinearConstraintMotion::LCM_Locked, 0);
         a->GetConstraintComp()->SetLinearYLimit(ELinearConstraintMotion::LCM_Locked, 0);
         a->GetConstraintComp()->SetLinearZLimit(ELinearConstraintMotion::LCM_Locked, 0);
         a->GetConstraintComp()->SetAngularSwing1Limit(EAngularConstraintMotion::ACM_Locked, 0);
         a->GetConstraintComp()->SetAngularSwing2Limit(EAngularConstraintMotion::ACM_Locked, 0);
         a->GetConstraintComp()->SetAngularTwistLimit(EAngularConstraintMotion::ACM_Locked, 0);

Thanks for your help! How do I give you credit for the answer?

I’ll repost my answer her for credit :slight_smile:

If the pieces are supposed to rigidly attach to each other and then act as one, I would look into “AttachTo” rather than using a physics constraint. Example using blueprints: Attach Actor to another Actor based on Socket points - Blueprint - Epic Developer Community Forums. Docs on C++: USceneComponent::AttachTo | Unreal Engine Documentation.