Why does activating physics simulation during gameplay detach components from their parent?

Why does activating physics simulation during gameplay detach components from their parent? The following code, called in an interaction event during gameplay, first prints the name of the parent SceneComponent and then “No parent!”:

if (SimObject->GetAttachParent())
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, SimObject->GetAttachParent()->GetName());

SimObject->SetSimulatePhysics(true);

if (SimObject->GetAttachParent())
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, SimObject->GetAttachParent()->GetName());
else
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, "No parent!");

Afterwards, the relative transform of the SimObject is identical to its world transform. I’ve tried reattaching the component using its AttachParent property, but that has led to various crashes. What am I missing?

Hi,

Turning simulation on does detach from the parent, this is by design. The attachment hierarchy makes it so that if you move the parent actor, all of its children will move as well. Imagine a case where you have a table as the parent and it has a book attached to it. The book is physically simulated. In this case if you manage to cause the book to move up off of the table and you then move the table to the left the book will move as well which would look like an incorrect physics simulation.

I can think of two situations where you’d want the parent’s movement to affect the child. The first is when you want some sort of constraint, for example a parent might be connected to the child in some way. In this case it’s best to use a physical constraint.

The second situation is where you would like to create a complex rigid body that is physically simulated and is made up of smaller physically simulated pieces. This second scenario is something that we are working towards supporting, but as of now it’s not fully implemented.

If you tell me more about the effect you’re trying to achieve I might have some tips.

Thanks

Thank you for your answer! That makes sense. The first situation you described applies here: I’m trying to create a door. I already have a constraint and the door swings correctly.

However, I want to be able to “close” the door so it has to be explicitly opened by the player and won’t swing when hit by an object while closed. I’ve used the relative rotation and angular velocity to detect when the door should close and then deactivated physics. But the relative rotation is identical to the world rotation when the object has no parent, so the mechanism only works when the whole blueprint isn’t rotated.

I’ve now managed to get the behavior I want by manually applying the inverse rotation from the root object. Is that the best way to do it?

By the way, when I just create a blueprint with a physically simulated object attached to a root scene, it stays attached. That had confused me, it only gets detached when dynamically activating physics.

Using the inverse rotation sounds like the right way to do it. I’m not sure what you mean about the root not being detached.

In the editor we allow you to attach physically simulated objects so that you can easily group things together. For example if you have several physically simulated books sitting on a table, we allow you to attach them to the table so that you can easily move the entire group together. Then at runtime we detach anything that’s physically simulated.

But if I just create a blueprint with a SceneComponent and a StaticMesh and turn physics on in the editor, the object stays attached during runtime and I can actually move it by modifying the SceneComponent transform. Did I understand you correctly that this is not the intended behavior? Or, if it is, why the inconsistency?

What you describe sounds like it’s not the intended behavior. However, when I try it I see that the actor’s location stays fixed while the static mesh is free to move around. If you wouldn’t mind uploading a simple setup of this I could take a look.

Yes, the actor’s location always stays fixed. But the transformation of the static mesh can be either relative to the world or to the actor. I’ve uploaded a simple example blueprint. It prints both the SceneComponent’s and the StaticMesh’s position. Try connecting the “Set Simulate Physics” node in the construction script, that produces different output for me.

If the component has no parent its relative and global location will be the same. Do you expect the relative location to be 0 in the case when the component has no parent?

No, I agree that detaching physical objects from their parent makes sense in general (although it makes things a bit more complicated in my use case). I expect the relative transform to be different from 0 if the object is not at the origin. Everything works out as I would expect if I activate the physics in a blueprint or in code.

But when I activate physics in the editor and not change it afterwards, the object is still attached to its parent during gameplay, which seems inconsistent. The observable behavior is the same, but AttachParent and the relative transform are different.

In the example you provided how do you determine that the child is still attached to the parent? The scene component has no mesh so you can’t actually see if it’s attached. I thought you were just using the relative location to figure this out

Yes, the child has relative position 0 if physics is just turned on in the editor. If SetSimulatePhysics(true) is called in the construction script (or anywhere else), it has the world position of the placed blueprint. But just printing the name of the parent gives the same result.

hi,
I recently found a case where this behavior breaks. So it looks like this is in fact a bug. I’m looking at a fix for this now, thanks for the information

an update on this:
I’ve just checked in a fix for the bug where blueprint children that are physically simulated do not get automatically detached.

This will make it into 4.3 or you can find it in master on github.

Ok, thanks for the update and your help!

Up above this was posted by Ori Cohen in 2014:


The second situation is where you would like to create a complex rigid body that is physically simulated and is made up of smaller physically simulated pieces. This second scenario is something that we are working towards supporting, but as of now it’s not fully implemented.

Did this ever get fully implemented? Is it soon going to be? It still does not seem possible to associate physics objects in such a way where some maintain their attachment to a parent or other actor / component. Certainly there are situations where having the attachment would “break” the physics, but there are also cases where it wouldn’t and could be used productively. So, at least having an option to auto detach or not would be something to put in place, especially since it has been 2 years since work was going on in this area. Thanks for any new info you can provide!

“The second situation is where you would like to create a complex rigid body that is physically simulated and is made up of smaller physically simulated pieces. This second scenario is something that we are working towards supporting, but as of now it’s not fully implemented.” I’ve noticed you also replied to this thread Is there another way to "weld" physics enabled static meshes together, or am I using constraints wrong? - World Creation - Epic Developer Community Forums
has this feature been implemented, are you still working on it, or is it dead?

In my case, I have a house that is moveable because there is a city builder aspect to my game. Am I correct if I conclude from the discussion above that I cannot use physics + constraints to open/close the door because the door will get left behind when the house moves? Ie, I’ll just need to write some custom logic?